1
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
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
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
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