|
33 | 33 | import org.springframework.security.web.authentication.logout.LogoutHandler;
|
34 | 34 | import org.springframework.security.web.firewall.StrictHttpFirewall;
|
35 | 35 | import org.springframework.security.web.util.matcher.RegexRequestMatcher;
|
36 |
| -import org.zowe.apiml.filter.SecureConnectionFilter; |
37 | 36 | import org.zowe.apiml.filter.AttlsFilter;
|
| 37 | +import org.zowe.apiml.filter.SecureConnectionFilter; |
38 | 38 | import org.zowe.apiml.gateway.controllers.AuthController;
|
39 | 39 | import org.zowe.apiml.gateway.controllers.CacheServiceController;
|
40 | 40 | import org.zowe.apiml.gateway.error.InternalServerErrorController;
|
41 | 41 | import org.zowe.apiml.gateway.security.login.x509.X509AuthenticationProvider;
|
42 |
| -import org.zowe.apiml.gateway.security.query.QueryFilter; |
43 |
| -import org.zowe.apiml.gateway.security.query.SuccessfulQueryHandler; |
44 |
| -import org.zowe.apiml.gateway.security.query.TokenAuthenticationProvider; |
| 42 | +import org.zowe.apiml.gateway.security.query.*; |
| 43 | +import org.zowe.apiml.gateway.security.refresh.SuccessfulRefreshHandler; |
45 | 44 | import org.zowe.apiml.gateway.security.service.AuthenticationService;
|
46 | 45 | import org.zowe.apiml.gateway.security.ticket.SuccessfulTicketHandler;
|
47 | 46 | import org.zowe.apiml.gateway.services.ServicesInfoController;
|
48 |
| -import org.zowe.apiml.security.common.config.AuthConfigurationProperties; |
49 |
| -import org.zowe.apiml.security.common.config.CertificateAuthenticationProvider; |
50 |
| -import org.zowe.apiml.security.common.config.HandlerInitializer; |
| 47 | +import org.zowe.apiml.security.common.config.*; |
51 | 48 | import org.zowe.apiml.security.common.content.BasicContentFilter;
|
52 | 49 | import org.zowe.apiml.security.common.content.CookieContentFilter;
|
53 | 50 | import org.zowe.apiml.security.common.filter.ApimlX509Filter;
|
@@ -86,6 +83,7 @@ public class NewSecurityConfiguration {
|
86 | 83 | private final HandlerInitializer handlerInitializer;
|
87 | 84 | private final SuccessfulQueryHandler successfulQueryHandler;
|
88 | 85 | private final SuccessfulTicketHandler successfulTicketHandler;
|
| 86 | + private final SuccessfulRefreshHandler successfulRefreshHandler; |
89 | 87 | @Qualifier("publicKeyCertificatesBase64")
|
90 | 88 | private final Set<String> publicKeyCertificatesBase64;
|
91 | 89 | private final X509AuthenticationProvider x509AuthenticationProvider;
|
@@ -178,7 +176,7 @@ private LogoutHandler logoutHandler() {
|
178 | 176 | }
|
179 | 177 |
|
180 | 178 | /**
|
181 |
| - * Query and Ticket endpoints share single filter that handles auth with and without certificate. This logic is encapsulated in the queryFilter or ticketFilter. |
| 179 | + * Query and Ticket and Refresh endpoints share single filter that handles auth with and without certificate. This logic is encapsulated in the queryFilter or ticketFilter. |
182 | 180 | * Query endpoint does not require certificate to be present in RequestContext. It verifies JWT token.
|
183 | 181 | */
|
184 | 182 | @Configuration
|
@@ -218,7 +216,7 @@ private QueryFilter queryFilter(String queryEndpoint, AuthenticationManager auth
|
218 | 216 | }
|
219 | 217 |
|
220 | 218 | /**
|
221 |
| - * Query and Ticket endpoints share single filter that handles auth with and without certificate. This logic is encapsulated in the queryFilter or ticketFilter. |
| 219 | + * Query and Ticket and Refresh endpoints share single filter that handles auth with and without certificate. This logic is encapsulated in the queryFilter or ticketFilter. |
222 | 220 | * Ticket endpoint does require certificate to be present in RequestContext. It verifies the JWT token.
|
223 | 221 | */
|
224 | 222 |
|
@@ -262,6 +260,52 @@ private QueryFilter ticketFilter(String ticketEndpoint, AuthenticationManager au
|
262 | 260 | }
|
263 | 261 | }
|
264 | 262 |
|
| 263 | + /** |
| 264 | + * Query and Ticket and Refresh endpoints share single filter that handles auth with and without certificate. |
| 265 | + * Refresh endpoint does require certificate to be present in RequestContext. It verifies the JWT token and based |
| 266 | + * on valid token and client certificate issues a new valid token |
| 267 | + */ |
| 268 | + @Configuration |
| 269 | + @RequiredArgsConstructor |
| 270 | + @ConditionalOnProperty(name = "apiml.security.allowTokenRefresh", havingValue = "true", matchIfMissing = false) |
| 271 | + @Order(6) |
| 272 | + class Refresh extends WebSecurityConfigurerAdapter { |
| 273 | + |
| 274 | + private final AuthenticationProvider tokenAuthenticationProvider; |
| 275 | + |
| 276 | + @Override |
| 277 | + protected void configure(AuthenticationManagerBuilder auth) { |
| 278 | + auth.authenticationProvider(tokenAuthenticationProvider); // for authenticating Tokens |
| 279 | + } |
| 280 | + |
| 281 | + @Override |
| 282 | + protected void configure(HttpSecurity http) throws Exception { |
| 283 | + baseConfigure(http.requestMatchers().antMatchers( |
| 284 | + authConfigurationProperties.getGatewayRefreshEndpointNewFormat(), |
| 285 | + authConfigurationProperties.getGatewayRefreshEndpointOldFormat() |
| 286 | + ).and()).authorizeRequests() |
| 287 | + .anyRequest().authenticated() |
| 288 | + .and() |
| 289 | + .logout().disable() // logout filter in this chain not needed |
| 290 | + .x509() //default x509 filter, authenticates trusted cert, ticketFilter(..) depends on this |
| 291 | + .subjectPrincipalRegex(EXTRACT_USER_PRINCIPAL_FROM_COMMON_NAME) |
| 292 | + .userDetailsService(new SimpleUserDetailService()) |
| 293 | + .and() |
| 294 | + .addFilterBefore(refreshFilter("/**", authenticationManager()), UsernamePasswordAuthenticationFilter.class); |
| 295 | + } |
| 296 | + |
| 297 | + private QueryFilter refreshFilter(String ticketEndpoint, AuthenticationManager authenticationManager) { |
| 298 | + return new QueryFilter( |
| 299 | + ticketEndpoint, |
| 300 | + successfulRefreshHandler, |
| 301 | + handlerInitializer.getAuthenticationFailureHandler(), |
| 302 | + authenticationService, |
| 303 | + HttpMethod.POST, |
| 304 | + true, |
| 305 | + authenticationManager); |
| 306 | + } |
| 307 | + } |
| 308 | + |
265 | 309 | /**
|
266 | 310 | * Endpoints which are protected by client certificate
|
267 | 311 | * Default Spring security x509 filter authenticates any trusted certificate
|
|
0 commit comments