Search in sources :

Example 6 with AuthorizationSuccessResponse

use of com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse in project asgardeo-java-oidc-sdk by asgardeo.

the class DefaultOIDCManagerTest method testHandleOIDCCallback.

@Test
public void testHandleOIDCCallback() throws Exception {
    AccessToken accessToken = new AccessToken(AccessTokenType.BEARER, "sampleAccessToken") {

        @Override
        public String toAuthorizationHeader() {
            return null;
        }
    };
    RefreshToken refreshToken = new RefreshToken("sampleRefreshToken");
    Tokens tokens = new Tokens(accessToken, refreshToken);
    Map<String, Object> customParameters = new HashMap<>();
    String parsedIdToken = "eyJ4NXQiOiJNell4TW1Ga09HWXdNV0kwWldObU5EY3hOR1l3WW1NNFpUQTNNV0kyTkRBelpHUXpOR00wWkdS" + "bE5qSmtPREZrWkRSaU9URmtNV0ZoTXpVMlpHVmxOZyIsImtpZCI6Ik16WXhNbUZrT0dZd01XSTBaV05tTkRjeE5HWXdZbU00WlR" + "BM01XSTJOREF6WkdRek5HTTBaR1JsTmpKa09ERmtaRFJpT1RGa01XRmhNelUyWkdWbE5nX1JTMjU2IiwiYWxnIjoiUlMyNTYifQ" + ".eyJhdF9oYXNoIjoiSEJOUlJOeTlaVy1CMXF3dFdLRkJEZyIsInN1YiI6ImFsZXhAY2FyYm9uLnN1cGVyIiwiY291bnRyeSI6Ik" + "xLIiwiYW1yIjpbIkJhc2ljQXV0aGVudGljYXRvciJdLCJpc3MiOiJodHRwczpcL1wvbG9jYWxob3N0Ojk0NDNcL29hdXRoMlwvd" + "G9rZW4iLCJzaWQiOiJkYmJhNGNkMC0wNWRjLTQxN2QtYTcwYy1lOGNmYmNiNDlhMDMiLCJhdWQiOiJLRTRPWWVZX2dmWXd6UWJK" + "YTl0R2hqMWhaSk1hIiwiY19oYXNoIjoiWXhUQ25rZ2UtOG9PSWZ3RUpmS2tfdyIsIm5iZiI6MTYwMjIyNjA5MSwiYXpwIjoiS0U" + "0T1llWV9nZll3elFiSmE5dEdoajFoWkpNYSIsImV4cCI6MTYwMjIyOTY5MSwiaWF0IjoxNjAyMjI2MDkxLCJlbWFpbCI6ImFsZX" + "hAd3NvMi5jb20ifQ.pHwsQqn64tif2J6iYcRShK_85WO3aBuL7Pz8urcHErXjyh6zvroOqSWD9KbSxJPocyoIshdqWdAEhdURKL" + "tXiw-l73HlvnX4qJKYT71VKXMTC26Z8dlk4TgytXiskmj8OpAcem3czuEWTrTLVbYzIw71p9kx-5Xxb9WNvzBg1YpwGC8MK3dkW" + "TfmUsu6oncIvHyv-gbX3kJebgMserp";
    JWT idToken = JWTParser.parse(parsedIdToken);
    customParameters.put(SSOAgentConstants.ID_TOKEN, parsedIdToken);
    when(requestResolver.isError()).thenReturn(false);
    when(requestResolver.isAuthorizationCodeResponse()).thenReturn(true);
    MockedStatic<AuthorizationResponse> mockedAuthorizationResponse = mockStatic(AuthorizationResponse.class);
    MockedStatic<ServletUtils> mockedServletUtils = mockStatic(ServletUtils.class);
    MockedStatic<TokenResponse> mockedTokenResponse = mockStatic(TokenResponse.class);
    HTTPRequest httpRequest = mock(HTTPRequest.class);
    AuthorizationResponse authorizationResponse = mock(AuthorizationResponse.class);
    AuthorizationSuccessResponse successResponse = mock(AuthorizationSuccessResponse.class);
    AuthorizationCode authorizationCode = mock(AuthorizationCode.class);
    TokenResponse tokenResponse = mock(TokenResponse.class);
    AccessTokenResponse accessTokenResponse = mock(AccessTokenResponse.class);
    when(ServletUtils.createHTTPRequest(request)).thenReturn(httpRequest);
    when(AuthorizationResponse.parse(httpRequest)).thenReturn(authorizationResponse);
    when(authorizationResponse.indicatesSuccess()).thenReturn(true);
    when(authorizationResponse.toSuccessResponse()).thenReturn(successResponse);
    when(successResponse.getAuthorizationCode()).thenReturn(authorizationCode);
    when(TokenResponse.parse((HTTPResponse) any())).thenReturn(tokenResponse);
    when(tokenResponse.indicatesSuccess()).thenReturn(true);
    when(tokenResponse.toSuccessResponse()).thenReturn(accessTokenResponse);
    when(accessTokenResponse.getTokens()).thenReturn(tokens);
    when(accessTokenResponse.getCustomParameters()).thenReturn(customParameters);
    HttpSession session = mock(HttpSession.class);
    when(request.getSession(false)).thenReturn(session);
    when(session.getAttribute(SSOAgentConstants.NONCE)).thenReturn(new Nonce());
    RequestContext requestContext = new RequestContext(new State("state"), new Nonce());
    OIDCManager oidcManager = new DefaultOIDCManager(oidcAgentConfig);
    SessionContext sessionContext = oidcManager.handleOIDCCallback(request, response, requestContext);
    assertEquals(sessionContext.getAccessToken(), accessToken.toJSONString());
    assertEquals(sessionContext.getRefreshToken(), refreshToken.getValue());
    assertEquals(sessionContext.getIdToken(), parsedIdToken);
    assertEquals(sessionContext.getUser().getSubject(), "alex@carbon.super");
    mockedAuthorizationResponse.close();
    mockedServletUtils.close();
    mockedTokenResponse.close();
}
Also used : HashMap(java.util.HashMap) RefreshToken(com.nimbusds.oauth2.sdk.token.RefreshToken) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) AuthorizationSuccessResponse(com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse) RequestContext(io.asgardeo.java.oidc.sdk.bean.RequestContext) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse) Tokens(com.nimbusds.oauth2.sdk.token.Tokens) AuthorizationCode(com.nimbusds.oauth2.sdk.AuthorizationCode) HTTPRequest(com.nimbusds.oauth2.sdk.http.HTTPRequest) JWT(com.nimbusds.jwt.JWT) HttpSession(javax.servlet.http.HttpSession) AuthorizationResponse(com.nimbusds.oauth2.sdk.AuthorizationResponse) Nonce(com.nimbusds.openid.connect.sdk.Nonce) ServletUtils(com.nimbusds.oauth2.sdk.http.ServletUtils) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse) TokenResponse(com.nimbusds.oauth2.sdk.TokenResponse) State(com.nimbusds.oauth2.sdk.id.State) SessionContext(io.asgardeo.java.oidc.sdk.bean.SessionContext) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with AuthorizationSuccessResponse

