Search in sources :

Example 1 with ReceiptListResponse

use of org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse in project carbon-identity-framework by wso2.

the class ConsentDeletionUserEventHandler method handleEvent.

/**
 * Delete consents issued against a particular user when a user is deleted.
 *
 * @param event Post User Delete event.
 * @throws IdentityEventException IdentityEventException.
 */
@Override
public void handleEvent(Event event) throws IdentityEventException {
    IdentityEventMessageContext eventContext = new IdentityEventMessageContext(event);
    if (!isEnabled(eventContext)) {
        if (log.isDebugEnabled()) {
            log.debug("ConsentDeletionUserEventHandler is disabled. Not handling the " + event.getEventName() + " event.");
        }
        return;
    }
    Map<String, Object> eventProperties = event.getEventProperties();
    String userName = (String) eventProperties.get(IdentityEventConstants.EventProperty.USER_NAME);
    UserStoreManager userStoreManager = (UserStoreManager) eventProperties.get(IdentityEventConstants.EventProperty.USER_STORE_MANAGER);
    String domainName = userStoreManager.getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
    String tenantDomain = getUserTenantDomain(eventProperties);
    String usernameWithUserStoreDomain = UserCoreUtil.addDomainToName(userName, domainName);
    if (log.isDebugEnabled()) {
        log.debug(String.format("Deleting consents for user: %s , in tenant domain :%s", usernameWithUserStoreDomain, tenantDomain));
    }
    ConsentManager consentManager = IdentityConsentDataHolder.getInstance().getPrivilegedConsentManager();
    try {
        List<ReceiptListResponse> receiptListResponses = consentManager.searchReceipts(consentSearchLimit, 0, usernameWithUserStoreDomain, null, "*", null);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Found %d receipts issued for user: %s, in tenant domain: %s", receiptListResponses.size(), usernameWithUserStoreDomain, tenantDomain));
        }
        receiptListResponses.forEach(rethrowConsumer(receiptListResponse -> {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Deleting receipt with ID : %s, issued for application %s", receiptListResponse.getConsentReceiptId(), receiptListResponse.getSpDisplayName()));
            }
            consentManager.deleteReceipt(receiptListResponse.getConsentReceiptId());
        }));
    } catch (ConsentManagementException e) {
        throw new IdentityEventException("Error while deleting consents for user " + userName, e);
    }
}
Also used : IdentityConsentMgtUtils(org.wso2.carbon.identity.consent.mgt.IdentityConsentMgtUtils) IdentityConsentDataHolder(org.wso2.carbon.identity.consent.mgt.internal.IdentityConsentDataHolder) InitConfig(org.wso2.carbon.identity.core.handler.InitConfig) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) LambdaExceptionUtils.rethrowConsumer(org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer) AbstractEventHandler(org.wso2.carbon.identity.event.handler.AbstractEventHandler) ConsentManager(org.wso2.carbon.consent.mgt.core.ConsentManager) ConsentManagementException(org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException) Event(org.wso2.carbon.identity.event.event.Event) UserCoreConstants(org.wso2.carbon.user.core.UserCoreConstants) MessageContext(org.wso2.carbon.identity.core.bean.context.MessageContext) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) List(java.util.List) Map(java.util.Map) ReceiptListResponse(org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse) IdentityEventMessageContext(org.wso2.carbon.identity.event.bean.IdentityEventMessageContext) IdentityEventConstants(org.wso2.carbon.identity.event.IdentityEventConstants) UserCoreUtil(org.wso2.carbon.user.core.util.UserCoreUtil) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) ReceiptListResponse(org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse) ConsentManagementException(org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) ConsentManager(org.wso2.carbon.consent.mgt.core.ConsentManager) IdentityEventMessageContext(org.wso2.carbon.identity.event.bean.IdentityEventMessageContext)

Example 2 with ReceiptListResponse

use of org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse in project identity-governance by wso2-extensions.

the class ConsentInformationProviderTest method testGetRetainedUserInformationGetDomainException.

