use of io.micronaut.security.authentication.UserDetails in project check-ins by objectcomputing.
the class UserPasswordAuthProvider method authenticate.
@Override
public Publisher<AuthenticationResponse> authenticate(HttpRequest<?> httpRequest, AuthenticationRequest<?, ?> authReq) {
String email = authReq.getIdentity().toString();
String roleCred = authReq.getSecret().toString();
Map<String, Object> attributes = new HashMap<>();
attributes.put("email", email);
UserDetails details = new UserDetails(email, store.getUserRole(roleCred), attributes);
return Flowable.just(details);
}
use of io.micronaut.security.authentication.UserDetails in project check-ins by objectcomputing.
the class LocalLoginController method auth.
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON })
@Post
public Single<MutableHttpResponse<?>> auth(HttpRequest<?> request, String email, String role) {
UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(email, role);
Flowable<AuthenticationResponse> authenticationResponseFlowable = Flowable.fromPublisher(authenticator.authenticate(request, usernamePasswordCredentials));
return authenticationResponseFlowable.map(authenticationResponse -> {
if (authenticationResponse.isAuthenticated() && authenticationResponse.getUserDetails().isPresent()) {
UserDetails userDetails = authenticationResponse.getUserDetails().get();
// Get member profile by work email
MemberProfile memberProfile = currentUserServices.findOrSaveUser("", "", email);
String firstName = memberProfile.getFirstName() != null ? memberProfile.getFirstName() : "";
String lastName = memberProfile.getLastName() != null ? memberProfile.getLastName() : "";
userDetails.setAttributes(Map.of("email", memberProfile.getWorkEmail(), "name", firstName + ' ' + lastName, "picture", ""));
eventPublisher.publishEvent(new LoginSuccessfulEvent(userDetails));
return loginHandler.loginSuccess(userDetails, request);
} else {
eventPublisher.publishEvent(new LoginFailedEvent(authenticationResponse));
return loginHandler.loginFailed(authenticationResponse, request);
}
}).first(HttpResponse.status(HttpStatus.UNAUTHORIZED));
}
use of io.micronaut.security.authentication.UserDetails in project check-ins by objectcomputing.
the class CheckinsOpenIdUserDetailMapper method createUserDetails.
@NonNull
@Override
public UserDetails createUserDetails(String providerName, OpenIdTokenResponse tokenResponse, OpenIdClaims openIdClaims) {
Map<String, Object> claims = buildAttributes(providerName, tokenResponse, openIdClaims);
List<String> roles = getRoles(openIdClaims);
String username = openIdClaims.getSubject();
UserDetails userDetails = new UserDetails(username, roles, claims);
LOG.info("Creating new userdetails for user: {}", userDetails.getUsername());
return userDetails;
}
use of io.micronaut.security.authentication.UserDetails in project check-ins by objectcomputing.
the class CheckinsOpenIdUserDetailMapperTest method testCreateAuthenticationResponse.
@Test
void testCreateAuthenticationResponse() {
CheckinsOpenIdUserDetailMapper checkinsOpenIdUserDetailMapper = (CheckinsOpenIdUserDetailMapper) openIdUserDetailsMapper;
MemberProfile memberProfile = createADefaultMemberProfile();
List<String> roles = List.of(RoleType.Constants.ADMIN_ROLE, RoleType.Constants.PDL_ROLE);
for (String role : roles) {
createAndAssignRole(RoleType.valueOf(role), memberProfile);
}
String provider = "Test";
OpenIdTokenResponse openIdTokenResponse = new OpenIdTokenResponse();
OpenIdClaims openIdClaims = new JWTOpenIdClaims(new JWTClaimsSet.Builder().claim("email", memberProfile.getWorkEmail()).claim("sub", MemberProfileUtils.getFullName(memberProfile)).build());
AuthenticationResponse auth = checkinsOpenIdUserDetailMapper.createAuthenticationResponse(provider, openIdTokenResponse, openIdClaims, null);
assertNotNull(auth);
UserDetails userDetails = auth.getUserDetails().orElse(null);
assertNotNull(userDetails);
assertEquals(MemberProfileUtils.getFullName(memberProfile), userDetails.getUsername());
assertThat(userDetails.getRoles(), CoreMatchers.hasItems(RoleType.Constants.PDL_ROLE, RoleType.Constants.ADMIN_ROLE));
assertTrue(roles.containsAll(userDetails.getRoles()));
assertEquals(roles.size(), userDetails.getRoles().size());
}
use of io.micronaut.security.authentication.UserDetails in project ns4kafka by michelin.
the class LocalUserAuthenticationProviderTest method authenticateMatchUserMatchPassword.
@Test
void authenticateMatchUserMatchPassword() {
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "admin");
Mockito.when(securityConfig.getLocalUsers()).thenReturn(List.of(LocalUser.builder().username("admin").password("8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918").groups(List.of("admin")).build()));
Mockito.when(resourceBasedSecurityRule.computeRolesFromGroups(ArgumentMatchers.any())).thenReturn(List.of());
TestSubscriber<AuthenticationResponse> subscriber = new TestSubscriber();
Publisher<AuthenticationResponse> authenticationResponsePublisher = localUserAuthenticationProvider.authenticate(null, credentials);
authenticationResponsePublisher.subscribe(subscriber);
subscriber.awaitTerminalEvent();
// then
subscriber.assertComplete();
subscriber.assertNoErrors();
subscriber.assertValueCount(1);
AuthenticationResponse actual = subscriber.values().get(0);
Assertions.assertTrue(actual.isAuthenticated());
Assertions.assertTrue(actual.getUserDetails().isPresent());
UserDetails actualUserDetails = actual.getUserDetails().get();
Assertions.assertEquals("admin", actualUserDetails.getUsername());
}
Aggregations