use of com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse in project asgardeo-java-oidc-sdk by asgardeo.

the class OIDCRequestResolver method isAuthorizationCodeResponse.

/**
 * Checks if the request is an Authorization Code response.
 *
 * @return True if the request is parsed as a valid Authorization response, false otherwise.
 */
public boolean isAuthorizationCodeResponse() {
    AuthorizationResponse authorizationResponse;
    AuthorizationSuccessResponse authorizationSuccessResponse;
    try {
        authorizationResponse = AuthorizationResponse.parse(ServletUtils.createHTTPRequest(request));
    } catch (com.nimbusds.oauth2.sdk.ParseException | IOException e) {
        logger.log(Level.ERROR, "Error occurred while parsing the authorization response.", e);
        return false;
    }
    if (!authorizationResponse.indicatesSuccess()) {
        logErrorAuthorizationResponse(authorizationResponse);
        return false;
    }
    authorizationSuccessResponse = authorizationResponse.toSuccessResponse();
    if (authorizationSuccessResponse.getAuthorizationCode() == null) {
        return false;
    }
    return true;
}
Also used : AuthorizationSuccessResponse(com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse) IOException(java.io.IOException) AuthorizationResponse(com.nimbusds.oauth2.sdk.AuthorizationResponse)

Aggregations

AuthorizationSuccessResponse (com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse)7 AuthorizationCode (com.nimbusds.oauth2.sdk.AuthorizationCode)5 AuthorizationResponse (com.nimbusds.oauth2.sdk.AuthorizationResponse)5 AccessTokenResponse (com.nimbusds.oauth2.sdk.AccessTokenResponse)3 TokenResponse (com.nimbusds.oauth2.sdk.TokenResponse)3 HTTPRequest (com.nimbusds.oauth2.sdk.http.HTTPRequest)3 IOException (java.io.IOException)3 AuthorizationRequest (com.nimbusds.oauth2.sdk.AuthorizationRequest)2 TokenRequest (com.nimbusds.oauth2.sdk.TokenRequest)2 ServletUtils (com.nimbusds.oauth2.sdk.http.ServletUtils)2 State (com.nimbusds.oauth2.sdk.id.State)2 Tokens (com.nimbusds.oauth2.sdk.token.Tokens)2 URI (java.net.URI)2 List (java.util.List)2 Map (java.util.Map)2 HttpSession (javax.servlet.http.HttpSession)2 JWT (com.nimbusds.jwt.JWT)1 AuthorizationCodeGrant (com.nimbusds.oauth2.sdk.AuthorizationCodeGrant)1 AuthorizationGrant (com.nimbusds.oauth2.sdk.AuthorizationGrant)1 ResponseType (com.nimbusds.oauth2.sdk.ResponseType)1