Search in sources :

Example 1 with KeyManagementException

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);
        }
    }
}
Also used : Response(feign.Response) GsonDecoder(feign.gson.GsonDecoder) OAuth2TokenInfo(org.wso2.carbon.apimgt.core.auth.dto.OAuth2TokenInfo) IOException(java.io.IOException) OAuth2ServiceStubs(org.wso2.carbon.apimgt.core.auth.OAuth2ServiceStubs) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException) OAuth2TokenInfo(org.wso2.carbon.apimgt.core.auth.dto.OAuth2TokenInfo)

Example 2 with KeyManagementException

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);
    }
}
Also used : Response(feign.Response) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException)

Example 3 with KeyManagementException

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);
    }
}
Also used : Response(feign.Response) ScopeInfo(org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException)

Example 4 with KeyManagementException

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);
    }
}
Also used : OAuth2IntrospectionResponse(org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse) Response(feign.Response) AccessTokenInfo(org.wso2.carbon.apimgt.core.models.AccessTokenInfo) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuth2IntrospectionResponse(org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException)

Example 5 with KeyManagementException

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);
    }
}
Also used : OAuth2IntrospectionResponse(org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse) Response(feign.Response) DCRError(org.wso2.carbon.apimgt.core.auth.dto.DCRError) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) DCRClientInfo(org.wso2.carbon.apimgt.core.auth.dto.DCRClientInfo) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException)

Aggregations

KeyManagementException (org.wso2.carbon.apimgt.core.exception.KeyManagementException)25 Response (feign.Response)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)11 OAuth2IntrospectionResponse (org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse)11 Gson (com.google.gson.Gson)10 ScopeInfo (org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo)9 KeyManagementException (java.security.KeyManagementException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 OAuth2ServiceStubs (org.wso2.carbon.apimgt.core.auth.OAuth2ServiceStubs)8 DCRMServiceStub (org.wso2.carbon.apimgt.core.auth.DCRMServiceStub)7 ScopeRegistration (org.wso2.carbon.apimgt.core.auth.ScopeRegistration)7 IOException (java.io.IOException)6 KeyStoreException (java.security.KeyStoreException)6 HttpResponse (org.apache.http.HttpResponse)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 OAuthApplicationInfo (org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)6 Scope (org.wso2.carbon.apimgt.core.models.Scope)6