|
| 1 | +/** |
| 2 | + * ContainerProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2021 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.containerproxy.service; |
| 22 | + |
| 23 | +import eu.openanalytics.containerproxy.auth.IAuthenticationBackend; |
| 24 | +import eu.openanalytics.containerproxy.model.spec.ProxyAccessControl; |
| 25 | +import eu.openanalytics.containerproxy.model.spec.ProxySpec; |
| 26 | +import eu.openanalytics.containerproxy.spec.IProxySpecProvider; |
| 27 | +import org.springframework.context.annotation.Lazy; |
| 28 | +import org.springframework.security.authentication.AnonymousAuthenticationToken; |
| 29 | +import org.springframework.security.core.Authentication; |
| 30 | +import org.springframework.stereotype.Service; |
| 31 | + |
| 32 | +import javax.inject.Inject; |
| 33 | + |
| 34 | +@Service |
| 35 | +public class AccessControlService { |
| 36 | + |
| 37 | + @Lazy |
| 38 | + @Inject |
| 39 | + private IAuthenticationBackend authBackend; |
| 40 | + |
| 41 | + @Inject |
| 42 | + private UserService userService; |
| 43 | + |
| 44 | + @Inject |
| 45 | + private IProxySpecProvider specProvider; |
| 46 | + |
| 47 | + public boolean canAccess(Authentication auth, String specId) { |
| 48 | + return canAccess(auth, specProvider.getSpec(specId)); |
| 49 | + } |
| 50 | + |
| 51 | + public boolean canAccess(Authentication auth, ProxySpec spec) { |
| 52 | + if (auth == null || spec == null) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + if (auth instanceof AnonymousAuthenticationToken && !authBackend.hasAuthorization()) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + if (specHasNoAccessControl(spec.getAccessControl())) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + if (allowedByGroups(auth, spec)) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + if (allowedByUsers(auth, spec)) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + if (allowedByExpression(auth, spec)) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + public boolean specHasNoAccessControl(ProxyAccessControl accessControl) { |
| 80 | + if (accessControl == null) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + return !accessControl.hasGroupAccess() |
| 85 | + && !accessControl.hasUserAccess() |
| 86 | + && !accessControl.hasExpressionAccess(); |
| 87 | + } |
| 88 | + |
| 89 | + public boolean allowedByGroups(Authentication auth, ProxySpec spec) { |
| 90 | + if (!spec.getAccessControl().hasGroupAccess()) { |
| 91 | + // no groups defined -> this user has no access based on the groups |
| 92 | + return false; |
| 93 | + } |
| 94 | + for (String group : spec.getAccessControl().getGroups()) { |
| 95 | + if (userService.isMember(auth, group)) { |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + public boolean allowedByUsers(Authentication auth, ProxySpec spec) { |
| 103 | + if (!spec.getAccessControl().hasUserAccess()) { |
| 104 | + // no users defined -> this user has no access based on the users |
| 105 | + return false; |
| 106 | + } |
| 107 | + for (String user : spec.getAccessControl().getUsers()) { |
| 108 | + if (auth.getName().equals(user)) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + } |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + public boolean allowedByExpression(Authentication auth, ProxySpec spec) { |
| 116 | + if (!spec.getAccessControl().hasExpressionAccess()) { |
| 117 | + // no expression defined -> this user has no access based on the expression |
| 118 | + return false; |
| 119 | + } |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments