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();
}
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;
}
Aggregations