use of io.gravitee.management.service.exceptions.UserNotFoundException in project gravitee-management-rest-api by gravitee-io.
the class OAuth2AuthenticationResourceTest method shouldConnectNewUserWithNoMatchingGroupsMappingFromUserInfo.
@Test
public void shouldConnectNewUserWithNoMatchingGroupsMappingFromUserInfo() throws Exception {
// -- MOCK
// mock environment
mockDefaultEnvironment();
mockGroupsMapping();
// mock oauth2 exchange authorisation code for access token
mockExchangeAuthorizationCodeForAccessToken();
// mock oauth2 user info call
mockUserInfo(okJson(IOUtils.toString(read("/oauth2/json/user_info_response_body_no_matching.json"), Charset.defaultCharset())));
// mock DB find user by name
when(userService.findByUsername("janedoe@example.com", false)).thenThrow(new UserNotFoundException("janedoe@example.com"));
// mock create user
NewExternalUserEntity newExternalUserEntity = mockNewExternalUserEntity();
UserEntity createdUser = mockUserEntity();
mockUserCreation(newExternalUserEntity, createdUser, true);
// mock DB update user picture
UpdateUserEntity updateUserEntity = mockUpdateUserPicture(createdUser);
// mock DB user connect
when(userService.connect("janedoe@example.com")).thenReturn(createdUser);
// -- CALL
AbstractAuthenticationResource.Payload payload = createPayload("the_client_id", "http://localhost/callback", "CoDe", "StAtE");
;
Response response = target().request().post(json(payload));
// -- VERIFY
verify(userService, times(1)).findByUsername("janedoe@example.com", false);
verify(userService, times(1)).create(refEq(newExternalUserEntity), eq(true));
verify(userService, times(1)).update(refEq(updateUserEntity));
verify(userService, times(1)).connect("janedoe@example.com");
// verify group creations
verify(membershipService, times(0)).addOrUpdateMember(any(MembershipService.MembershipReference.class), any(MembershipService.MembershipUser.class), any(MembershipService.MembershipRole.class));
assertEquals(HttpStatusCode.OK_200, response.getStatus());
// verify response body
verifyUserInResponseBody(response);
// verify jwt token
verifyJwtToken(response);
}
use of io.gravitee.management.service.exceptions.UserNotFoundException in project gravitee-management-rest-api by gravitee-io.
the class OAuth2AuthenticationResourceTest method shouldNotConnectNewUserWhenWrongELGroupsMapping.
@Test
public void shouldNotConnectNewUserWhenWrongELGroupsMapping() throws Exception {
// -- MOCK
// mock environment
mockDefaultEnvironment();
mockWrongELGroupsMapping();
// mock oauth2 exchange authorisation code for access token
mockExchangeAuthorizationCodeForAccessToken();
// mock oauth2 user info call
mockUserInfo(okJson(IOUtils.toString(read("/oauth2/json/user_info_response_body.json"), Charset.defaultCharset())));
// mock DB find user by name
when(userService.findByUsername("janedoe@example.com", false)).thenThrow(new UserNotFoundException("janedoe@example.com"));
NewExternalUserEntity newExternalUserEntity = mockNewExternalUserEntity();
UserEntity createdUser = mockUserEntity();
mockUserCreation(newExternalUserEntity, createdUser, true);
// -- CALL
AbstractAuthenticationResource.Payload payload = createPayload("the_client_id", "http://localhost/callback", "CoDe", "StAtE");
;
Response response = target().request().post(json(payload));
// -- VERIFY
verify(userService, times(1)).findByUsername("janedoe@example.com", false);
verify(userService, times(0)).create(any(NewExternalUserEntity.class), anyBoolean());
verify(userService, times(0)).update(any(UpdateUserEntity.class));
verify(userService, times(0)).connect(anyString());
// verify group creations
verify(membershipService, times(0)).addOrUpdateMember(any(MembershipService.MembershipReference.class), any(MembershipService.MembershipUser.class), any(MembershipService.MembershipRole.class));
assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR_500, response.getStatus());
// verify jwt token
verifyJwtTokenIsNotPresent(response);
}
use of io.gravitee.management.service.exceptions.UserNotFoundException in project gravitee-management-rest-api by gravitee-io.
the class OAuth2AuthenticationResourceTest method shouldConnectNewUser.
@Test
public void shouldConnectNewUser() throws Exception {
// -- MOCK
// mock environment
mockDefaultEnvironment();
// mock oauth2 exchange authorisation code for access token
mockExchangeAuthorizationCodeForAccessToken();
// mock oauth2 user info call
mockUserInfo(okJson(IOUtils.toString(read("/oauth2/json/user_info_response_body.json"), Charset.defaultCharset())));
// mock DB find user by name
when(userService.findByUsername("janedoe@example.com", false)).thenThrow(new UserNotFoundException("janedoe@example.com"));
// mock create user
NewExternalUserEntity newExternalUserEntity = mockNewExternalUserEntity();
UserEntity createdUser = mockUserEntity();
mockUserCreation(newExternalUserEntity, createdUser, true);
// mock DB update user picture
UpdateUserEntity user = mockUpdateUserPicture(createdUser);
// mock DB user connect
when(userService.connect("janedoe@example.com")).thenReturn(createdUser);
// -- CALL
AbstractAuthenticationResource.Payload payload = createPayload("the_client_id", "http://localhost/callback", "CoDe", "StAtE");
;
Response response = target().request().post(json(payload));
// -- VERIFY
verify(userService, times(1)).findByUsername("janedoe@example.com", false);
verify(userService, times(1)).create(refEq(newExternalUserEntity), eq(true));
verify(userService, times(1)).update(refEq(user));
verify(userService, times(1)).connect("janedoe@example.com");
assertEquals(HttpStatusCode.OK_200, response.getStatus());
// verify response body
verifyUserInResponseBody(response);
// verify jwt token
verifyJwtToken(response);
}
use of io.gravitee.management.service.exceptions.UserNotFoundException in project gravitee-management-rest-api by gravitee-io.
the class CurrentUserResource method getCurrentUser.
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get the authenticated user")
public Response getCurrentUser() {
if (isAuthenticated()) {
final UserDetails details = getAuthenticatedUserDetails();
final String userId = details.getUsername();
UserEntity userEntity;
try {
userEntity = userService.findByIdWithRoles(userId);
} catch (final UserNotFoundException unfe) {
final String unfeMessage = "User '{}' does not exist.";
if (LOG.isDebugEnabled()) {
LOG.info(unfeMessage, userId, unfe);
} else {
LOG.info(unfeMessage, userId);
}
return logout();
}
List<GrantedAuthority> authorities = new ArrayList<>(details.getAuthorities());
UserDetails userDetails = new UserDetails(userEntity.getId(), details.getPassword(), authorities);
userDetails.setId(userEntity.getId());
userDetails.setFirstname(details.getFirstname());
userDetails.setLastname(details.getLastname());
userDetails.setUsername(userEntity.getUsername());
userDetails.setEmail(details.getEmail());
// convert UserEntityRoles to UserDetailsRoles
userDetails.setRoles(userEntity.getRoles().stream().map(userEntityRole -> {
UserDetailRole userDetailRole = new UserDetailRole();
userDetailRole.setScope(userEntityRole.getScope().name());
userDetailRole.setName(userEntityRole.getName());
userDetailRole.setPermissions(userEntityRole.getPermissions());
return userDetailRole;
}).collect(Collectors.toList()));
return Response.ok(userDetails, MediaType.APPLICATION_JSON).build();
} else {
return Response.ok().build();
}
}
use of io.gravitee.management.service.exceptions.UserNotFoundException in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method findByIds.
@Override
public Set<UserEntity> findByIds(List<String> ids) {
try {
LOGGER.debug("Find users by ID: {}", ids);
Set<User> users = userRepository.findByIds(ids);
if (!users.isEmpty()) {
return users.stream().map(u -> this.convert(u, false)).collect(Collectors.toSet());
}
Optional<String> idsAsString = ids.stream().reduce((a, b) -> a + '/' + b);
if (idsAsString.isPresent()) {
throw new UserNotFoundException(idsAsString.get());
} else {
throw new UserNotFoundException("?");
}
} catch (TechnicalException ex) {
Optional<String> idsAsString = ids.stream().reduce((a, b) -> a + '/' + b);
LOGGER.error("An error occurs while trying to find users using their ID {}", idsAsString, ex);
throw new TechnicalManagementException("An error occurs while trying to find users using their ID " + idsAsString, ex);
}
}
Aggregations