use of io.asgardeo.java.oidc.sdk.exception.SSOAgentServerException 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.exception.SSOAgentServerException in project asgardeo-java-oidc-sdk by asgardeo.
the class IDTokenValidator method validate.
public IDTokenClaimsSet validate(Nonce expectedNonce) throws SSOAgentServerException {
JWSAlgorithm jwsAlgorithm = validateJWSAlgorithm(idToken);
com.nimbusds.openid.connect.sdk.validators.IDTokenValidator validator = getIDTokenValidator(jwsAlgorithm);
IDTokenClaimsSet claims;
try {
claims = validator.validate(idToken, expectedNonce);
validateAudience(claims);
} catch (JOSEException | BadJOSEException e) {
throw new SSOAgentServerException(e.getMessage(), e.getCause());
}
return claims;
}
use of io.asgardeo.java.oidc.sdk.exception.SSOAgentServerException in project asgardeo-java-oidc-sdk by asgardeo.
the class IDTokenValidator method validateJWSAlgorithm.
private JWSAlgorithm validateJWSAlgorithm(JWT idToken) throws SSOAgentServerException {
JWSAlgorithm jwsAlgorithm = (JWSAlgorithm) idToken.getHeader().getAlgorithm();
JWSAlgorithm expectedJWSAlgorithm = oidcAgentConfig.getSignatureAlgorithm();
if (expectedJWSAlgorithm == null) {
if (JWSAlgorithm.RS256.equals(jwsAlgorithm)) {
return jwsAlgorithm;
} else {
throw new SSOAgentServerException(String.format("Signed JWT rejected. Provided signature algorithm: " + "%s is not the default of RS256.", jwsAlgorithm.getName()));
}
} else if (!expectedJWSAlgorithm.equals(jwsAlgorithm)) {
throw new SSOAgentServerException(String.format("Signed JWT rejected: Another algorithm expected. " + "Provided signature algorithm: %s.", jwsAlgorithm.getName()));
}
return jwsAlgorithm;
}
use of io.asgardeo.java.oidc.sdk.exception.SSOAgentServerException 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());
}
}
use of io.asgardeo.java.oidc.sdk.exception.SSOAgentServerException in project asgardeo-java-oidc-sdk by asgardeo.
the class DefaultOIDCManager method handleAuthentication.
private boolean handleAuthentication(final HttpServletRequest request, SessionContext authenticationInfo, Nonce nonce) throws SSOAgentServerException {
AuthorizationResponse authorizationResponse;
AuthorizationCode authorizationCode;
AuthorizationSuccessResponse successResponse;
TokenRequest tokenRequest;
TokenResponse tokenResponse;
try {
authorizationResponse = AuthorizationResponse.parse(ServletUtils.createHTTPRequest(request));
if (!authorizationResponse.indicatesSuccess()) {
handleErrorAuthorizationResponse(authorizationResponse);
return false;
}
successResponse = authorizationResponse.toSuccessResponse();
authorizationCode = successResponse.getAuthorizationCode();
tokenRequest = getTokenRequest(authorizationCode);
tokenResponse = getTokenResponse(tokenRequest);
if (!tokenResponse.indicatesSuccess()) {
handleErrorTokenResponse(tokenRequest, tokenResponse);
return false;
}
handleSuccessTokenResponse(tokenResponse, authenticationInfo, nonce);
return true;
} catch (com.nimbusds.oauth2.sdk.ParseException | SSOAgentServerException | IOException e) {
throw new SSOAgentServerException(e.getMessage(), e);
}
}
Aggregations