protected Principal checkExternalAuthentication ( final HttpServletRequest request , final HttpServletResponse response ) throws FrameworkException { final String path = PathHelper . clean ( request . getPathInfo ( ) ) ; final String [ ] uriParts = PathHelper . getParts ( path ) ; logger . debug ( "Checking external authentication ..." ) ; if ( uriParts == null || uriParts . length != 3 || ! ( "oauth" . equals ( uriParts [ 0 ] ) ) ) { logger . debug ( "Incorrect URI parts for OAuth process, need /oauth/<name>/<action>" ) ; return null ; } final String name = uriParts [ 1 ] ; final String action = uriParts [ 2 ] ; // Try to getValue an OAuth2 server for the given name final StructrOAuthClient oauthServer = StructrOAuthClient . getServer ( name ) ; if ( oauthServer == null ) { logger . debug ( "No OAuth2 authentication server configured for {}" , path ) ; return null ; } if ( "login" . equals ( action ) ) { try { response . sendRedirect ( oauthServer . getEndUserAuthorizationRequestUri ( request ) ) ; return null ; } catch ( Exception ex ) { logger . error ( "Could not send redirect to authorization server" , ex ) ; } } else if ( "auth" . equals ( action ) ) { final String accessToken = oauthServer . getAccessToken ( request ) ; final SecurityContext superUserContext = SecurityContext . getSuperUserInstance ( ) ; if ( accessToken != null ) { logger . debug ( "Got access token {}" , accessToken ) ; String value = oauthServer . getCredential ( request ) ; logger . debug ( "Got credential value: {}" , new Object [ ] { value } ) ; if ( value != null ) { final PropertyKey credentialKey = oauthServer . getCredentialKey ( ) ; Principal user = AuthHelper . getPrincipalForCredential ( credentialKey , value ) ; if ( user == null && Settings . RestUserAutocreate . getValue ( ) ) { user = RegistrationResource . createUser ( superUserContext , credentialKey , value , true , getUserClass ( ) , null ) ; // let oauth implementation augment user info oauthServer . initializeUser ( user ) ; } if ( user != null ) { AuthHelper . doLogin ( request , user ) ; HtmlServlet . setNoCacheHeaders ( response ) ; try { logger . debug ( "Response status: {}" , response . getStatus ( ) ) ; response . sendRedirect ( oauthServer . getReturnUri ( ) ) ; } catch ( IOException ex ) { logger . error ( "Could not redirect to {}: {}" , new Object [ ] { oauthServer . getReturnUri ( ) , ex } ) ; } return user ; } } } } try { response . sendRedirect ( oauthServer . getErrorUri ( ) ) ; } catch ( IOException ex ) { logger . error ( "Could not redirect to {}: {}" , new Object [ ] { oauthServer . getReturnUri ( ) , ex } ) ; } return null ; }
This method checks all configured external authentication services .