1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.servlet.core;
20
21 import io.undertow.security.idm.Account;
22 import io.undertow.servlet.api.AuthorizationManager;
23 import io.undertow.servlet.api.Deployment;
24 import io.undertow.servlet.api.SecurityInfo;
25 import io.undertow.servlet.api.SecurityRoleRef;
26 import io.undertow.servlet.api.ServletInfo;
27 import io.undertow.servlet.api.TransportGuaranteeType;
28 import io.undertow.servlet.api.SingleConstraintMatch;
29
30 import javax.servlet.http.HttpServletRequest;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34
35 /**
36  * Default authorization manager that simply implements the rules as specified by the servlet spec
37  *
38  * @author Stuart Douglas
39  */

40 public class DefaultAuthorizationManager implements AuthorizationManager {
41
42     public static final DefaultAuthorizationManager INSTANCE = new DefaultAuthorizationManager();
43
44     private DefaultAuthorizationManager() {
45     }
46
47     @Override
48     public boolean isUserInRole(String role, Account account, ServletInfo servletInfo, HttpServletRequest request, Deployment deployment) {
49
50         final Map<String, Set<String>> principalVersusRolesMap = deployment.getDeploymentInfo().getPrincipalVersusRolesMap();
51         final Set<String> roles = principalVersusRolesMap.get(account.getPrincipal().getName());
52         //TODO: a more efficient imple
53         for (SecurityRoleRef ref : servletInfo.getSecurityRoleRefs()) {
54             if (ref.getRole().equals(role)) {
55                 if (roles != null && roles.contains(ref.getLinkedRole())) {
56                     return true;
57                 }
58                 return account.getRoles().contains(ref.getLinkedRole());
59             }
60         }
61         if (roles != null && roles.contains(role)) {
62             return true;
63         }
64         return account.getRoles().contains(role);
65     }
66
67     @Override
68     public boolean canAccessResource(List<SingleConstraintMatch> constraints, Account account, ServletInfo servletInfo, HttpServletRequest request, Deployment deployment) {
69         if (constraints == null || constraints.isEmpty()) {
70             return true;
71         }
72         for (final SingleConstraintMatch constraint : constraints) {
73
74             boolean found = false;
75
76             Set<String> roleSet = constraint.getRequiredRoles();
77             if (roleSet.isEmpty() && constraint.getEmptyRoleSemantic() != SecurityInfo.EmptyRoleSemantic.DENY) {
78                     /*
79                      * The EmptyRoleSemantic was either PERMIT or AUTHENTICATE, either way a roles check is not needed.
80                      */

81                 found = true;
82             } else if (account != null) {
83                 if(roleSet.contains("**") && !deployment.getDeploymentInfo().getSecurityRoles().contains("**")) {
84                     found = true;
85                 } else {
86                     final Set<String> roles = deployment.getDeploymentInfo().getPrincipalVersusRolesMap().get(account.getPrincipal().getName());
87
88                     for (String role : roleSet) {
89                         if (roles != null) {
90                             if (roles.contains(role)) {
91                                 found = true;
92                                 break;
93                             }
94                         }
95                         if (account.getRoles().contains(role)) {
96                             found = true;
97                             break;
98                         }
99                     }
100                 }
101             }
102             if (!found) {
103                 return false;
104             }
105         }
106         return true;
107
108     }
109
110     @Override
111     public TransportGuaranteeType transportGuarantee(TransportGuaranteeType currentConnectionGuarantee, TransportGuaranteeType configuredRequiredGuarentee, HttpServletRequest request) {
112         return configuredRequiredGuarentee;
113     }
114 }
115