Search in sources :

Example 11 with UserDetails

use of io.gravitee.management.idp.api.authentication.UserDetails in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceTest method shouldNotSubscribe_applicationWithoutClientId.

@Test(expected = PlanNotSubscribableException.class)
public void shouldNotSubscribe_applicationWithoutClientId() throws Exception {
    // Prepare data
    when(plan.getApis()).thenReturn(Collections.singleton(API_ID));
    when(plan.getValidation()).thenReturn(PlanValidationType.AUTO);
    when(plan.getSecurity()).thenReturn(PlanSecurityType.OAUTH2);
    // subscription object is not a mock since its state is updated by the call to subscriptionService.create()
    Subscription subscription = new Subscription();
    subscription.setId(SUBSCRIPTION_ID);
    subscription.setApplication(APPLICATION_ID);
    subscription.setPlan(PLAN_ID);
    subscription.setStatus(Subscription.Status.PENDING);
    SecurityContextHolder.setContext(new SecurityContext() {

        @Override
        public Authentication getAuthentication() {
            return new Authentication() {

                @Override
                public Collection<? extends GrantedAuthority> getAuthorities() {
                    return null;
                }

                @Override
                public Object getCredentials() {
                    return null;
                }

                @Override
                public Object getDetails() {
                    return null;
                }

                @Override
                public Object getPrincipal() {
                    return new UserDetails("tester", "password", Collections.emptyList());
                }

                @Override
                public boolean isAuthenticated() {
                    return false;
                }

                @Override
                public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
                }

                @Override
                public String getName() {
                    return null;
                }
            };
        }

        @Override
        public void setAuthentication(Authentication authentication) {
        }
    });
    // Stub
    when(planService.findById(PLAN_ID)).thenReturn(plan);
    when(applicationService.findById(APPLICATION_ID)).thenReturn(application);
    when(apiService.findByIdForTemplates(API_ID)).thenReturn(apiModelEntity);
    // Run
    subscriptionService.create(new NewSubscriptionEntity(PLAN_ID, APPLICATION_ID));
}
Also used : UserDetails(io.gravitee.management.idp.api.authentication.UserDetails) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SecurityContext(org.springframework.security.core.context.SecurityContext) Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 12 with UserDetails

use of io.gravitee.management.idp.api.authentication.UserDetails in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceTest method init.

@Before
public void init() {
    setField(ratingService, "enabled", true);
    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(new UserDetails(USER, "", emptyList()));
    final SecurityContext securityContext = mock(SecurityContext.class);
    when(securityContext.getAuthentication()).thenReturn(authentication);
    SecurityContextHolder.setContext(securityContext);
    when(rating.getId()).thenReturn(RATING_ID);
    when(rating.getApi()).thenReturn(API_ID);
    when(rating.getTitle()).thenReturn(TITLE);
    when(rating.getComment()).thenReturn(COMMENT);
    when(rating.getRate()).thenReturn(RATE);
    when(rating.getUser()).thenReturn(USER);
    when(userService.findById(USER)).thenReturn(user);
    when(user.getId()).thenReturn(USER);
}
Also used : UserDetails(io.gravitee.management.idp.api.authentication.UserDetails) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) Before(org.junit.Before)

Example 13 with UserDetails

use of io.gravitee.management.idp.api.authentication.UserDetails 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();
    }
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) UserDetails(io.gravitee.management.idp.api.authentication.UserDetails) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) UserDetailRole(io.gravitee.management.idp.api.authentication.UserDetailRole) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

UserDetails (io.gravitee.management.idp.api.authentication.UserDetails)13 Authentication (org.springframework.security.core.Authentication)7 GrantedAuthority (org.springframework.security.core.GrantedAuthority)7 UserNotFoundException (io.gravitee.management.service.exceptions.UserNotFoundException)5 SecurityContext (org.springframework.security.core.context.SecurityContext)5 UserEntity (io.gravitee.management.model.UserEntity)4 Subscription (io.gravitee.repository.management.model.Subscription)4 Test (org.junit.Test)4 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)4 NewExternalUserEntity (io.gravitee.management.model.NewExternalUserEntity)3 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 JWTSigner (com.auth0.jwt.JWTSigner)2 RoleEntity (io.gravitee.management.model.RoleEntity)2 UpdateUserEntity (io.gravitee.management.model.UpdateUserEntity)2 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 Cookie (javax.servlet.http.Cookie)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2