Search in sources :

Example 1 with SCIMUser

use of org.wso2.carbon.apimgt.core.auth.dto.SCIMUser in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImpl method getRoleNamesOfUser.

@Override
public List<String> getRoleNamesOfUser(String userId) throws IdentityProviderException {
    List<String> roleNames = new ArrayList<>();
    Response response = scimServiceStub.getUser(userId);
    if (response == null) {
        String errorMessage = "Error occurred while retrieving user with Id " + userId + ". Error : Response is null.";
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    try {
        if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
            SCIMUser scimUser = (SCIMUser) new GsonDecoder().decode(response, SCIMUser.class);
            if (scimUser != null) {
                List<SCIMUser.SCIMUserGroups> roles = scimUser.getGroups();
                if (roles != null) {
                    roles.forEach(role -> roleNames.add(role.getDisplay()));
                    String message = "Role names of user " + scimUser.getName() + " are successfully retrieved as " + StringUtils.join(roleNames, ", ") + ".";
                    if (log.isDebugEnabled()) {
                        log.debug(message);
                    }
                }
            } else {
                String errorMessage = "Error occurred while retrieving user with user Id " + userId + " from SCIM endpoint. " + "Response body is null or empty.";
                log.error(errorMessage);
                throw new IdentityProviderException("Error occurred while retrieving user with user Id " + userId + " from SCIM endpoint. " + "Response body is null or empty.", ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
            }
        } else {
            String errorMessage = "Error occurred while retrieving role names of user with Id " + userId + ". Error : " + getErrorMessage(response);
            log.error(errorMessage);
            throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
        }
    } catch (IOException e) {
        String errorMessage = "Error occurred while parsing response from SCIM endpoint.";
        log.error(errorMessage);
        throw new IdentityProviderException("Error occurred while parsing response from SCIM endpoint for ", e, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    return roleNames;
}
Also used : Response(feign.Response) SCIMUser(org.wso2.carbon.apimgt.core.auth.dto.SCIMUser) ArrayList(java.util.ArrayList) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException)

Example 2 with SCIMUser

use of org.wso2.carbon.apimgt.core.auth.dto.SCIMUser in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImpl method getIdOfUser.

@Override
public String getIdOfUser(String userName) throws IdentityProviderException {
    // should not user id outside this domain and should not log that id.
    try {
        userName = userNameMapper.getLoggedInUserIDFromPseudoName(userName);
    } catch (APIManagementException e) {
        throw new IdentityProviderException(e.getMessage(), ExceptionCodes.USER_MAPPING_RETRIEVAL_FAILED);
    }
    Response userResponse = scimServiceStub.searchUsers(FILTER_PREFIX_USER + userName);
    String userId;
    if (userResponse == null) {
        String errorMessage = "Error occurred while retrieving Id of user " + userName + ". Error : Response is null.";
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    if (userResponse.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
        String responseBody = userResponse.body().toString();
        JsonParser parser = new JsonParser();
        JsonObject parsedResponseBody = (JsonObject) parser.parse(responseBody);
        JsonArray user = (JsonArray) parsedResponseBody.get(RESOURCES);
        JsonObject scimUser = (JsonObject) user.get(0);
        userId = scimUser.get(ID).getAsString();
        String message = "Id " + userId + " of user " + scimUser.get(USERNAME).getAsString() + " is successfully retrieved from SCIM endpoint.";
        if (log.isDebugEnabled()) {
            log.debug(message);
        }
    } else {
        String errorMessage = "Error occurred while retrieving Id of user " + userName + ". Error : " + getErrorMessage(userResponse);
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    return userId;
}
Also used : Response(feign.Response) JsonArray(com.google.gson.JsonArray) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) JsonObject(com.google.gson.JsonObject) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) JsonParser(com.google.gson.JsonParser)

Example 3 with SCIMUser

use of org.wso2.carbon.apimgt.core.auth.dto.SCIMUser in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImpl method registerUser.

@Override
public void registerUser(User user) throws IdentityProviderException {
    SCIMUser scimUser = new SCIMUser();
    scimUser.setUsername(user.getUsername());
    scimUser.setPassword(new String(user.getPassword()));
    scimUser.setName(new SCIMUser.SCIMName(user.getFirstName(), user.getLastName()));
    List<SCIMUser.SCIMUserEmails> emails = new ArrayList<>();
    emails.add(new SCIMUser.SCIMUserEmails(user.getEmail(), HOME_EMAIL, true));
    scimUser.setEmails(emails);
    Response response = scimServiceStub.addUser(scimUser);
    if (response == null || response.status() != APIMgtConstants.HTTPStatusCodes.SC_201_CREATED) {
        StringBuilder errorMessage = new StringBuilder("Error occurred while creating user. ");
        if (response == null) {
            errorMessage.append("Response is null");
        } else {
            String msg = getErrorMessage(response);
            if (!StringUtils.isEmpty(msg)) {
                errorMessage.append(msg);
            }
        }
        throw new IdentityProviderException(errorMessage.toString(), ExceptionCodes.USER_CREATION_FAILED);
    } else {
        String message = "User  " + user.getUsername() + " is successfully created";
        if (log.isDebugEnabled()) {
            log.debug(message);
        }
    }
}
Also used : Response(feign.Response) SCIMUser(org.wso2.carbon.apimgt.core.auth.dto.SCIMUser) ArrayList(java.util.ArrayList) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException)

Example 4 with SCIMUser

use of org.wso2.carbon.apimgt.core.auth.dto.SCIMUser in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImpl method getRoleIdsOfUser.

@Override
public List<String> getRoleIdsOfUser(String userId) throws IdentityProviderException {
    List<String> roleIds = new ArrayList<>();
    Response response = scimServiceStub.getUser(userId);
    if (response == null) {
        String errorMessage = "Error occurred while retrieving user with Id " + userId + ". Error : Response is null.";
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    try {
        if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
            SCIMUser scimUser = (SCIMUser) new GsonDecoder().decode(response, SCIMUser.class);
            if (scimUser != null) {
                List<SCIMUser.SCIMUserGroups> roles = scimUser.getGroups();
                if (roles != null) {
                    roles.forEach(role -> roleIds.add(role.getValue()));
                    String message = "Role Ids of user " + scimUser.getName() + " are successfully retrieved as " + StringUtils.join(roleIds, ", ") + ".";
                    if (log.isDebugEnabled()) {
                        log.debug(message);
                    }
                }
            } else {
                String errorMessage = "Error occurred while retrieving user with user Id " + userId + " from SCIM endpoint. " + "Response body is null or empty.";
                log.error(errorMessage);
                throw new IdentityProviderException("Error occurred while retrieving user with user Id " + userId + " from SCIM endpoint. " + "Response body is null or empty.", ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
            }
        } else {
            String errorMessage = "Error occurred while retrieving role Ids of user with Id " + userId + ". Error : " + getErrorMessage(response);
            log.error(errorMessage);
            throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
        }
    } catch (IOException e) {
        String errorMessage = "Error occurred while parsing response from SCIM endpoint.";
        log.error(errorMessage);
        throw new IdentityProviderException("Error occurred while parsing response from SCIM endpoint for ", e, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    return roleIds;
}
Also used : Response(feign.Response) SCIMUser(org.wso2.carbon.apimgt.core.auth.dto.SCIMUser) ArrayList(java.util.ArrayList) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException)

Example 5 with SCIMUser

use of org.wso2.carbon.apimgt.core.auth.dto.SCIMUser in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImplTestCase method testRegisterUser.

@Test
public void testRegisterUser() throws Exception {
    SCIMServiceStub scimServiceStub = Mockito.mock(SCIMServiceStub.class);
    UserNameMapper userNameMapper = Mockito.mock(UserNameMapperImpl.class);
    DefaultIdentityProviderImpl idpImpl = new DefaultIdentityProviderImpl(scimServiceStub, userNameMapper);
    // happy path
    User user = new User();
    user.setFirstName("john");
    user.setLastName("doe");
    user.setUsername("johnd");
    user.setEmail("john@wso2.com");
    user.setPassword(new char[] { 'p', 'a', 's', 's' });
    SCIMUser scimUser = new SCIMUser();
    SCIMUser.SCIMName scimName = new SCIMUser.SCIMName();
    scimName.setGivenName(user.getFirstName());
    scimName.setFamilyName(user.getLastName());
    scimUser.setName(scimName);
    SCIMUser.SCIMUserEmails scimUserEmails = new SCIMUser.SCIMUserEmails(user.getEmail(), "home", true);
    List<SCIMUser.SCIMUserEmails> scimUserEmailList = new ArrayList<>();
    scimUserEmailList.add(scimUserEmails);
    scimUser.setEmails(scimUserEmailList);
    scimUser.setUsername(user.getUsername());
    scimUser.setPassword(String.valueOf(user.getPassword()));
    Response createdResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_201_CREATED).headers(new HashMap<>()).build();
    Mockito.when(scimServiceStub.addUser(scimUser)).thenReturn(createdResponse);
    try {
        idpImpl.registerUser(user);
        Assert.assertTrue(true);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
    // error path
    final int errorSc = APIMgtConstants.HTTPStatusCodes.SC_409_CONFLICT;
    final String errorMsg = "{\"Errors\":[{\"code\":\"409\",\"description\":\"Error in adding the user: test to " + "the user store.\"}]}";
    Response errorResponse = Response.builder().status(errorSc).headers(new HashMap<>()).body(errorMsg.getBytes()).build();
    Mockito.when(scimServiceStub.addUser(any(SCIMUser.class))).thenReturn(errorResponse);
    try {
        idpImpl.registerUser(user);
        Assert.fail("Exception was expected, but wasn't thrown");
    } catch (IdentityProviderException ex) {
        Assert.assertTrue(ex.getMessage().startsWith("Error occurred while creating user."));
    }
}
Also used : SCIMUser(org.wso2.carbon.apimgt.core.auth.dto.SCIMUser) User(org.wso2.carbon.apimgt.core.models.User) SCIMUser(org.wso2.carbon.apimgt.core.auth.dto.SCIMUser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) Response(feign.Response) UserNameMapper(org.wso2.carbon.apimgt.core.api.UserNameMapper) SCIMServiceStub(org.wso2.carbon.apimgt.core.auth.SCIMServiceStub) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) Test(org.testng.annotations.Test)

Aggregations

Response (feign.Response)5 IdentityProviderException (org.wso2.carbon.apimgt.core.exception.IdentityProviderException)5 ArrayList (java.util.ArrayList)4 SCIMUser (org.wso2.carbon.apimgt.core.auth.dto.SCIMUser)4 GsonDecoder (feign.gson.GsonDecoder)2 IOException (java.io.IOException)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 HashMap (java.util.HashMap)1 Test (org.testng.annotations.Test)1 UserNameMapper (org.wso2.carbon.apimgt.core.api.UserNameMapper)1 SCIMServiceStub (org.wso2.carbon.apimgt.core.auth.SCIMServiceStub)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 User (org.wso2.carbon.apimgt.core.models.User)1