@Test(expectedExceptions = UserExportException.class)
public void testGetRetainedUserInformationGetDomainException() throws Exception {
    RealmService realmService = mock(RealmService.class);
    TenantManager tenantManager = mock(TenantManager.class);
    when(realmService.getTenantManager()).thenReturn(tenantManager);
    when(tenantManager.getDomain(anyInt())).thenThrow(new UserStoreException());
    ReceiptListResponse receiptListResponse = mock(ReceiptListResponse.class);
    List<ReceiptListResponse> receiptListResponses = new ArrayList<>();
    receiptListResponses.add(receiptListResponse);
    ConsentManager consentManager = mock(ConsentManager.class);
    when(consentManager.searchReceipts(eq(100), eq(0), anyString(), anyString(), anyString(), anyString())).thenReturn(receiptListResponses);
    when(consentManager.searchReceipts(eq(100), eq(100), anyString(), anyString(), anyString(), anyString())).thenReturn(new ArrayList<ReceiptListResponse>());
    Receipt mockReceipt = mock(Receipt.class);
    when(mockReceipt.getPiiPrincipalId()).thenReturn(USERNAME_CLAIM_VALUE);
    when(consentManager.getReceipt(anyString())).thenReturn(mockReceipt);
    ConsentInformationProvider consentInformationProvider = new ConsentInformationProvider();
    consentInformationProvider.setRealmService(realmService);
    consentInformationProvider.setConsentManager(consentManager);
    consentInformationProvider.getRetainedUserInformation(USERNAME_CLAIM_VALUE, UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME, -1234);
}
Also used : Receipt(org.wso2.carbon.consent.mgt.core.model.Receipt) RealmService(org.wso2.carbon.user.core.service.RealmService) ReceiptListResponse(org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ArrayList(java.util.ArrayList) ConsentManager(org.wso2.carbon.consent.mgt.core.ConsentManager) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Test(org.testng.annotations.Test)

Example 3 with ReceiptListResponse

use of org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse in project identity-governance by wso2-extensions.

the class ConsentInformationProviderTest method testGetRetainedUserInformation.

@Test
public void testGetRetainedUserInformation() throws Exception {
    RealmService realmService = mock(RealmService.class);
    TenantManager tenantManager = mock(TenantManager.class);
    when(realmService.getTenantManager()).thenReturn(tenantManager);
    when(tenantManager.getDomain(anyInt())).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    ReceiptListResponse receiptListResponse = new ReceiptListResponse("test1", "test1", "1", -1234, "test1", "test1", "test1");
    List<ReceiptListResponse> receiptListResponses = new ArrayList<>();
    receiptListResponses.add(receiptListResponse);
    ConsentManager consentManager = mock(ConsentManager.class);
    when(consentManager.searchReceipts(eq(100), eq(0), anyString(), anyString(), isNull(), anyString())).thenReturn(receiptListResponses);
    when(consentManager.searchReceipts(eq(100), eq(100), anyString(), anyString(), isNull(), anyString())).thenReturn(new ArrayList<ReceiptListResponse>());
    Receipt mockReceipt = mock(Receipt.class);
    when(mockReceipt.getPiiPrincipalId()).thenReturn(USERNAME_CLAIM_VALUE);
    when(consentManager.getReceipt(anyString())).thenReturn(mockReceipt);
    ConsentInformationProvider consentInformationProvider = new ConsentInformationProvider();
    consentInformationProvider.setRealmService(realmService);
    consentInformationProvider.setConsentManager(consentManager);
    UserInformationDTO retainedUserInformationObj = consentInformationProvider.getRetainedUserInformation(USERNAME_CLAIM_VALUE, UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME, -1234);
    if (retainedUserInformationObj != null && retainedUserInformationObj.getData() instanceof List) {
        List retainedUserInformationList = (List) retainedUserInformationObj.getData();
        Object receiptObj = retainedUserInformationList.get(0);
        if (receiptObj instanceof ConsentReceiptDTO) {
            ConsentReceiptDTO receipt = (ConsentReceiptDTO) receiptObj;
            Assert.assertEquals(receipt.getPiiPrincipalId(), USERNAME_CLAIM_VALUE);
        } else {
            Assert.fail();
        }
    } else {
        Assert.fail();
    }
}
Also used : ConsentReceiptDTO(org.wso2.carbon.identity.user.export.core.dto.ConsentReceiptDTO) Receipt(org.wso2.carbon.consent.mgt.core.model.Receipt) ArrayList(java.util.ArrayList) ConsentManager(org.wso2.carbon.consent.mgt.core.ConsentManager) UserInformationDTO(org.wso2.carbon.identity.user.export.core.dto.UserInformationDTO) RealmService(org.wso2.carbon.user.core.service.RealmService) ReceiptListResponse(org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse) ArrayList(java.util.ArrayList) List(java.util.List) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Test(org.testng.annotations.Test)

Example 4 with ReceiptListResponse

use of org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse in project identity-governance by wso2-extensions.

the class ConsentInformationProviderTest method testGetRetainedUserInformationSearchReceiptsException.

@Test(expectedExceptions = UserExportException.class)
public void testGetRetainedUserInformationSearchReceiptsException() throws Exception {
    RealmService realmService = mock(RealmService.class);
    TenantManager tenantManager = mock(TenantManager.class);
    when(realmService.getTenantManager()).thenReturn(tenantManager);
    when(tenantManager.getDomain(anyInt())).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    ConsentManager consentManager = mock(ConsentManager.class);
    when(consentManager.searchReceipts(eq(100), eq(00), anyString(), anyString(), isNull(), anyString())).thenThrow(new ConsentManagementException());
    when(consentManager.searchReceipts(eq(100), eq(100), anyString(), anyString(), isNull(), anyString())).thenReturn(new ArrayList<ReceiptListResponse>());
    Receipt mockReceipt = mock(Receipt.class);
    when(mockReceipt.getPiiPrincipalId()).thenReturn(USERNAME_CLAIM_VALUE);
    when(consentManager.getReceipt(anyString())).thenReturn(mockReceipt);
    ConsentInformationProvider consentInformationProvider = new ConsentInformationProvider();
    consentInformationProvider.setRealmService(realmService);
    consentInformationProvider.setConsentManager(consentManager);
    consentInformationProvider.getRetainedUserInformation(USERNAME_CLAIM_VALUE, UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME, -1234);
}
Also used : Receipt(org.wso2.carbon.consent.mgt.core.model.Receipt) RealmService(org.wso2.carbon.user.core.service.RealmService) ConsentManagementException(org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException) ReceiptListResponse(org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse) ConsentManager(org.wso2.carbon.consent.mgt.core.ConsentManager) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Test(org.testng.annotations.Test)

Example 5 with ReceiptListResponse

use of org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse in project carbon-identity-framework by wso2.

the class SSOConsentServiceImpl method getConsentReceiptOfUser.

private Receipt getConsentReceiptOfUser(ServiceProvider serviceProvider, AuthenticatedUser authenticatedUser, String spName, String spTenantDomain, String subject) throws SSOConsentServiceException {
    int receiptListLimit = 2;
    List<ReceiptListResponse> receiptListResponses;
    try {
        receiptListResponses = getReceiptListOfUserForSP(authenticatedUser, spName, spTenantDomain, subject, receiptListLimit);
        if (isDebugEnabled()) {
            String message = String.format("Retrieved %s receipts for user: %s, service provider: %s in tenant " + "domain %s", receiptListResponses.size(), subject, serviceProvider, spTenantDomain);
            logDebug(message);
        }
        if (hasUserMultipleReceipts(receiptListResponses)) {
            throw new SSOConsentServiceException("Consent Management Error", "User cannot have more than one " + "ACTIVE consent per service provider.");
        } else if (hasUserSingleReceipt(receiptListResponses)) {
            String receiptId = getFirstConsentReceiptFromList(receiptListResponses);
            return getReceipt(authenticatedUser, receiptId);
        } else {
            return null;
        }
    } catch (ConsentManagementException e) {
        throw new SSOConsentServiceException("Consent Management Error", "Error while retrieving user consents.", e);
    }
}
Also used : ReceiptListResponse(org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse) ConsentManagementException(org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException) SSOConsentServiceException(org.wso2.carbon.identity.application.authentication.framework.handler.request.impl.consent.exception.SSOConsentServiceException)

Aggregations

ReceiptListResponse (org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse)7 ConsentManager (org.wso2.carbon.consent.mgt.core.ConsentManager)5 ConsentManagementException (org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException)5 Receipt (org.wso2.carbon.consent.mgt.core.model.Receipt)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Test (org.testng.annotations.Test)3 RealmService (org.wso2.carbon.user.core.service.RealmService)3 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)3 Map (java.util.Map)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 IdentityConsentMgtUtils (org.wso2.carbon.identity.consent.mgt.IdentityConsentMgtUtils)2 IdentityConsentDataHolder (org.wso2.carbon.identity.consent.mgt.internal.IdentityConsentDataHolder)2 LambdaExceptionUtils.rethrowConsumer (org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer)2 ConsentReceiptDTO (org.wso2.carbon.identity.user.export.core.dto.ConsentReceiptDTO)2 UserInformationDTO (org.wso2.carbon.identity.user.export.core.dto.UserInformationDTO)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 Properties (java.util.Properties)1 SSOConsentServiceException (org.wso2.carbon.identity.application.authentication.framework.handler.request.impl.consent.exception.SSOConsentServiceException)1