View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.security;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.annotation.security.DenyAll;
26  import javax.annotation.security.PermitAll;
27  import javax.annotation.security.RolesAllowed;
28  import javax.faces.context.FacesContext;
29  import java.lang.annotation.Annotation;
30  import java.lang.reflect.AnnotatedElement;
31  import java.lang.reflect.Method;
32  import java.util.Arrays;
33  import java.util.Map;
34  import java.util.concurrent.ConcurrentHashMap;
35  
36  public class AuthorizationUtils {
37    private static final Logger LOG = LoggerFactory.getLogger(AuthorizationUtils.class);
38  
39    private static final Object NULL_VALUE = new Object();
40    private static final Map<String, Object> AUTHORISATION_CACHE = new ConcurrentHashMap<String, Object>();
41  
42    private AuthorizationUtils() {
43    }
44  
45    public static boolean isAuthorized(final FacesContext facesContext, final String expression) {
46  
47      final Annotation securityAnnotation = getSecurityAnnotation(facesContext, expression);
48      if (securityAnnotation == null) {
49        return true;
50      }
51  
52      if (securityAnnotation instanceof DenyAll) {
53        if (LOG.isDebugEnabled()) {
54          LOG.debug("DenyAll");
55        }
56        return false;
57      }
58      if (securityAnnotation instanceof RolesAllowed) {
59        final String [] roles = ((RolesAllowed) securityAnnotation).value();
60        if (LOG.isDebugEnabled()) {
61          LOG.debug("RolesAllowed " + Arrays.asList(((RolesAllowed) securityAnnotation).value()));
62        }
63        for (final String role : roles) {
64          final boolean authorised = facesContext.getExternalContext().isUserInRole(role);
65          if (authorised) {
66            return true;
67          }
68        }
69        return false;
70      }
71      if (securityAnnotation instanceof PermitAll) {
72        if (LOG.isDebugEnabled()) {
73          LOG.debug("PermitAll");
74        }
75        return true;
76      }
77      return true;
78    }
79  
80    private static Annotation getSecurityAnnotations(final AnnotatedElement annotatedElement) {
81      Annotation annotation = annotatedElement.getAnnotation(RolesAllowed.class);
82      if (annotation != null) {
83        return annotation;
84      }
85      annotation = annotatedElement.getAnnotation(DenyAll.class);
86      if (annotation != null) {
87        return annotation;
88      }
89      annotation = annotatedElement.getAnnotation(PermitAll.class);
90      if (annotation != null) {
91        return annotation;
92      }
93      return null;
94    }
95  
96    private static Annotation getSecurityAnnotation(final FacesContext facesContext, String expression) {
97      if (AUTHORISATION_CACHE.containsKey(expression)) {
98        final Object obj = AUTHORISATION_CACHE.get(expression);
99        if (obj instanceof Annotation) {
100         return (Annotation) obj;
101       }
102       return null;
103     } else {
104       Annotation securityAnnotation = null;
105       if (expression.startsWith("#{") && expression.endsWith("}")) {
106         expression = expression.substring(2, expression.length()-1);
107         final int index = expression.lastIndexOf('.');
108         if (index != -1) {
109           final String methodExpression = expression.substring(index+1, expression.length());
110           final String beanExpression = expression.substring(0, index);
111           // TODO find a better way
112           final Object bean =
113               facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, beanExpression);
114           if (bean != null) {
115             try {
116               final Method method = bean.getClass().getMethod(methodExpression);
117               securityAnnotation = getSecurityAnnotations(method);
118               if (securityAnnotation == null) {
119                 securityAnnotation = getSecurityAnnotations(bean.getClass());
120               }
121             } catch (final NoSuchMethodException e) {
122               LOG.error("No Method " + methodExpression + " in class " + bean.getClass(), e);
123             }
124           }
125         }
126       }
127       if (securityAnnotation != null) {
128         AUTHORISATION_CACHE.put(expression, securityAnnotation);
129       } else {
130         AUTHORISATION_CACHE.put(expression, NULL_VALUE);
131       }
132       return securityAnnotation;
133     }
134   }
135 }
136