Search in sources :

Example 1 with KeyManagerClientException

use of org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImplTest method testCreateApplicationAppNameWithSpecialChars.

@Test
public void testCreateApplicationAppNameWithSpecialChars() throws APIManagementException, KeyManagerClientException {
    String applicationName = "ÅÄÖÅÄÖ";
    System.setProperty("carbon.home", "jhkjn");
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    OAuthAppRequest oauthRequest = new OAuthAppRequest();
    OAuthApplicationInfo oauthApplication = new OAuthApplicationInfo();
    oauthApplication.setAppOwner(APP_OWNER);
    oauthApplication.setCallBackURL(StringUtils.join(REDIRECT_URIS, ","));
    oauthApplication.setClientName(applicationName);
    oauthApplication.addParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME, APP_OWNER);
    oauthApplication.addParameter(ApplicationConstants.APP_KEY_TYPE, KEY_TYPE);
    oauthApplication.setJsonString(getJSONString());
    oauthRequest.setMappingId("123");
    oauthRequest.setOAuthApplicationInfo(oauthApplication);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    ClientInfo response = new ClientInfo();
    response.setClientId(CLIENT_ID);
    response.setClientName(APP_UUID);
    response.setClientSecret(CLIENT_SECRET);
    response.setRedirectUris(Arrays.asList(REDIRECT_URIS));
    response.setGrantTypes(Arrays.asList(GRANT_TYPES));
    Mockito.when(dcrClient.createApplication(Mockito.any(ClientInfo.class))).thenReturn(response);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    Mockito.when(APIUtil.getApplicationUUID(Mockito.anyString(), Mockito.anyString())).thenReturn(APP_UUID);
    OAuthApplicationInfo oauthApplicationResponse = keyManager.createApplication(oauthRequest);
    Assert.assertEquals(StringUtils.join(REDIRECT_URIS, ","), oauthApplicationResponse.getCallBackURL());
    Assert.assertEquals(APP_UUID, oauthApplicationResponse.getClientName());
}
Also used : OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) ClientInfo(org.wso2.carbon.apimgt.impl.kmclient.model.ClientInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with KeyManagerClientException

use of org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImpl method getTokenMetaData.

@Override
public AccessTokenInfo getTokenMetaData(String accessToken) throws APIManagementException {
    AccessTokenInfo tokenInfo = new AccessTokenInfo();
    try {
        IntrospectInfo introspectInfo = introspectionClient.introspect(accessToken);
        tokenInfo.setAccessToken(accessToken);
        boolean isActive = introspectInfo.isActive();
        if (!isActive) {
            tokenInfo.setTokenValid(false);
            tokenInfo.setErrorcode(APIConstants.KeyValidationStatus.API_AUTH_INVALID_CREDENTIALS);
            return tokenInfo;
        }
        tokenInfo.setTokenValid(true);
        if (introspectInfo.getIat() > 0 && introspectInfo.getExpiry() > 0) {
            if (introspectInfo.getExpiry() != Long.MAX_VALUE) {
                long validityPeriod = introspectInfo.getExpiry() - introspectInfo.getIat();
                tokenInfo.setValidityPeriod(validityPeriod * 1000L);
            } else {
                tokenInfo.setValidityPeriod(Long.MAX_VALUE);
            }
            tokenInfo.setIssuedTime(introspectInfo.getIat() * 1000L);
        }
        if (StringUtils.isNotEmpty(introspectInfo.getScope())) {
            String[] scopes = introspectInfo.getScope().split(" ");
            tokenInfo.setScope(scopes);
        }
        tokenInfo.setConsumerKey(introspectInfo.getClientId());
        String username = introspectInfo.getUsername();
        if (!StringUtils.isEmpty(username)) {
            tokenInfo.setEndUserName(username);
        }
        return tokenInfo;
    } catch (KeyManagerClientException e) {
        throw new APIManagementException("Error occurred in token introspection!", e);
    }
}
Also used : AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) IntrospectInfo(org.wso2.carbon.apimgt.impl.kmclient.model.IntrospectInfo)

Example 3 with KeyManagerClientException

use of org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImpl method getUserClaims.

@Override
public Map<String, String> getUserClaims(String username, Map<String, Object> properties) throws APIManagementException {
    Map<String, String> map = new HashMap<String, String>();
    String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(username);
    UserInfoDTO userinfo = new UserInfoDTO();
    userinfo.setUsername(tenantAwareUserName);
    if (tenantAwareUserName.contains(CarbonConstants.DOMAIN_SEPARATOR)) {
        userinfo.setDomain(tenantAwareUserName.split(CarbonConstants.DOMAIN_SEPARATOR)[0]);
    }
    if (properties.containsKey(APIConstants.KeyManager.ACCESS_TOKEN)) {
        userinfo.setAccessToken(properties.get(APIConstants.KeyManager.ACCESS_TOKEN).toString());
    }
    if (properties.containsKey(APIConstants.KeyManager.CLAIM_DIALECT)) {
        userinfo.setDialectURI(properties.get(APIConstants.KeyManager.CLAIM_DIALECT).toString());
    }
    if (properties.containsKey(APIConstants.KeyManager.BINDING_FEDERATED_USER_CLAIMS)) {
        userinfo.setBindFederatedUserClaims(Boolean.valueOf(properties.get(APIConstants.KeyManager.BINDING_FEDERATED_USER_CLAIMS).toString()));
    }
    try {
        ClaimsList claims = userClient.generateClaims(userinfo);
        if (claims != null && claims.getList() != null) {
            for (Claim claim : claims.getList()) {
                map.put(claim.getUri(), claim.getValue());
            }
        }
    } catch (KeyManagerClientException e) {
        handleException("Error while getting user info", e);
    }
    return map;
}
Also used : KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) HashMap(java.util.HashMap) ClaimsList(org.wso2.carbon.apimgt.impl.kmclient.model.ClaimsList) UserInfoDTO(org.wso2.carbon.apimgt.impl.dto.UserInfoDTO) Claim(org.wso2.carbon.apimgt.impl.kmclient.model.Claim)

