use of io.asgardeo.java.oidc.sdk.validators.IDTokenValidator in project product-mi-tooling by wso2.
the class JWTSecurityHandler method isAuthenticated.
@Override
public boolean isAuthenticated(SSOConfig config, String token) {
JWT idTokenJWT = null;
try {
idTokenJWT = JWTParser.parse(token);
if (config.getOidcAgentConfig().getJwksEndpoint() == null) {
config.getOidcAgentConfig().setJwksEndpoint(getJWKSEndpointFromWellKnownEndpoint(config.getWellKnownEndpoint()));
}
IDTokenValidator validator = new IDTokenValidator(config.getOidcAgentConfig(), idTokenJWT);
validator.validate(null);
return true;
} catch (DashboardServerException | ParseException | SSOAgentServerException e) {
if (logger.isDebugEnabled()) {
logger.error("Error validating the access token", e);
}
}
return false;
}
use of io.asgardeo.java.oidc.sdk.validators.IDTokenValidator 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.validators.IDTokenValidator in project asgardeo-java-oidc-sdk by asgardeo.
the class DefaultOIDCManager method handleSuccessTokenResponse.
private void handleSuccessTokenResponse(TokenResponse tokenResponse, SessionContext sessionContext, Nonce nonce) throws SSOAgentServerException {
AccessTokenResponse successResponse = tokenResponse.toSuccessResponse();
AccessToken accessToken = successResponse.getTokens().getAccessToken();
RefreshToken refreshToken = successResponse.getTokens().getRefreshToken();
String idToken;
try {
idToken = successResponse.getCustomParameters().get(SSOAgentConstants.ID_TOKEN).toString();
} catch (NullPointerException e) {
logger.log(Level.ERROR, "id_token is null.");
throw new SSOAgentServerException(SSOAgentConstants.ErrorMessages.ID_TOKEN_NULL.getMessage(), SSOAgentConstants.ErrorMessages.ID_TOKEN_NULL.getCode(), e);
}
try {
JWT idTokenJWT = JWTParser.parse(idToken);
IDTokenValidator idTokenValidator = new IDTokenValidator(oidcAgentConfig, idTokenJWT);
IDTokenClaimsSet claimsSet = idTokenValidator.validate(nonce);
User user = new User(claimsSet.getSubject().getValue(), getUserAttributes(idToken));
sessionContext.setIdToken(idTokenJWT.getParsedString());
sessionContext.setUser(user);
sessionContext.setAccessToken(accessToken.toJSONString());
if (refreshToken != null) {
sessionContext.setRefreshToken(refreshToken.getValue());
}
} catch (ParseException e) {
throw new SSOAgentServerException(SSOAgentConstants.ErrorMessages.ID_TOKEN_PARSE.getMessage(), SSOAgentConstants.ErrorMessages.ID_TOKEN_PARSE.getCode(), e);
}
}
Aggregations