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