use of io.asgardeo.java.oidc.sdk.bean.SessionContext in project asgardeo-java-oidc-sdk by asgardeo.
the class OIDCRequestBuilderTest method setUp.
@BeforeMethod
public void setUp() throws URISyntaxException, ParseException {
ClientID clientID = new ClientID("sampleClientId");
Scope scope = new Scope("sampleScope1", "openid");
URI callbackURI = new URI("http://test/sampleCallbackURL");
URI authorizationEndpoint = new URI("http://test/sampleAuthzEP");
URI logoutEP = new URI("http://test/sampleLogoutEP");
URI redirectionURI = new URI("http://test/sampleRedirectionURL");
JWT idToken = JWTParser.parse("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwia" + "WF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c");
oidcAgentConfig = mock(OIDCAgentConfig.class);
sessionContext = mock(SessionContext.class);
when(oidcAgentConfig.getConsumerKey()).thenReturn(clientID);
when(oidcAgentConfig.getScope()).thenReturn(scope);
when(oidcAgentConfig.getCallbackUrl()).thenReturn(callbackURI);
when(oidcAgentConfig.getAuthorizeEndpoint()).thenReturn(authorizationEndpoint);
when(oidcAgentConfig.getLogoutEndpoint()).thenReturn(logoutEP);
when(oidcAgentConfig.getPostLogoutRedirectURI()).thenReturn(redirectionURI);
when(sessionContext.getIdToken()).thenReturn(idToken.getParsedString());
}
use of io.asgardeo.java.oidc.sdk.bean.SessionContext in project asgardeo-java-oidc-sdk by asgardeo.
the class DefaultOIDCManagerTest method setUp.
@BeforeMethod
public void setUp() throws Exception {
mockServer = ClientAndServer.startClientAndServer(9441);
Issuer issuer = new Issuer("issuer");
ClientID clientID = new ClientID("sampleClientId");
Secret clientSecret = new Secret("sampleClientSecret");
URI callbackURI = new URI("http://localhost:9441/sampleCallbackURL");
URI tokenEPURI = new URI("http://localhost:9441/sampleTokenEP");
URI jwksURI = new URI("http://localhost:9441/jwksEP");
URI logoutEP = new URI("http://test/sampleLogoutEP");
Scope scope = new Scope("sampleScope1", "openid");
JWT idToken = JWTParser.parse("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwia" + "WF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c");
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
requestResolver = mock(OIDCRequestResolver.class);
sessionContext = mock(SessionContext.class);
oidcAgentConfig.setConsumerKey(clientID);
oidcAgentConfig.setConsumerSecret(clientSecret);
oidcAgentConfig.setCallbackUrl(callbackURI);
oidcAgentConfig.setTokenEndpoint(tokenEPURI);
oidcAgentConfig.setLogoutEndpoint(logoutEP);
oidcAgentConfig.setScope(scope);
oidcAgentConfig.setIssuer(issuer);
oidcAgentConfig.setJwksEndpoint(jwksURI);
when(sessionContext.getIdToken()).thenReturn(idToken.getParsedString());
IDTokenClaimsSet claimsSet = mock(IDTokenClaimsSet.class);
IDTokenValidator idTokenValidator = mock(IDTokenValidator.class);
com.nimbusds.openid.connect.sdk.validators.IDTokenValidator validator = mock(com.nimbusds.openid.connect.sdk.validators.IDTokenValidator.class);
PowerMockito.whenNew(IDTokenValidator.class).withAnyArguments().thenReturn(idTokenValidator);
PowerMockito.whenNew(com.nimbusds.openid.connect.sdk.validators.IDTokenValidator.class).withAnyArguments().thenReturn(validator);
when(validator.validate(any(JWT.class), any(Nonce.class))).thenReturn(claimsSet);
Mockito.when(idTokenValidator.validate(any(Nonce.class))).thenReturn(claimsSet);
Mockito.when(claimsSet.getSubject()).thenReturn(new Subject("alex@carbon.super"));
}
use of io.asgardeo.java.oidc.sdk.bean.SessionContext in project asgardeo-java-oidc-sdk by asgardeo.
the class HTTPSessionBasedOIDCProcessorTest method testHandleOIDCCallback.
@Test
public void testHandleOIDCCallback() throws SSOAgentException {
SessionContext sessionContext = new SessionContext();
RequestContext requestContext = new RequestContext();
HttpSession session = mock(HttpSession.class);
mockedOIDCManagerFactory = mockStatic(DefaultOIDCManagerFactory.class);
when(DefaultOIDCManagerFactory.createOIDCManager(oidcAgentConfig)).thenReturn(defaultOIDCManager);
when(request.getSession()).thenReturn(session);
when(request.getSession(false)).thenReturn(session);
when(session.getAttribute(SSOAgentConstants.REQUEST_CONTEXT)).thenReturn(requestContext);
when(defaultOIDCManager.handleOIDCCallback(request, response, requestContext)).thenReturn(sessionContext);
HTTPSessionBasedOIDCProcessor provider = new HTTPSessionBasedOIDCProcessor(oidcAgentConfig);
provider.handleOIDCCallback(request, response);
verify(session).setAttribute(SSOAgentConstants.SESSION_CONTEXT, sessionContext);
}
use of io.asgardeo.java.oidc.sdk.bean.SessionContext in project asgardeo-java-oidc-sdk by asgardeo.
the class HTTPSessionBasedOIDCProcessorTest method testLogout.
@Test
public void testLogout() throws SSOAgentException {
RequestContext requestContext = new RequestContext();
SessionContext sessionContext = new SessionContext();
HttpSession session = mock(HttpSession.class);
mockedOIDCManagerFactory = mockStatic(DefaultOIDCManagerFactory.class);
when(DefaultOIDCManagerFactory.createOIDCManager(oidcAgentConfig)).thenReturn(defaultOIDCManager);
when(request.getSession()).thenReturn(session);
when(request.getSession(false)).thenReturn(session);
when(session.getAttribute(SSOAgentConstants.SESSION_CONTEXT)).thenReturn(sessionContext);
when(defaultOIDCManager.logout(sessionContext, response)).thenReturn(requestContext);
HTTPSessionBasedOIDCProcessor provider = new HTTPSessionBasedOIDCProcessor(oidcAgentConfig);
provider.logout(request, response);
verify(session).setAttribute(SSOAgentConstants.REQUEST_CONTEXT, requestContext);
}
use of io.asgardeo.java.oidc.sdk.bean.SessionContext in project asgardeo-java-oidc-sdk by asgardeo.
the class DefaultOIDCManager method handleOIDCCallback.
/**
* {@inheritDoc}
*/
@Override
public SessionContext handleOIDCCallback(HttpServletRequest request, HttpServletResponse response, RequestContext requestContext) throws SSOAgentException {
OIDCRequestResolver requestResolver = new OIDCRequestResolver(request, oidcAgentConfig);
SessionContext sessionContext = new SessionContext();
Nonce nonce = requestContext.getNonce();
try {
if (requestResolver.isAuthorizationCodeResponse()) {
// Auth code is received.
logger.log(Level.TRACE, "Handling the OIDC Authorization response.");
boolean isAuthenticated = handleAuthentication(request, sessionContext, nonce);
if (isAuthenticated) {
logger.log(Level.TRACE, "Authentication successful. Redirecting to the target page.");
return sessionContext;
}
} else if (requestResolver.isError()) {
// Error occurred.
if (StringUtils.isNotEmpty(request.getParameter(SSOAgentConstants.ERROR_DESCRIPTION))) {
logger.log(Level.ERROR, "Authentication unsuccessful. Error description: " + request.getParameter(SSOAgentConstants.ERROR_DESCRIPTION));
throw new SSOAgentServerException(request.getParameter(SSOAgentConstants.ERROR_DESCRIPTION), SSOAgentConstants.ErrorMessages.AUTHENTICATION_FAILED.getCode());
}
} else {
// Successful logout.
sessionContext.getAdditionalParams().put(SSOAgentConstants.IS_LOGOUT, true);
return sessionContext;
}
logger.log(Level.ERROR, "Authentication unsuccessful. Clearing the active session and redirecting.");
throw new SSOAgentServerException(SSOAgentConstants.ErrorMessages.AUTHENTICATION_FAILED.getMessage(), SSOAgentConstants.ErrorMessages.AUTHENTICATION_FAILED.getCode());
} catch (SSOAgentServerException e) {
throw new SSOAgentException(e.getMessage(), e.getErrorCode());
}
}
Aggregations