|
| 1 | +package org.owasp.esapi.reference.accesscontrol; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import org.owasp.esapi.AccessControlRule; |
| 6 | +import org.owasp.esapi.AccessController; |
| 7 | +import org.owasp.esapi.ESAPI; |
| 8 | +import org.owasp.esapi.Logger; |
| 9 | +import org.owasp.esapi.errors.AccessControlException; |
| 10 | +import org.owasp.esapi.reference.accesscontrol.policyloader.ACRPolicyFileLoader; |
| 11 | +import org.owasp.esapi.reference.accesscontrol.policyloader.PolicyDTO; |
| 12 | + |
| 13 | +public class ExperimentalAccessController implements AccessController { |
| 14 | + private Map ruleMap; |
| 15 | + |
| 16 | + protected final Logger logger = ESAPI.getLogger("DefaultAccessController"); |
| 17 | + |
| 18 | + public ExperimentalAccessController(Map ruleMap) { |
| 19 | + this.ruleMap = ruleMap; |
| 20 | + } |
| 21 | + public ExperimentalAccessController() throws AccessControlException { |
| 22 | + ACRPolicyFileLoader policyDescriptor = new ACRPolicyFileLoader(); |
| 23 | + PolicyDTO policyDTO = policyDescriptor.load(); |
| 24 | + ruleMap = policyDTO.getAccessControlRules(); |
| 25 | + } |
| 26 | + |
| 27 | + public boolean isAuthorized(Object key, Object runtimeParameter) { |
| 28 | + try { |
| 29 | + AccessControlRule rule = (AccessControlRule)ruleMap.get(key); |
| 30 | + if(rule == null) { |
| 31 | + throw new AccessControlException("Access Denied", |
| 32 | + "AccessControlRule was not found for key: " + key); |
| 33 | + } |
| 34 | + if(logger.isDebugEnabled()){ logger.debug(Logger.EVENT_SUCCESS, "Evaluating Authorization Rule \"" + key + "\" Using class: " + rule.getClass().getCanonicalName()); } |
| 35 | + return rule.isAuthorized(runtimeParameter); |
| 36 | + } catch(Exception e) { |
| 37 | + try { |
| 38 | + //Log the exception by throwing and then catching it. |
| 39 | + //TODO figure out what which string goes where. |
| 40 | + throw new AccessControlException("Access Denied", |
| 41 | + "An unhandled Exception was " + |
| 42 | + "caught, so access is denied.", |
| 43 | + e); |
| 44 | + } catch(AccessControlException ace) { |
| 45 | + //the exception was just logged. There's nothing left to do. |
| 46 | + } |
| 47 | + return false; //fail closed |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public void assertAuthorized(Object key, Object runtimeParameter) |
| 52 | + throws AccessControlException { |
| 53 | + boolean isAuthorized = false; |
| 54 | + try { |
| 55 | + AccessControlRule rule = (AccessControlRule)ruleMap.get(key); |
| 56 | + if(rule == null) { |
| 57 | + throw new AccessControlException("Access Denied", |
| 58 | + "AccessControlRule was not found for key: " + key); |
| 59 | + } |
| 60 | + if(logger.isDebugEnabled()){ logger.debug(Logger.EVENT_SUCCESS, "Asserting Authorization Rule \"" + key + "\" Using class: " + rule.getClass().getCanonicalName()); } |
| 61 | + isAuthorized = rule.isAuthorized(runtimeParameter); |
| 62 | + } catch(Exception e) { |
| 63 | + //TODO figure out what which string goes where. |
| 64 | + throw new AccessControlException("Access Denied", "An unhandled Exception was " + |
| 65 | + "caught, so access is denied." + |
| 66 | + "AccessControlException.", |
| 67 | + e); |
| 68 | + } |
| 69 | + if(!isAuthorized) { |
| 70 | + throw new AccessControlException("Access Denied", |
| 71 | + "Access Denied for key: " + key + |
| 72 | + " runtimeParameter: " + runtimeParameter); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /**** Below this line is legacy support ****/ |
| 77 | + |
| 78 | + /** |
| 79 | + * @param action |
| 80 | + * @param data |
| 81 | + * @throws AccessControlException |
| 82 | + * @see org.owasp.esapi.reference.FileBasedAccessController#assertAuthorizedForData(java.lang.String, java.lang.Object) |
| 83 | + * @deprecated |
| 84 | + */ |
| 85 | + public void assertAuthorizedForData(String action, Object data) |
| 86 | + throws AccessControlException { |
| 87 | + this.assertAuthorized("AC 1.0 Data", new Object[] {action, data}); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param filepath |
| 92 | + * @throws AccessControlException |
| 93 | + * @see org.owasp.esapi.reference.FileBasedAccessController#assertAuthorizedForFile(java.lang.String) |
| 94 | + * @deprecated |
| 95 | + */ |
| 96 | + public void assertAuthorizedForFile(String filepath) |
| 97 | + throws AccessControlException { |
| 98 | + this.assertAuthorized("AC 1.0 File", new Object[] {filepath}); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param functionName |
| 103 | + * @throws AccessControlException |
| 104 | + * @see org.owasp.esapi.reference.FileBasedAccessController#assertAuthorizedForFunction(java.lang.String) |
| 105 | + * @deprecated |
| 106 | + */ |
| 107 | + public void assertAuthorizedForFunction(String functionName) |
| 108 | + throws AccessControlException { |
| 109 | + this.assertAuthorized("AC 1.0 Function", new Object[] {functionName}); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param serviceName |
| 114 | + * @throws AccessControlException |
| 115 | + * @see org.owasp.esapi.reference.FileBasedAccessController#assertAuthorizedForService(java.lang.String) |
| 116 | + * @deprecated |
| 117 | + */ |
| 118 | + public void assertAuthorizedForService(String serviceName) |
| 119 | + throws AccessControlException { |
| 120 | + this.assertAuthorized("AC 1.0 Service", new Object[] {serviceName}); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param url |
| 125 | + * @throws AccessControlException |
| 126 | + * @see org.owasp.esapi.reference.FileBasedAccessController#assertAuthorizedForURL(java.lang.String) |
| 127 | + * @deprecated |
| 128 | + */ |
| 129 | + public void assertAuthorizedForURL(String url) |
| 130 | + throws AccessControlException { |
| 131 | + this.assertAuthorized("AC 1.0 URL", new Object[] {url}); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @param action |
| 136 | + * @param data |
| 137 | + * @return |
| 138 | + * @see org.owasp.esapi.reference.FileBasedAccessController#isAuthorizedForData(java.lang.String, java.lang.Object) |
| 139 | + * @deprecated |
| 140 | + */ |
| 141 | + public boolean isAuthorizedForData(String action, Object data) { |
| 142 | + return this.isAuthorized("AC 1.0 Data", new Object[] {action, data}); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param filepath |
| 147 | + * @return |
| 148 | + * @see org.owasp.esapi.reference.FileBasedAccessController#isAuthorizedForFile(java.lang.String) |
| 149 | + * @deprecated |
| 150 | + */ |
| 151 | + public boolean isAuthorizedForFile(String filepath) { |
| 152 | + return this.isAuthorized("AC 1.0 File", new Object[] {filepath}); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @param functionName |
| 157 | + * @return |
| 158 | + * @see org.owasp.esapi.reference.FileBasedAccessController#isAuthorizedForFunction(java.lang.String) |
| 159 | + * @deprecated |
| 160 | + */ |
| 161 | + public boolean isAuthorizedForFunction(String functionName) { |
| 162 | + return this.isAuthorized("AC 1.0 Function", new Object[] {functionName}); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @param serviceName |
| 167 | + * @return |
| 168 | + * @see org.owasp.esapi.reference.FileBasedAccessController#isAuthorizedForService(java.lang.String) |
| 169 | + * @deprecated |
| 170 | + */ |
| 171 | + public boolean isAuthorizedForService(String serviceName) { |
| 172 | + return this.isAuthorized("AC 1.0 Service", new Object[] {serviceName}); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @param url |
| 177 | + * @return |
| 178 | + * @see org.owasp.esapi.reference.FileBasedAccessController#isAuthorizedForURL(java.lang.String) |
| 179 | + * @deprecated |
| 180 | + */ |
| 181 | + public boolean isAuthorizedForURL(String url) { |
| 182 | + return this.isAuthorized("AC 1.0 URL", new Object[] {url}); |
| 183 | + } |
| 184 | +} |
0 commit comments