Search in sources :

Example 1 with RolesPermitted

use of fish.payara.cdi.auth.roles.RolesPermitted in project Payara by payara.

the class RolesPermittedInterceptor method method.

/**
 * Method invoked whenever a method annotated with @Roles, or a method
 * within a class annotated with @Roles is called.
 *
 * @param invocationContext Context provided by Weld.
 * @return Proceed to next interceptor in chain.
 * @throws java.lang.Exception
 * @throws fish.payara.cdi.auth.roles.CallerAccessException if access is not permitted
 */
@AroundInvoke
public Object method(InvocationContext invocationContext) throws Exception {
    RolesPermitted roles = getRolesPermitted(invocationContext);
    boolean isAccessPermitted = checkAccessPermitted(roles, invocationContext);
    if (!isAccessPermitted) {
        throw new CallerAccessException("Caller was not permitted access to a protected resource");
    }
    return invocationContext.proceed();
}
Also used : RolesPermitted(fish.payara.cdi.auth.roles.RolesPermitted) CallerAccessException(fish.payara.cdi.auth.roles.CallerAccessException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 2 with RolesPermitted

use of fish.payara.cdi.auth.roles.RolesPermitted in project Payara by payara.

the class RolesCDIInterceptor method method.

/**
 * Method invoked whenever a method annotated with @Roles, or a method within a class annotated with @Roles is
 * called.
 *
 * @param invocationContext Context provided by Weld.
 * @return Proceed to next interceptor in chain.
 */
@AroundInvoke
public Object method(InvocationContext invocationContext) throws Exception {
    RolesPermitted roles = getRolesPermitted(invocationContext);
    boolean isAccessPermitted = checkAccessPermitted(roles);
    if (!isAccessPermitted) {
        throw new CallerAccessException("Caller was not permitted access to a protected resource");
    }
    return invocationContext.proceed();
}
Also used : RolesPermitted(fish.payara.cdi.auth.roles.RolesPermitted) CallerAccessException(fish.payara.cdi.auth.roles.CallerAccessException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 3 with RolesPermitted

use of fish.payara.cdi.auth.roles.RolesPermitted in project Payara by payara.

the class RolesPermittedInterceptor method getRolesPermitted.

private RolesPermitted getRolesPermitted(InvocationContext invocationContext) {
    Optional<RolesPermitted> optionalRolesPermitted;
    // Try the Weld bindings first. This gives us the *exact* binding which caused this interceptor being called
    @SuppressWarnings("unchecked") Set<Annotation> bindings = (Set<Annotation>) invocationContext.getContextData().get("org.jboss.weld.interceptor.bindings");
    if (bindings != null) {
        optionalRolesPermitted = bindings.stream().filter(annotation -> annotation.annotationType().equals(RolesPermitted.class)).findAny().map(RolesPermitted.class::cast);
        if (optionalRolesPermitted.isPresent()) {
            return optionalRolesPermitted.get();
        }
    }
    final BeanManager beanManager = lazyProperties.getBeanManager();
    // Failing the Weld binding, check the method first
    optionalRolesPermitted = getAnnotationFromMethod(beanManager, invocationContext.getMethod(), RolesPermitted.class);
    if (optionalRolesPermitted.isPresent()) {
        return optionalRolesPermitted.get();
    }
    // If nothing found on the method, check the the bean class
    optionalRolesPermitted = getAnnotation(beanManager, interceptedBean.getBeanClass(), RolesPermitted.class);
    if (optionalRolesPermitted.isPresent()) {
        return optionalRolesPermitted.get();
    }
    // find it signals a critical error.
    throw new IllegalStateException("@RolesPermitted not found on " + interceptedBean.getBeanClass());
}
Also used : InvocationContext(javax.interceptor.InvocationContext) AND(fish.payara.cdi.auth.roles.LogicalOperator.AND) Intercepted(javax.enterprise.inject.Intercepted) Inject(javax.inject.Inject) Interceptor(javax.interceptor.Interceptor) SecurityContext(javax.security.enterprise.SecurityContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) OR(fish.payara.cdi.auth.roles.LogicalOperator.OR) NOT_DONE(javax.security.enterprise.AuthenticationStatus.NOT_DONE) Arrays.asList(java.util.Arrays.asList) Parameter(java.lang.reflect.Parameter) Named(javax.inject.Named) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) AuthenticationParameters.withParams(javax.security.enterprise.authentication.mechanism.http.AuthenticationParameters.withParams) AuthenticationStatus(javax.security.enterprise.AuthenticationStatus) SEND_FAILURE(javax.security.enterprise.AuthenticationStatus.SEND_FAILURE) Context(javax.ws.rs.core.Context) AnnotationELPProcessor.evalELExpression(org.glassfish.soteria.cdi.AnnotationELPProcessor.evalELExpression) HttpServletResponse(javax.servlet.http.HttpServletResponse) AnnotationELPProcessor.hasAnyELExpression(org.glassfish.soteria.cdi.AnnotationELPProcessor.hasAnyELExpression) Set(java.util.Set) ELProcessor(javax.el.ELProcessor) Serializable(java.io.Serializable) CallerAccessException(fish.payara.cdi.auth.roles.CallerAccessException) Priority(javax.annotation.Priority) SUCCESS(javax.security.enterprise.AuthenticationStatus.SUCCESS) List(java.util.List) Response(javax.ws.rs.core.Response) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) AroundInvoke(javax.interceptor.AroundInvoke) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) Queue(java.util.Queue) CdiUtils.getAnnotation(org.glassfish.soteria.cdi.CdiUtils.getAnnotation) RolesPermitted(fish.payara.cdi.auth.roles.RolesPermitted) Bean(javax.enterprise.inject.spi.Bean) BeanManager(javax.enterprise.inject.spi.BeanManager) Set(java.util.Set) RolesPermitted(fish.payara.cdi.auth.roles.RolesPermitted) BeanManager(javax.enterprise.inject.spi.BeanManager) Annotation(java.lang.annotation.Annotation) CdiUtils.getAnnotation(org.glassfish.soteria.cdi.CdiUtils.getAnnotation)

Aggregations

CallerAccessException (fish.payara.cdi.auth.roles.CallerAccessException)3 RolesPermitted (fish.payara.cdi.auth.roles.RolesPermitted)3 AroundInvoke (javax.interceptor.AroundInvoke)3 AND (fish.payara.cdi.auth.roles.LogicalOperator.AND)1 OR (fish.payara.cdi.auth.roles.LogicalOperator.OR)1 Serializable (java.io.Serializable)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Arrays.asList (java.util.Arrays.asList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Optional (java.util.Optional)1 Queue (java.util.Queue)1 Set (java.util.Set)1 Priority (javax.annotation.Priority)1 ELProcessor (javax.el.ELProcessor)1 Intercepted (javax.enterprise.inject.Intercepted)1 Bean (javax.enterprise.inject.spi.Bean)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1