@@ -170,7 +170,12 @@ func (srv *Server) config(ctx Context) *gossh.ServerConfig {
170170 config .KeyboardInteractiveCallback = func (conn gossh.ConnMetadata , challenger gossh.KeyboardInteractiveChallenge ) (* gossh.Permissions , error ) {
171171 resetPermissions (ctx )
172172 applyConnMetadata (ctx , conn )
173- if ok := srv .KeyboardInteractiveHandler (ctx , challenger ); ! ok {
173+ ok := srv .KeyboardInteractiveHandler (ctx , challenger )
174+ err := ensureNoPKInPermissions (ctx )
175+ if err != nil {
176+ return ctx .Permissions ().Permissions , err
177+ }
178+ if ! ok {
174179 return ctx .Permissions ().Permissions , fmt .Errorf ("permission denied" )
175180 }
176181 return ctx .Permissions ().Permissions , nil
@@ -302,6 +307,30 @@ func (srv *Server) HandleConn(newConn net.Conn) {
302307 return
303308 }
304309
310+ if sshConn .Permissions != nil {
311+ // Now that the connection was authed, if the permissionsPublicKeyExt was
312+ // attached, we need to re-parse it as a public key.
313+ if keyData , ok := sshConn .Permissions .Extensions [permissionsPublicKeyExt ]; ok {
314+ decodedData , err := base64 .StdEncoding .DecodeString (keyData )
315+ if err != nil {
316+ if srv .ConnectionFailedCallback != nil {
317+ srv .ConnectionFailedCallback (conn , err )
318+ }
319+ return
320+ }
321+
322+ key , err := gossh .ParsePublicKey (decodedData )
323+ if err != nil {
324+ if srv .ConnectionFailedCallback != nil {
325+ srv .ConnectionFailedCallback (conn , err )
326+ }
327+ return
328+ }
329+
330+ ctx .SetValue (ContextKeyPublicKey , key )
331+ }
332+ }
333+
305334 // Additionally, now that the connection was authed, we can take the
306335 // permissions off of the gossh.Conn and re-attach them to the Permissions
307336 // object stored in the Context.
0 commit comments