|
| 1 | +/* |
| 2 | + * Copyright 2020-2020 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.cloud.gcp.autoconfigure.security; |
| 18 | + |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 24 | +import org.springframework.boot.autoconfigure.AutoConfigureBefore; |
| 25 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 26 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 27 | +import org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration; |
| 28 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 29 | +import org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration; |
| 30 | +import org.springframework.cloud.gcp.core.GcpProjectIdProvider; |
| 31 | +import org.springframework.cloud.gcp.security.firebase.FirebaseJwtTokenDecoder; |
| 32 | +import org.springframework.cloud.gcp.security.firebase.FirebaseTokenValidator; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | +import org.springframework.http.client.SimpleClientHttpRequestFactory; |
| 36 | +import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator; |
| 37 | +import org.springframework.security.oauth2.core.OAuth2TokenValidator; |
| 38 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 39 | +import org.springframework.security.oauth2.jwt.JwtDecoder; |
| 40 | +import org.springframework.security.oauth2.jwt.JwtIssuerValidator; |
| 41 | +import org.springframework.security.oauth2.jwt.JwtTimestampValidator; |
| 42 | +import org.springframework.web.client.RestOperations; |
| 43 | +import org.springframework.web.client.RestTemplate; |
| 44 | + |
| 45 | +/** |
| 46 | + * |
| 47 | + * @author Vinicius Carvalho |
| 48 | + * @since 1.3 |
| 49 | + */ |
| 50 | +@Configuration |
| 51 | +@ConditionalOnProperty(value = "spring.cloud.gcp.security.firebase.enabled", matchIfMissing = true) |
| 52 | +@AutoConfigureBefore(OAuth2ResourceServerAutoConfiguration.class) |
| 53 | +@AutoConfigureAfter(GcpContextAutoConfiguration.class) |
| 54 | +@EnableConfigurationProperties(FirebaseAuthenticationProperties.class) |
| 55 | +public class FirebaseAuthenticationAutoConfiguration { |
| 56 | + |
| 57 | + private static final String ISSUER_TEMPLATE = "https://securetoken.google.com/%s"; |
| 58 | + |
| 59 | + @Bean |
| 60 | + @ConditionalOnMissingBean(name = "firebaseJwtDelegatingValidator") |
| 61 | + public DelegatingOAuth2TokenValidator<Jwt> firebaseJwtDelegatingValidator(JwtIssuerValidator jwtIssuerValidator, GcpProjectIdProvider gcpProjectIdProvider) { |
| 62 | + List<OAuth2TokenValidator<Jwt>> validators = new ArrayList<>(); |
| 63 | + validators.add(new JwtTimestampValidator()); |
| 64 | + validators.add(jwtIssuerValidator); |
| 65 | + validators.add(new FirebaseTokenValidator(gcpProjectIdProvider.getProjectId())); |
| 66 | + return new DelegatingOAuth2TokenValidator<>(validators); |
| 67 | + } |
| 68 | + |
| 69 | + @Bean |
| 70 | + @ConditionalOnMissingBean |
| 71 | + public JwtDecoder firebaseAuthenticationJwtDecoder( |
| 72 | + DelegatingOAuth2TokenValidator<Jwt> firebaseJwtDelegatingValidator, |
| 73 | + FirebaseAuthenticationProperties properties) { |
| 74 | + return new FirebaseJwtTokenDecoder(restOperations(), properties.getPublicKeysEndpoint(), |
| 75 | + firebaseJwtDelegatingValidator); |
| 76 | + } |
| 77 | + |
| 78 | + @Bean |
| 79 | + public JwtIssuerValidator jwtIssuerValidator(GcpProjectIdProvider gcpProjectIdProvider) { |
| 80 | + return new JwtIssuerValidator(String.format(ISSUER_TEMPLATE, gcpProjectIdProvider.getProjectId())); |
| 81 | + } |
| 82 | + |
| 83 | + private RestOperations restOperations() { |
| 84 | + SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); |
| 85 | + clientHttpRequestFactory.setConnectTimeout(5_000); |
| 86 | + clientHttpRequestFactory.setReadTimeout(2_000); |
| 87 | + return new RestTemplate(clientHttpRequestFactory); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments