Search in sources :

Example 31 with IdentityProvider

use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.

the class NotificationTestCase method testNotificationExecutor.

@Test
public void testNotificationExecutor() throws Exception {
    Properties properties = Mockito.mock(Properties.class);
    NotificationDTO notificationDTO = Mockito.mock(NotificationDTO.class);
    Mockito.when(notificationDTO.getTitle()).thenReturn("Title");
    Mockito.when(notificationDTO.getType()).thenReturn("ApiNewVersion");
    Mockito.when(notificationDTO.getMessage()).thenReturn("Message");
    Mockito.when(notificationDTO.getProperties()).thenReturn(properties);
    APIMConfigurations apimConfigurations = Mockito.mock(APIMConfigurations.class);
    NotificationConfigurations notificationConfigurations = Mockito.mock(NotificationConfigurations.class);
    PowerMockito.mockStatic(APIMConfigurations.class);
    PowerMockito.when(apimConfigurations.getNotificationConfigurations()).thenReturn(notificationConfigurations);
    APIManagerFactory apiManagerFactory = Mockito.mock(APIManagerFactory.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    Set subscriber = new HashSet();
    subscriber.add("User");
    Mockito.when((Set<String>) notificationDTO.getProperty(NotifierConstants.SUBSCRIBERS_PER_API)).thenReturn(subscriber);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(apiManagerFactory);
    PowerMockito.when(apiManagerFactory.getIdentityProvider()).thenReturn(identityProvider);
    PowerMockito.when(identityProvider.getIdOfUser("User")).thenReturn("1111");
    PowerMockito.when(identityProvider.getEmailOfUser("1111")).thenReturn("admin@gmail.com");
    new NotificationExecutor().sendAsyncNotifications(notificationDTO);
}
Also used : APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) NotificationDTO(org.wso2.carbon.apimgt.core.template.dto.NotificationDTO) Set(java.util.Set) HashSet(java.util.HashSet) NotificationConfigurations(org.wso2.carbon.apimgt.core.configuration.models.NotificationConfigurations) NotificationExecutor(org.wso2.carbon.apimgt.core.executors.NotificationExecutor) APIMConfigurations(org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) Properties(java.util.Properties) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 32 with IdentityProvider

use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.

the class OAuth2AuthenticatorTestCase method testOauthAuthenticate.

@Test
public void testOauthAuthenticate() throws Exception {
    HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
    Request requestObj = new Request(carbonMessage);
    Response responseObj = Mockito.mock(Response.class);
    ServiceMethodInfo serviceMethodInfoObj = Mockito.mock(ServiceMethodInfo.class);
    final String authorizationHttpHeader = "Bearer 7d33e3cd-60f0-3484-9651-cc31f2e09fb4";
    final String accessToken = "7d33e3cd-60f0-3484-9651-cc31f2e09fb4";
    Mockito.when(requestObj.getHeader(RestApiConstants.AUTHORIZATION_HTTP_HEADER)).thenReturn(authorizationHttpHeader);
    AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
    accessTokenInfo.setTokenValid(true);
    accessTokenInfo.setEndUserName("admin@carbon.super");
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getIdentityProvider()).thenReturn(identityProvider);
    Mockito.when(identityProvider.getTokenMetaData(accessToken)).thenReturn(accessTokenInfo);
    when((String) requestObj.getProperty(APIConstants.REQUEST_URL)).thenReturn("/api/am/publisher/");
    OAuth2Authenticator oAuth2Authenticator = new OAuth2Authenticator();
    oAuth2Authenticator.authenticate(requestObj, responseObj, serviceMethodInfoObj);
    Assert.assertEquals(0, responseObj.getStatusCode());
}
Also used : Response(org.wso2.msf4j.Response) AccessTokenInfo(org.wso2.carbon.apimgt.core.models.AccessTokenInfo) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) OAuth2Authenticator(org.wso2.carbon.apimgt.rest.api.common.impl.OAuth2Authenticator) Request(org.wso2.msf4j.Request) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 33 with IdentityProvider

use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.

the class SystemScopesIssuer method configureForJWTGrant.

protected void configureForJWTGrant(OAuthTokenReqMessageContext tokReqMsgCtx) {
    SignedJWT signedJWT = null;
    JWTClaimsSet claimsSet = null;
    String[] roles = null;
    try {
        signedJWT = getSignedJWT(tokReqMsgCtx);
    } catch (IdentityOAuth2Exception e) {
        log.error("Couldn't retrieve signed JWT", e);
    }
    if (signedJWT != null) {
        claimsSet = getClaimSet(signedJWT);
    }
    String jwtIssuer = claimsSet != null ? claimsSet.getIssuer() : null;
    String tenantDomain = tokReqMsgCtx.getOauth2AccessTokenReqDTO().getTenantDomain();
    try {
        identityProvider = IdentityProviderManager.getInstance().getIdPByName(jwtIssuer, tenantDomain);
        if (identityProvider != null) {
            if (StringUtils.equalsIgnoreCase(identityProvider.getIdentityProviderName(), "default")) {
                identityProvider = this.getResidentIDPForIssuer(tenantDomain, jwtIssuer);
                if (identityProvider == null) {
                    log.error("No Registered IDP found for the JWT with issuer name : " + jwtIssuer);
                }
            }
        } else {
            log.error("No Registered IDP found for the JWT with issuer name : " + jwtIssuer);
        }
    } catch (IdentityProviderManagementException | IdentityOAuth2Exception e) {
        log.error("Couldn't initiate identity provider instance", e);
    }
    try {
        roles = claimsSet != null ? claimsSet.getStringArrayClaim(identityProvider.getClaimConfig().getRoleClaimURI()) : null;
    } catch (ParseException e) {
        log.error("Couldn't retrieve roles:", e);
    }
    List<String> updatedRoles = new ArrayList<>();
    if (roles != null) {
        for (String role : roles) {
            String updatedRoleClaimValue = getUpdatedRoleClaimValue(identityProvider, role);
            if (updatedRoleClaimValue != null) {
                updatedRoles.add(updatedRoleClaimValue);
            } else {
                updatedRoles.add(role);
            }
        }
    }
    AuthenticatedUser user = tokReqMsgCtx.getAuthorizedUser();
    Map<ClaimMapping, String> userAttributes = user.getUserAttributes();
    String roleClaim = identityProvider.getClaimConfig().getRoleClaimURI();
    if (roleClaim != null) {
        userAttributes.put(ClaimMapping.build(roleClaim, roleClaim, null, false), updatedRoles.toString().replace(" ", ""));
        tokReqMsgCtx.addProperty(APIConstants.SystemScopeConstants.ROLE_CLAIM, roleClaim);
    }
    user.setUserAttributes(userAttributes);
    tokReqMsgCtx.setAuthorizedUser(user);
}
Also used : SignedJWT(com.nimbusds.jwt.SignedJWT) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) ParseException(java.text.ParseException) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException)

Example 34 with IdentityProvider

use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.

the class APIAdminImpl method setAliasForTokenExchangeKeyManagers.

private void setAliasForTokenExchangeKeyManagers(List<KeyManagerConfigurationDTO> keyManagerConfigurationsByTenant, String tenantDomain) throws APIManagementException {
    for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurationsByTenant) {
        if (StringUtils.equals(KeyManagerConfiguration.TokenType.EXCHANGED.toString(), keyManagerConfigurationDTO.getTokenType()) || StringUtils.equals(KeyManagerConfiguration.TokenType.BOTH.toString(), keyManagerConfigurationDTO.getTokenType())) {
            if (keyManagerConfigurationDTO.getExternalReferenceId() != null) {
                IdentityProvider identityProvider;
                try {
                    identityProvider = IdentityProviderManager.getInstance().getIdPByResourceId(keyManagerConfigurationDTO.getExternalReferenceId(), tenantDomain, Boolean.FALSE);
                } catch (IdentityProviderManagementException e) {
                    throw new APIManagementException("IdP retrieval failed. " + e.getMessage(), e, ExceptionCodes.IDP_RETRIEVAL_FAILED);
                }
                // Set alias value since this will be used from the Devportal side.
                keyManagerConfigurationDTO.setAlias(identityProvider.getAlias());
            }
        }
    }
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException)

Example 35 with IdentityProvider

use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.

the class APIAdminImpl method setIdentityProviderRelatedInformation.

private void setIdentityProviderRelatedInformation(List<KeyManagerConfigurationDTO> keyManagerConfigurationsByOrganization, String organization) throws APIManagementException {
    for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurationsByOrganization) {
        if (StringUtils.equals(KeyManagerConfiguration.TokenType.EXCHANGED.toString(), keyManagerConfigurationDTO.getTokenType()) || StringUtils.equals(KeyManagerConfiguration.TokenType.BOTH.toString(), keyManagerConfigurationDTO.getTokenType())) {
            try {
                if (keyManagerConfigurationDTO.getExternalReferenceId() != null) {
                    IdentityProvider identityProvider = IdentityProviderManager.getInstance().getIdPByResourceId(keyManagerConfigurationDTO.getExternalReferenceId(), APIUtil.getTenantDomainFromTenantId(APIUtil.getInternalOrganizationId(organization)), Boolean.FALSE);
                    keyManagerConfigurationDTO.setDescription(identityProvider.getIdentityProviderDescription());
                    keyManagerConfigurationDTO.setEnabled(identityProvider.isEnable());
                }
            } catch (IdentityProviderManagementException e) {
                // handled in this way in order to not break other key managers.
                log.error("IdP retrieval failed. ", e);
            }
        }
    }
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException)

Aggregations

IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)54 Test (org.testng.annotations.Test)50 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)47 API (org.wso2.carbon.apimgt.core.models.API)43 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)35 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)34 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)20 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)19 KeyManager (org.wso2.carbon.apimgt.core.api.KeyManager)16 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)15 FileInputStream (java.io.FileInputStream)11 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)11 Scope (org.wso2.carbon.apimgt.core.models.Scope)11 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)8 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)8 DedicatedGateway (org.wso2.carbon.apimgt.core.models.DedicatedGateway)8 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)8 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)8 ArrayList (java.util.ArrayList)7 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)7