use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project product-apim by wso2.
the class TestUtil method generateToken.
private static void generateToken(String username, String password, String scopes) throws APIManagementException {
if (StringUtils.isEmpty(clientId) | StringUtils.isEmpty(clientSecret)) {
generateClient();
}
OAuth2ServiceStubs.TokenServiceStub tokenServiceStub = getOauth2Client();
Response response = tokenServiceStub.generatePasswordGrantAccessToken(username, password, scopes, -1, clientId, clientSecret);
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
// 200 - Success
logger.debug("A new access token is successfully generated.");
try {
OAuth2TokenInfo oAuth2TokenInfo = (OAuth2TokenInfo) new GsonDecoder().decode(response, OAuth2TokenInfo.class);
accessTokenInfo = new TokenInfo(oAuth2TokenInfo.getAccessToken(), System.currentTimeMillis() + oAuth2TokenInfo.getExpiresIn());
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing token response", e, ExceptionCodes.ACCESS_TOKEN_GENERATION_FAILED);
}
}
}
use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.
the class WSO2ISKeyManagerImpl method revokeAccessToken.
// TODO: Remove after revoke endpoint implementation done in key manager.
@Override
public void revokeAccessToken(String accessToken, String clientId, String clientSecret) throws KeyManagementException {
log.debug("Revoking access token");
Response response;
try {
response = oAuth2ServiceStubs.getRevokeServiceStub().revokeAccessToken(accessToken, clientId, clientSecret);
} catch (APIManagementException e) {
throw new KeyManagementException("Error occurred while revoking current access token", e, ExceptionCodes.ACCESS_TOKEN_REVOKE_FAILED);
}
if (response == null) {
throw new KeyManagementException("Error occurred while revoking current access token. " + "Response is null", ExceptionCodes.ACCESS_TOKEN_REVOKE_FAILED);
}
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
if (log.isDebugEnabled()) {
log.debug("Successfully revoked access token: " + accessToken);
}
} else {
throw new KeyManagementException("Token revocation failed. HTTP error code: " + response.status() + " Error Response Body: " + response.body().toString(), ExceptionCodes.ACCESS_TOKEN_REVOKE_FAILED);
}
}
use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.
the class DefaultScopeRegistrationImpl method updateScope.
@Override
public boolean updateScope(Scope scope) throws KeyManagementException {
ScopeInfo scopeInfo = getScopeInfo(scope);
Response response = scopeRegistrationServiceStub.updateScope(scopeInfo, scope.getName());
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
return true;
} else {
throw new KeyManagementException("Scope update failed", ExceptionCodes.INTERNAL_ERROR);
}
}
use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.
the class DefaultKeyManagerImpl method getTokenMetaData.
@Override
public AccessTokenInfo getTokenMetaData(String accessToken) throws KeyManagementException {
log.debug("Token introspection request is being sent.");
Response response;
try {
response = oAuth2ServiceStubs.getIntrospectionServiceStub().introspectToken(accessToken);
} catch (APIManagementException e) {
throw new KeyManagementException("Error occurred while introspecting access token.", e, ExceptionCodes.TOKEN_INTROSPECTION_FAILED);
}
if (response == null) {
throw new KeyManagementException("Error occurred while introspecting access token. " + "Response is null", ExceptionCodes.TOKEN_INTROSPECTION_FAILED);
}
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
log.debug("Token introspection is successful");
try {
OAuth2IntrospectionResponse introspectResponse = (OAuth2IntrospectionResponse) new GsonDecoder().decode(response, OAuth2IntrospectionResponse.class);
AccessTokenInfo tokenInfo = new AccessTokenInfo();
boolean active = introspectResponse.isActive();
if (active) {
tokenInfo.setTokenValid(true);
tokenInfo.setAccessToken(accessToken);
tokenInfo.setScopes(introspectResponse.getScope());
tokenInfo.setConsumerKey(introspectResponse.getClientId());
tokenInfo.setIssuedTime(introspectResponse.getIat());
tokenInfo.setExpiryTime(introspectResponse.getExp());
if (StringUtils.isNotEmpty(introspectResponse.getUsername())) {
tokenInfo.setEndUserName(introspectResponse.getUsername());
}
long validityPeriod = introspectResponse.getExp() - introspectResponse.getIat();
tokenInfo.setValidityPeriod(validityPeriod);
} else {
tokenInfo.setTokenValid(false);
log.error("Invalid or expired access token received.");
tokenInfo.setErrorCode(KeyManagerConstants.KeyValidationStatus.API_AUTH_INVALID_CREDENTIALS);
}
return tokenInfo;
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing token introspection response", e, ExceptionCodes.TOKEN_INTROSPECTION_FAILED);
}
} else {
throw new KeyManagementException("Token introspection request failed. HTTP error code: " + response.status() + " Error Response Body: " + response.body().toString(), ExceptionCodes.TOKEN_INTROSPECTION_FAILED);
}
}
use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.
the class DefaultKeyManagerImpl method updateApplication.
@Override
public OAuthApplicationInfo updateApplication(OAuthApplicationInfo oAuthApplicationInfo) throws KeyManagementException {
if (log.isDebugEnabled()) {
log.debug("Updating OAuth2 application with : " + oAuthApplicationInfo.toString());
}
String applicationName = oAuthApplicationInfo.getClientName();
String keyType = (String) oAuthApplicationInfo.getParameter(KeyManagerConstants.APP_KEY_TYPE);
if (keyType != null) {
// Derive oauth2 app name based on key type and user input for app name
applicationName = applicationName + '_' + keyType;
}
DCRClientInfo dcrClientInfo = new DCRClientInfo();
dcrClientInfo.setClientName(applicationName);
dcrClientInfo.setClientId(oAuthApplicationInfo.getClientId());
dcrClientInfo.setClientSecret(oAuthApplicationInfo.getClientSecret());
dcrClientInfo.addCallbackUrl(oAuthApplicationInfo.getCallBackURL());
dcrClientInfo.setGrantTypes(oAuthApplicationInfo.getGrantTypes());
Response response = dcrmServiceStub.updateApplication(dcrClientInfo, dcrClientInfo.getClientId());
if (response == null) {
throw new KeyManagementException("Error occurred while updating DCR application. Response is null", ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
// 200 - Success
try {
OAuthApplicationInfo oAuthApplicationInfoResponse = getOAuthApplicationInfo(response);
// setting original parameter list
oAuthApplicationInfoResponse.setParameters(oAuthApplicationInfo.getParameters());
if (log.isDebugEnabled()) {
log.debug("OAuth2 application updated: " + oAuthApplicationInfoResponse.toString());
}
return oAuthApplicationInfoResponse;
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR application update response " + "message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
} else if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_400_BAD_REQUEST) {
// 400 - Known Error
try {
DCRError error = (DCRError) new GsonDecoder().decode(response, DCRError.class);
throw new KeyManagementException("Error occurred while updating DCR application. Error: " + error.getError() + ". Error Description: " + error.getErrorDescription() + ". Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR error message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
} else {
// Unknown Error
throw new KeyManagementException("Error occurred while updating DCR application. Error: " + response.body().toString() + " Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
}
Aggregations