Search in sources :

Example 1 with ServletUtils

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

the class OIDCRequestResolverTest method testIsAuthorizationCodeResponse.

@Test
public void testIsAuthorizationCodeResponse() throws IOException, ParseException, URISyntaxException {
    MockedStatic<AuthorizationResponse> mockedAuthorizationResponse = mockStatic(AuthorizationResponse.class);
    MockedStatic<ServletUtils> mockedServletUtils = mockStatic(ServletUtils.class);
    HTTPRequest httpRequest = mock(HTTPRequest.class);
    AuthorizationResponse authorizationResponse = mock(AuthorizationResponse.class);
    AuthorizationSuccessResponse authorizationSuccessResponse = mock(AuthorizationSuccessResponse.class);
    AuthorizationCode authzCode = new AuthorizationCode("auth-code");
    when(ServletUtils.createHTTPRequest(request)).thenReturn(httpRequest);
    when(AuthorizationResponse.parse(httpRequest)).thenReturn(authorizationResponse);
    when(authorizationResponse.indicatesSuccess()).thenReturn(true);
    when(authorizationResponse.toSuccessResponse()).thenReturn(authorizationSuccessResponse);
    when(authorizationSuccessResponse.getAuthorizationCode()).thenReturn(authzCode);
    OIDCRequestResolver resolver = new OIDCRequestResolver(request, oidcAgentConfig);
    assertTrue(resolver.isAuthorizationCodeResponse());
    mockedAuthorizationResponse.close();
    mockedServletUtils.close();
}
Also used : ServletUtils(com.nimbusds.oauth2.sdk.http.ServletUtils) AuthorizationCode(com.nimbusds.oauth2.sdk.AuthorizationCode) HTTPRequest(com.nimbusds.oauth2.sdk.http.HTTPRequest) AuthorizationSuccessResponse(com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse) AuthorizationResponse(com.nimbusds.oauth2.sdk.AuthorizationResponse) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with ServletUtils

use of com.nimbusds.oauth2.sdk.http.ServletUtils 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)

Aggregations

AuthorizationCode (com.nimbusds.oauth2.sdk.AuthorizationCode)2 AuthorizationResponse (com.nimbusds.oauth2.sdk.AuthorizationResponse)2 AuthorizationSuccessResponse (com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse)2 HTTPRequest (com.nimbusds.oauth2.sdk.http.HTTPRequest)2 ServletUtils (com.nimbusds.oauth2.sdk.http.ServletUtils)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Test (org.testng.annotations.Test)2 JWT (com.nimbusds.jwt.JWT)1 AccessTokenResponse (com.nimbusds.oauth2.sdk.AccessTokenResponse)1 TokenResponse (com.nimbusds.oauth2.sdk.TokenResponse)1 State (com.nimbusds.oauth2.sdk.id.State)1 AccessToken (com.nimbusds.oauth2.sdk.token.AccessToken)1 RefreshToken (com.nimbusds.oauth2.sdk.token.RefreshToken)1 Tokens (com.nimbusds.oauth2.sdk.token.Tokens)1 Nonce (com.nimbusds.openid.connect.sdk.Nonce)1 RequestContext (io.asgardeo.java.oidc.sdk.bean.RequestContext)1 SessionContext (io.asgardeo.java.oidc.sdk.bean.SessionContext)1 HashMap (java.util.HashMap)1 HttpSession (javax.servlet.http.HttpSession)1