|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.web.authentication; |
| 18 | + |
| 19 | +import java.util.Optional; |
| 20 | +import javax.servlet.http.HttpServletRequest; |
| 21 | + |
| 22 | +import org.springframework.core.convert.converter.Converter; |
| 23 | +import org.springframework.security.authentication.AuthenticationManager; |
| 24 | +import org.springframework.security.authentication.AuthenticationManagerResolver; |
| 25 | +import org.springframework.util.Assert; |
| 26 | +import org.springframework.web.util.UriComponents; |
| 27 | +import org.springframework.web.util.UriComponentsBuilder; |
| 28 | + |
| 29 | +/** |
| 30 | + * An implementation of {@link AuthenticationManagerResolver} that separates the tasks of |
| 31 | + * extracting the request's tenant identifier and looking up an {@link AuthenticationManager} |
| 32 | + * by that tenant identifier. |
| 33 | + * |
| 34 | + * @author Josh Cummings |
| 35 | + * @since 5.2 |
| 36 | + * @see AuthenticationManagerResolver |
| 37 | + */ |
| 38 | +public final class MultiTenantAuthenticationManagerResolver<T> implements AuthenticationManagerResolver<HttpServletRequest> { |
| 39 | + |
| 40 | + private final Converter<HttpServletRequest, AuthenticationManager> authenticationManagerResolver; |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructs a {@link MultiTenantAuthenticationManagerResolver} with the provided parameters |
| 44 | + * |
| 45 | + * @param tenantResolver |
| 46 | + * @param authenticationManagerResolver |
| 47 | + */ |
| 48 | + public MultiTenantAuthenticationManagerResolver |
| 49 | + (Converter<HttpServletRequest, T> tenantResolver, |
| 50 | + Converter<T, AuthenticationManager> authenticationManagerResolver) { |
| 51 | + |
| 52 | + Assert.notNull(tenantResolver, "tenantResolver cannot be null"); |
| 53 | + Assert.notNull(authenticationManagerResolver, "authenticationManagerResolver cannot be null"); |
| 54 | + |
| 55 | + this.authenticationManagerResolver = request -> { |
| 56 | + Optional<T> context = Optional.ofNullable(tenantResolver.convert(request)); |
| 57 | + return context.map(authenticationManagerResolver::convert) |
| 58 | + .orElseThrow(() -> new IllegalArgumentException |
| 59 | + ("Could not resolve AuthenticationManager by reference " + context.orElse(null))); |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public AuthenticationManager resolve(HttpServletRequest context) { |
| 65 | + return this.authenticationManagerResolver.convert(context); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates an {@link AuthenticationManagerResolver} that will use a hostname's first label as |
| 70 | + * the resolution key for the underlying {@link AuthenticationManagerResolver}. |
| 71 | + * |
| 72 | + * For example, you might have a set of {@link AuthenticationManager}s defined like so: |
| 73 | + * |
| 74 | + * <pre> |
| 75 | + * Map<String, AuthenticationManager> authenticationManagers = new HashMap<>(); |
| 76 | + * authenticationManagers.put("tenantOne", managerOne()); |
| 77 | + * authenticationManagers.put("tenantTwo", managerTwo()); |
| 78 | + * </pre> |
| 79 | + * |
| 80 | + * And that your system serves hostnames like <pre>https://tenantOne.example.org</pre>. |
| 81 | + * |
| 82 | + * Then, you could create an {@link AuthenticationManagerResolver} that uses the "tenantOne" value from |
| 83 | + * the hostname to resolve Tenant One's {@link AuthenticationManager} like so: |
| 84 | + * |
| 85 | + * <pre> |
| 86 | + * AuthenticationManagerResolver<HttpServletRequest> resolver = |
| 87 | + * resolveFromSubdomain(authenticationManagers::get); |
| 88 | + * </pre> |
| 89 | + * |
| 90 | + * {@link HttpServletRequest} |
| 91 | + * @param resolver A {@link String}-resolving {@link AuthenticationManagerResolver} |
| 92 | + * @return A hostname-resolving {@link AuthenticationManagerResolver} |
| 93 | + */ |
| 94 | + public static AuthenticationManagerResolver<HttpServletRequest> |
| 95 | + resolveFromSubdomain(Converter<String, AuthenticationManager> resolver) { |
| 96 | + |
| 97 | + return new MultiTenantAuthenticationManagerResolver<>(request -> |
| 98 | + Optional.ofNullable(request.getServerName()) |
| 99 | + .map(host -> host.split("\\.")) |
| 100 | + .filter(segments -> segments.length > 0) |
| 101 | + .map(segments -> segments[0]).orElse(null), resolver); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Creates an {@link AuthenticationManagerResolver} that will use a request path's first segment as |
| 106 | + * the resolution key for the underlying {@link AuthenticationManagerResolver}. |
| 107 | + * |
| 108 | + * For example, you might have a set of {@link AuthenticationManager}s defined like so: |
| 109 | + * |
| 110 | + * <pre> |
| 111 | + * Map<String, AuthenticationManager> authenticationManagers = new HashMap<>(); |
| 112 | + * authenticationManagers.put("tenantOne", managerOne()); |
| 113 | + * authenticationManagers.put("tenantTwo", managerTwo()); |
| 114 | + * </pre> |
| 115 | + * |
| 116 | + * And that your system serves requests like <pre>https://example.org/tenantOne</pre>. |
| 117 | + * |
| 118 | + * Then, you could create an {@link AuthenticationManagerResolver} that uses the "tenantOne" value from |
| 119 | + * the request to resolve Tenant One's {@link AuthenticationManager} like so: |
| 120 | + * |
| 121 | + * <pre> |
| 122 | + * AuthenticationManagerResolver<HttpServletRequest> resolver = |
| 123 | + * resolveFromPath(authenticationManagers::get); |
| 124 | + * </pre> |
| 125 | + * |
| 126 | + * {@link HttpServletRequest} |
| 127 | + * @param resolver A {@link String}-resolving {@link AuthenticationManagerResolver} |
| 128 | + * @return A path-resolving {@link AuthenticationManagerResolver} |
| 129 | + */ |
| 130 | + public static AuthenticationManagerResolver<HttpServletRequest> |
| 131 | + resolveFromPath(Converter<String, AuthenticationManager> resolver) { |
| 132 | + |
| 133 | + return new MultiTenantAuthenticationManagerResolver<>(request -> |
| 134 | + Optional.ofNullable(request.getRequestURI()) |
| 135 | + .map(UriComponentsBuilder::fromUriString) |
| 136 | + .map(UriComponentsBuilder::build) |
| 137 | + .map(UriComponents::getPathSegments) |
| 138 | + .filter(segments -> !segments.isEmpty()) |
| 139 | + .map(segments -> segments.get(0)).orElse(null), resolver); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Creates an {@link AuthenticationManagerResolver} that will use a request headers's value as |
| 144 | + * the resolution key for the underlying {@link AuthenticationManagerResolver}. |
| 145 | + * |
| 146 | + * For example, you might have a set of {@link AuthenticationManager}s defined like so: |
| 147 | + * |
| 148 | + * <pre> |
| 149 | + * Map<String, AuthenticationManager> authenticationManagers = new HashMap<>(); |
| 150 | + * authenticationManagers.put("tenantOne", managerOne()); |
| 151 | + * authenticationManagers.put("tenantTwo", managerTwo()); |
| 152 | + * </pre> |
| 153 | + * |
| 154 | + * And that your system serves requests with a header like <pre>X-Tenant-Id: tenantOne</pre>. |
| 155 | + * |
| 156 | + * Then, you could create an {@link AuthenticationManagerResolver} that uses the "tenantOne" value from |
| 157 | + * the request to resolve Tenant One's {@link AuthenticationManager} like so: |
| 158 | + * |
| 159 | + * <pre> |
| 160 | + * AuthenticationManagerResolver<HttpServletRequest> resolver = |
| 161 | + * resolveFromHeader("X-Tenant-Id", authenticationManagers::get); |
| 162 | + * </pre> |
| 163 | + * |
| 164 | + * {@link HttpServletRequest} |
| 165 | + * @param resolver A {@link String}-resolving {@link AuthenticationManagerResolver} |
| 166 | + * @return A header-resolving {@link AuthenticationManagerResolver} |
| 167 | + */ |
| 168 | + public static AuthenticationManagerResolver<HttpServletRequest> |
| 169 | + resolveFromHeader(String headerName, Converter<String, AuthenticationManager> resolver) { |
| 170 | + |
| 171 | + return new MultiTenantAuthenticationManagerResolver<> |
| 172 | + (request -> request.getHeader(headerName), resolver); |
| 173 | + } |
| 174 | +} |
0 commit comments