use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuth2Util method getAccessTokenDOFromMatchingTokenIssuer.
/**
* Loop through provided token issuer list and tries to get the access token DO.
*
* @param tokenIdentifier Provided token identifier.
* @param tokenIssuerMap List of token issuers.
* @return Obtained matching access token DO if possible.
* @throws IdentityOAuth2Exception
*/
private static AccessTokenDO getAccessTokenDOFromMatchingTokenIssuer(String tokenIdentifier, Map<String, OauthTokenIssuer> tokenIssuerMap, boolean includeExpired) throws IdentityOAuth2Exception {
AccessTokenDO accessTokenDO;
if (tokenIssuerMap != null) {
for (Map.Entry<String, OauthTokenIssuer> oauthTokenIssuerEntry : tokenIssuerMap.entrySet()) {
try {
OauthTokenIssuer oauthTokenIssuer = oauthTokenIssuerEntry.getValue();
String tokenAlias = oauthTokenIssuer.getAccessTokenHash(tokenIdentifier);
if (oauthTokenIssuer.usePersistedAccessTokenAlias()) {
accessTokenDO = OAuth2Util.getAccessTokenDOFromTokenIdentifier(tokenAlias, includeExpired);
} else {
accessTokenDO = OAuth2Util.getAccessTokenDOFromTokenIdentifier(tokenIdentifier, includeExpired);
}
if (accessTokenDO != null) {
return accessTokenDO;
}
} catch (OAuthSystemException e) {
if (log.isDebugEnabled()) {
if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
log.debug("Token issuer: " + oauthTokenIssuerEntry.getKey() + " was tried and" + " failed to parse the received token: " + tokenIdentifier);
} else {
log.debug("Token issuer: " + oauthTokenIssuerEntry.getKey() + " was tried and" + " failed to parse the received token.");
}
}
} catch (IllegalArgumentException e) {
if (log.isDebugEnabled()) {
if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
log.debug("Token issuer: " + oauthTokenIssuerEntry.getKey() + " was tried and" + " failed to get the token from database: " + tokenIdentifier);
} else {
log.debug("Token issuer: " + oauthTokenIssuerEntry.getKey() + " was tried and" + " failed to get the token from database.");
}
}
}
}
}
return null;
}
use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project identity-inbound-auth-oauth by wso2-extensions.
the class RefreshGrantHandler method createTokens.
private void createTokens(AccessTokenDO accessTokenDO, OAuthTokenReqMessageContext tokReqMsgCtx) throws IdentityOAuth2Exception {
try {
OauthTokenIssuer oauthTokenIssuer = OAuth2Util.getOAuthTokenIssuerForOAuthApp(accessTokenDO.getConsumerKey());
String accessToken = oauthTokenIssuer.accessToken(tokReqMsgCtx);
String refreshToken = oauthTokenIssuer.refreshToken(tokReqMsgCtx);
if (log.isDebugEnabled()) {
if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
log.debug("New access token (hashed): " + DigestUtils.sha256Hex(accessToken) + " & new refresh token (hashed): " + DigestUtils.sha256Hex(refreshToken));
} else {
log.debug("Access token and refresh token generated.");
}
}
accessTokenDO.setAccessToken(accessToken);
accessTokenDO.setRefreshToken(refreshToken);
} catch (OAuthSystemException e) {
throw new IdentityOAuth2Exception("Error when generating the tokens.", e);
} catch (InvalidOAuthClientException e) {
throw new IdentityOAuth2Exception("Error while retrieving oauth issuer for the app with clientId: " + accessTokenDO.getConsumerKey(), e);
}
}
use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project Kustvakt by KorAP.
the class OAuth2ResponseHandler method throwit.
public WebApplicationException throwit(OAuthProblemException e, String state) {
OAuthResponse oAuthResponse = null;
try {
OAuthErrorResponseBuilder builder = OAuthResponse.errorResponse(e.getResponseStatus()).error(e);
if (state != null && !state.isEmpty()) {
builder.setState(state);
}
oAuthResponse = builder.buildJSONMessage();
} catch (OAuthSystemException e1) {
throwit(e1, state);
}
Response r = createResponse(oAuthResponse);
return new WebApplicationException(r);
}
use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project Kustvakt by KorAP.
the class OAuth2Controller method requestAuthorizationCode.
/**
* Requests an authorization code.
*
* Kustvakt supports authorization only with Kalamar as the
* authorization web-frontend or user interface. Thus
* authorization code request requires user authentication
* using authorization header.
*
* <br /><br />
* RFC 6749:
* If the client omits the scope parameter when requesting
* authorization, the authorization server MUST either process the
* request using a pre-defined default value or fail the request
* indicating an invalid scope.
*
* @param request
* HttpServletRequest
* @param form
* form parameters
* @return a redirect URL
*/
@POST
@Path("authorize")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response requestAuthorizationCode(@Context HttpServletRequest request, @Context SecurityContext context, @FormParam("state") String state, MultivaluedMap<String, String> form) {
TokenContext tokenContext = (TokenContext) context.getUserPrincipal();
String username = tokenContext.getUsername();
ZonedDateTime authTime = tokenContext.getAuthenticationTime();
try {
scopeService.verifyScope(tokenContext, OAuth2Scope.AUTHORIZE);
HttpServletRequest requestWithForm = new FormRequestWrapper(request, form);
OAuth2AuthorizationRequest authzRequest = new OAuth2AuthorizationRequest(requestWithForm);
String uri = authorizationService.requestAuthorizationCode(requestWithForm, authzRequest, username, authTime);
return responseHandler.sendRedirect(uri);
} catch (OAuthSystemException e) {
throw responseHandler.throwit(e, state);
} catch (OAuthProblemException e) {
throw responseHandler.throwit(e, state);
} catch (KustvaktException e) {
throw responseHandler.throwit(e, state);
}
}
use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project Kustvakt by KorAP.
the class OAuth2Controller method revokeAllClientTokensViaSuperClient.
/**
* Revokes all tokens of a client for the authenticated user from
* a super client. This service is not part of the OAUTH2
* specification. It requires user authentication via
* authorization header, and super client
* via URL-encoded form parameters.
*
* @param request
* @param form
* containing client_id, super_client_id,
* super_client_secret
* @return 200 if token invalidation is successful or the given
* token is invalid
*/
@POST
@Path("revoke/super/all")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response revokeAllClientTokensViaSuperClient(@Context SecurityContext context, @Context HttpServletRequest request, MultivaluedMap<String, String> form) {
TokenContext tokenContext = (TokenContext) context.getUserPrincipal();
String username = tokenContext.getUsername();
try {
OAuth2RevokeAllTokenSuperRequest revokeTokenRequest = new OAuth2RevokeAllTokenSuperRequest(new FormRequestWrapper(request, form));
tokenService.revokeAllClientTokensViaSuperClient(username, revokeTokenRequest);
return Response.ok("SUCCESS").build();
} catch (OAuthSystemException e) {
throw responseHandler.throwit(e);
} catch (OAuthProblemException e) {
throw responseHandler.throwit(e);
} catch (KustvaktException e) {
throw responseHandler.throwit(e);
}
}
Aggregations