Example 4 with KeyManagerClientException

use of org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImpl method getNewApplicationAccessToken.

@Override
public AccessTokenInfo getNewApplicationAccessToken(AccessTokenRequest tokenRequest) throws APIManagementException {
    AccessTokenInfo tokenInfo;
    if (tokenRequest == null) {
        log.warn("No information available to generate Token.");
        return null;
    }
    // When validity time set to a negative value, a token is considered never to expire.
    if (tokenRequest.getValidityPeriod() == OAuthConstants.UNASSIGNED_VALIDITY_PERIOD) {
        // Setting a different -ve value if the set value is -1 (-1 will be ignored by TokenValidator)
        tokenRequest.setValidityPeriod(-2L);
    }
    // Generate New Access Token
    String scopes = String.join(" ", tokenRequest.getScope());
    TokenInfo tokenResponse;
    try {
        String credentials = tokenRequest.getClientId() + ':' + tokenRequest.getClientSecret();
        String authToken = Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8));
        if (APIConstants.OAuthConstants.TOKEN_EXCHANGE.equals(tokenRequest.getGrantType())) {
            tokenResponse = authClient.generate(tokenRequest.getClientId(), tokenRequest.getClientSecret(), tokenRequest.getGrantType(), scopes, (String) tokenRequest.getRequestParam(APIConstants.OAuthConstants.SUBJECT_TOKEN), APIConstants.OAuthConstants.JWT_TOKEN_TYPE);
        } else {
            tokenResponse = authClient.generate(authToken, GRANT_TYPE_VALUE, scopes);
        }
    } catch (KeyManagerClientException e) {
        throw new APIManagementException("Error occurred while calling token endpoint - " + e.getReason(), e);
    }
    tokenInfo = new AccessTokenInfo();
    if (StringUtils.isNotEmpty(tokenResponse.getScope())) {
        tokenInfo.setScope(tokenResponse.getScope().split(" "));
    } else {
        tokenInfo.setScope(new String[0]);
    }
    tokenInfo.setAccessToken(tokenResponse.getToken());
    tokenInfo.setValidityPeriod(tokenResponse.getExpiry());
    return tokenInfo;
}
Also used : AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TokenInfo(org.wso2.carbon.apimgt.impl.kmclient.model.TokenInfo) AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo)

Example 5 with KeyManagerClientException

use of org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImpl method updateScope.

/**
 * This method will be used to update a Scope in the authorization server.
 *
 * @param scope Scope object
 * @throws APIManagementException if an error occurs while updating the scope
 */
@Override
public void updateScope(Scope scope) throws APIManagementException {
    String scopeKey = scope.getKey();
    try {
        ScopeDTO scopeDTO = new ScopeDTO();
        scopeDTO.setDisplayName(scope.getName());
        scopeDTO.setDescription(scope.getDescription());
        if (StringUtils.isNotBlank(scope.getRoles()) && scope.getRoles().trim().split(",").length > 0) {
            scopeDTO.setBindings(Arrays.asList(scope.getRoles().trim().split(",")));
        }
        scopeClient.updateScope(scopeDTO, scope.getKey());
    } catch (KeyManagerClientException e) {
        String errorMessage = "Error occurred while updating scope: " + scopeKey;
        handleException(errorMessage, e);
    }
}
Also used : KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) ScopeDTO(org.wso2.carbon.apimgt.impl.dto.ScopeDTO)

Aggregations

KeyManagerClientException (org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)6 ClientInfo (org.wso2.carbon.apimgt.impl.kmclient.model.ClientInfo)6 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 AccessTokenInfo (org.wso2.carbon.apimgt.api.model.AccessTokenInfo)3 Response (feign.Response)2 OAuthAppRequest (org.wso2.carbon.apimgt.api.model.OAuthAppRequest)2 ScopeDTO (org.wso2.carbon.apimgt.impl.dto.ScopeDTO)2 IntrospectInfo (org.wso2.carbon.apimgt.impl.kmclient.model.IntrospectInfo)2 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 UserInfoDTO (org.wso2.carbon.apimgt.impl.dto.UserInfoDTO)1 Claim (org.wso2.carbon.apimgt.impl.kmclient.model.Claim)1 ClaimsList (org.wso2.carbon.apimgt.impl.kmclient.model.ClaimsList)1 TokenInfo (org.wso2.carbon.apimgt.impl.kmclient.model.TokenInfo)1