use of jakarta.annotation.security.RolesAllowed in project minijax by minijax.
the class MinijaxApplication method checkSecurity.
private void checkSecurity(final MinijaxRequestContext context) {
final Annotation a = context.getResourceMethod().getSecurityAnnotation();
if (a == null) {
return;
}
final Class<?> c = a.annotationType();
if (c == PermitAll.class) {
return;
}
if (c == DenyAll.class) {
throw new ForbiddenException();
}
if (c == RolesAllowed.class) {
final SecurityContext security = context.getSecurityContext();
if (security == null || security.getUserPrincipal() == null) {
throw new NotAuthorizedException(Response.status(Status.UNAUTHORIZED).build());
}
boolean found = false;
for (final String role : ((RolesAllowed) a).value()) {
if (security.isUserInRole(role)) {
found = true;
break;
}
}
if (!found) {
throw new ForbiddenException();
}
}
}
use of jakarta.annotation.security.RolesAllowed in project resteasy by resteasy.
the class EJBConstraintChecker method checkInternal.
private boolean checkInternal(Method method) {
// From now on we can use this class since it's there. I (Stef Epardaud) don't think we need to
// remove the reference here and use reflection.
RolesAllowed rolesAllowed = method.getAnnotation(RolesAllowed.class);
if (rolesAllowed == null) {
return true;
}
SecurityContext context = ResteasyContext.getContextData(SecurityContext.class);
for (String role : rolesAllowed.value()) {
if (context.isUserInRole(role)) {
return true;
}
}
return false;
}
use of jakarta.annotation.security.RolesAllowed in project helidon by oracle.
the class RoleValidator method fromAnnotations.
@Override
public RoleConfig fromAnnotations(EndpointConfig endpointConfig) {
RoleConfig.Builder builder = RoleConfig.builder();
for (SecurityLevel securityLevel : endpointConfig.securityLevels()) {
for (EndpointConfig.AnnotationScope scope : EndpointConfig.AnnotationScope.values()) {
// Order of the annotations matters because of annotation handling.
List<Annotation> annotations = new ArrayList<>();
for (Class<? extends Annotation> annotation : supportedAnnotations()) {
annotations.addAll(securityLevel.filterAnnotations(annotation, scope));
}
List<String> roles = new ArrayList<>();
List<String> serviceRoles = new ArrayList<>();
for (Annotation annotation : annotations) {
if (annotation instanceof RolesAllowed) {
// these are user roles by default
roles.addAll(Arrays.asList(((RolesAllowed) annotation).value()));
builder.permitAll(false);
builder.denyAll(false);
} else if (annotation instanceof Roles) {
// these are configured
Roles r = (Roles) annotation;
if (r.subjectType() == SubjectType.USER) {
roles.addAll(Arrays.asList(r.value()));
} else {
serviceRoles.addAll(Arrays.asList(r.value()));
}
builder.permitAll(false);
builder.denyAll(false);
} else if (annotation instanceof PermitAll) {
builder.permitAll(true);
builder.denyAll(false);
} else if (annotation instanceof DenyAll) {
builder.permitAll(false);
builder.denyAll(true);
}
}
if (!roles.isEmpty()) {
builder.clearRoles().addRoles(roles);
}
if (!serviceRoles.isEmpty()) {
builder.clearServiceRoles().addServiceRoles(serviceRoles);
}
}
}
return builder.build();
}
use of jakarta.annotation.security.RolesAllowed in project helidon by oracle.
the class RoleValidatorTest method testDenyAllAndRoles.
@Test
void testDenyAllAndRoles() {
RoleValidator validator = RoleValidator.create();
DenyAll denyAll = mock(DenyAll.class);
RolesAllowed rolesAllowed = mock(RolesAllowed.class);
String[] roleArray = new String[] { "admin" };
when(rolesAllowed.value()).thenReturn(roleArray);
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
EndpointConfig ep = mock(EndpointConfig.class);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(DenyAll.class, EndpointConfig.AnnotationScope.CLASS)).thenReturn(List.of(denyAll));
when(classSecurityLevel.filterAnnotations(RolesAllowed.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(rolesAllowed));
RoleValidator.RoleConfig rConfig = validator.fromAnnotations(ep);
Errors.Collector collector = Errors.collector();
ProviderRequest request = mock(ProviderRequest.class);
when(request.subject()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("myAdmin")).addGrant(Role.create("admin")).build()));
when(request.service()).thenReturn(Optional.empty());
validator.validate(rConfig, collector, request);
collector.collect().checkValid();
}
use of jakarta.annotation.security.RolesAllowed in project helidon by oracle.
the class RoleValidatorTest method testRolesAllowedPermit.
@Test
void testRolesAllowedPermit() {
RoleValidator validator = RoleValidator.create();
RolesAllowed annot = mock(RolesAllowed.class);
String[] roleArray = new String[] { "admin" };
when(annot.value()).thenReturn(roleArray);
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
EndpointConfig ep = mock(EndpointConfig.class);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(RolesAllowed.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(annot));
RoleValidator.RoleConfig rConfig = validator.fromAnnotations(ep);
Errors.Collector collector = Errors.collector();
ProviderRequest request = mock(ProviderRequest.class);
when(request.subject()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("myAdmin")).addGrant(Role.create("admin")).build()));
when(request.service()).thenReturn(Optional.empty());
validator.validate(rConfig, collector, request);
collector.collect().checkValid();
}
Aggregations