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;
}
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;
}
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);
}
}
}
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;
}
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."));
}
}
Aggregations