Search in sources :

Example 1 with UserDetails

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);
}
Also used : UserDetails(io.micronaut.security.authentication.UserDetails) HashMap(java.util.HashMap)

Example 2 with UserDetails

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));
}
Also used : UsernamePasswordCredentials(io.micronaut.security.authentication.UsernamePasswordCredentials) View(io.micronaut.views.View) Single(io.reactivex.Single) Environments(com.objectcomputing.checkins.Environments) LoginSuccessfulEvent(io.micronaut.security.event.LoginSuccessfulEvent) Flowable(io.reactivex.Flowable) HttpStatus(io.micronaut.http.HttpStatus) MediaType(io.micronaut.http.MediaType) Map(java.util.Map) Requires(io.micronaut.context.annotation.Requires) HttpResponse(io.micronaut.http.HttpResponse) HttpRequest(io.micronaut.http.HttpRequest) LoginFailedEvent(io.micronaut.security.event.LoginFailedEvent) AuthenticationResponse(io.micronaut.security.authentication.AuthenticationResponse) Get(io.micronaut.http.annotation.Get) Controller(io.micronaut.http.annotation.Controller) LoginHandler(io.micronaut.security.handlers.LoginHandler) Secured(io.micronaut.security.annotation.Secured) ApplicationEventPublisher(io.micronaut.context.event.ApplicationEventPublisher) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) SecurityRule(io.micronaut.security.rules.SecurityRule) Post(io.micronaut.http.annotation.Post) Authenticator(io.micronaut.security.authentication.Authenticator) UserDetails(io.micronaut.security.authentication.UserDetails) Consumes(io.micronaut.http.annotation.Consumes) CurrentUserServices(com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) Collections(java.util.Collections) UserDetails(io.micronaut.security.authentication.UserDetails) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) LoginSuccessfulEvent(io.micronaut.security.event.LoginSuccessfulEvent) AuthenticationResponse(io.micronaut.security.authentication.AuthenticationResponse) LoginFailedEvent(io.micronaut.security.event.LoginFailedEvent) UsernamePasswordCredentials(io.micronaut.security.authentication.UsernamePasswordCredentials) Consumes(io.micronaut.http.annotation.Consumes) Post(io.micronaut.http.annotation.Post)

Example 3 with UserDetails

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;
}
Also used : UserDetails(io.micronaut.security.authentication.UserDetails) NonNull(io.micronaut.core.annotation.NonNull)

Example 4 with 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());
}
Also used : UserDetails(io.micronaut.security.authentication.UserDetails) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) JWTOpenIdClaims(io.micronaut.security.oauth2.endpoint.token.response.JWTOpenIdClaims) OpenIdClaims(io.micronaut.security.oauth2.endpoint.token.response.OpenIdClaims) JWTOpenIdClaims(io.micronaut.security.oauth2.endpoint.token.response.JWTOpenIdClaims) AuthenticationResponse(io.micronaut.security.authentication.AuthenticationResponse) OpenIdTokenResponse(io.micronaut.security.oauth2.endpoint.token.response.OpenIdTokenResponse) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.annotation.MicronautTest)

Example 5 with UserDetails

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());
}
Also used : UserDetails(io.micronaut.security.authentication.UserDetails) TestSubscriber(io.reactivex.subscribers.TestSubscriber) AuthenticationResponse(io.micronaut.security.authentication.AuthenticationResponse) UsernamePasswordCredentials(io.micronaut.security.authentication.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Aggregations

UserDetails (io.micronaut.security.authentication.UserDetails)5 AuthenticationResponse (io.micronaut.security.authentication.AuthenticationResponse)3 MemberProfile (com.objectcomputing.checkins.services.memberprofile.MemberProfile)2 UsernamePasswordCredentials (io.micronaut.security.authentication.UsernamePasswordCredentials)2 Test (org.junit.jupiter.api.Test)2 Environments (com.objectcomputing.checkins.Environments)1 CurrentUserServices (com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices)1 Requires (io.micronaut.context.annotation.Requires)1 ApplicationEventPublisher (io.micronaut.context.event.ApplicationEventPublisher)1 NonNull (io.micronaut.core.annotation.NonNull)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpResponse (io.micronaut.http.HttpResponse)1 HttpStatus (io.micronaut.http.HttpStatus)1 MediaType (io.micronaut.http.MediaType)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 Consumes (io.micronaut.http.annotation.Consumes)1 Controller (io.micronaut.http.annotation.Controller)1 Get (io.micronaut.http.annotation.Get)1 Post (io.micronaut.http.annotation.Post)1 Secured (io.micronaut.security.annotation.Secured)1