Skip to content

Commit 578f0a4

Browse files
authored
Merge pull request #633 from overture-stack/rc/5.2.0
release 5.2.0 use spring authorization server instead of legacy spring oauth2 remove legacy spring oauth2 libs
2 parents 06659a1 + 66ccd27 commit 578f0a4

26 files changed

+279
-386
lines changed

pom.xml

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>bio.overture</groupId>
77
<artifactId>ego</artifactId>
8-
<version>5.1.0</version>
8+
<version>5.2.0</version>
99

1010
<name>ego</name>
1111
<description>OAuth 2.0 Authorization service that supports multiple OpenID Connect Providers</description>
@@ -34,16 +34,6 @@
3434
<groupId>org.springframework.boot</groupId>
3535
<artifactId>spring-boot-starter-actuator</artifactId>
3636
</dependency>
37-
<dependency>
38-
<groupId>org.springframework.security.oauth</groupId>
39-
<artifactId>spring-security-oauth2</artifactId>
40-
<version>2.5.0.RELEASE</version>
41-
</dependency>
42-
<dependency>
43-
<groupId>org.springframework.security.oauth.boot</groupId>
44-
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
45-
<version>2.4.4</version>
46-
</dependency>
4737
<dependency>
4838
<groupId>org.springframework.boot</groupId>
4939
<artifactId>spring-boot-starter-security</artifactId>
@@ -60,6 +50,12 @@
6050
<artifactId>spring-boot-starter-oauth2-client</artifactId>
6151
</dependency>
6252

53+
<dependency>
54+
<groupId>org.springframework.security</groupId>
55+
<artifactId>spring-security-oauth2-authorization-server</artifactId>
56+
<version>0.2.1</version>
57+
</dependency>
58+
6359
<dependency>
6460
<groupId>org.springframework.boot</groupId>
6561
<artifactId>spring-boot-starter-jdbc</artifactId>

src/main/java/bio/overture/ego/config/AuthConfig.java

-114
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package bio.overture.ego.config;
2+
3+
import bio.overture.ego.token.signer.TokenSigner;
4+
import com.nimbusds.jose.jwk.JWKSet;
5+
import com.nimbusds.jose.jwk.RSAKey;
6+
import com.nimbusds.jose.jwk.source.ImmutableJWKSet;
7+
import com.nimbusds.jose.jwk.source.JWKSource;
8+
import com.nimbusds.jose.proc.SecurityContext;
9+
import java.security.interfaces.RSAPrivateKey;
10+
import java.security.interfaces.RSAPublicKey;
11+
import java.util.UUID;
12+
import lombok.val;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.beans.factory.annotation.Value;
15+
import org.springframework.context.annotation.Bean;
16+
import org.springframework.context.annotation.Configuration;
17+
import org.springframework.security.oauth2.jwt.JwtDecoder;
18+
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
19+
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
20+
21+
@Configuration
22+
public class AuthorizationServerConfig {
23+
24+
// Soruce of this method :
25+
// https://github.com/spring-projects/spring-security-samples/blob/main/servlet/spring-boot/java/oauth2/authorization-server/src/main/java/example/OAuth2AuthorizationServerSecurityConfiguration.java
26+
@Bean
27+
public JWKSource<SecurityContext> jwkSource(@Autowired TokenSigner tokenSigner) {
28+
val keyPair =
29+
tokenSigner.getKeyPair().orElseThrow(() -> new RuntimeException("no key pair found"));
30+
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
31+
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
32+
// @formatter:off
33+
RSAKey rsaKey =
34+
new RSAKey.Builder(publicKey)
35+
.privateKey(privateKey)
36+
.keyID(UUID.randomUUID().toString())
37+
.build();
38+
// @formatter:on
39+
JWKSet jwkSet = new JWKSet(rsaKey);
40+
return new ImmutableJWKSet<>(jwkSet);
41+
}
42+
43+
@Bean
44+
public JwtDecoder jwtDecoder(@Autowired TokenSigner tokenSigner) {
45+
val keyPair =
46+
tokenSigner.getKeyPair().orElseThrow(() -> new RuntimeException("no key pair found"));
47+
return NimbusJwtDecoder.withPublicKey((RSAPublicKey) keyPair.getPublic()).build();
48+
}
49+
50+
@Bean
51+
public ProviderSettings providerSettings(@Value("${token.issuer}") String issuer) {
52+
return ProviderSettings.builder().tokenEndpoint("/oauth/token").issuer(issuer).build();
53+
}
54+
}

src/main/java/bio/overture/ego/config/SecureServerConfig.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import bio.overture.ego.model.exceptions.SSOAuthenticationFailureHandler;
2020
import bio.overture.ego.security.*;
2121
import bio.overture.ego.service.ApplicationService;
22+
import bio.overture.ego.service.TokenService;
2223
import bio.overture.ego.utils.Redirects;
2324
import java.io.IOException;
2425
import java.util.Arrays;
@@ -27,6 +28,7 @@
2728
import javax.servlet.http.HttpServletResponse;
2829
import lombok.SneakyThrows;
2930
import lombok.val;
31+
import org.springframework.beans.factory.annotation.Autowired;
3032
import org.springframework.boot.autoconfigure.security.SecurityProperties;
3133
import org.springframework.boot.web.servlet.FilterRegistrationBean;
3234
import org.springframework.context.annotation.*;
@@ -36,6 +38,7 @@
3638
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3739
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3840
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
41+
import org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization.OAuth2AuthorizationServerConfigurer;
3942
import org.springframework.security.config.http.SessionCreationPolicy;
4043
import org.springframework.security.core.Authentication;
4144
import org.springframework.security.oauth2.client.endpoint.DefaultAuthorizationCodeTokenResponseClient;
@@ -178,20 +181,27 @@ protected void configure(HttpSecurity http) throws Exception {
178181
}
179182
}
180183

184+
@Bean
185+
@SneakyThrows
186+
public JWTAuthorizationFilter authorizationFilter(
187+
TokenService tokenService, ApplicationService applicationService) {
188+
return new JWTAuthorizationFilter(PUBLIC_ENDPOINTS, tokenService, applicationService);
189+
}
190+
181191
@Configuration
182192
@Order(SecurityProperties.BASIC_AUTH_ORDER + 3)
183193
public class AppConfigurerAdapter extends WebSecurityConfigurerAdapter {
184194

185-
@Bean
186-
@SneakyThrows
187-
public JWTAuthorizationFilter authorizationFilter() {
188-
return new JWTAuthorizationFilter(PUBLIC_ENDPOINTS);
189-
}
195+
OAuth2AuthorizationServerConfigurer<HttpSecurity> authorizationServerConfigurer =
196+
new OAuth2AuthorizationServerConfigurer<>();
197+
@Autowired JWTAuthorizationFilter authorizationFilter;
190198

191199
@Override
192200
protected void configure(HttpSecurity http) throws Exception {
193201
http.csrf()
194202
.disable()
203+
.apply(authorizationServerConfigurer)
204+
.and()
195205
.authorizeRequests()
196206
.antMatchers(
197207
"/",
@@ -211,7 +221,7 @@ protected void configure(HttpSecurity http) throws Exception {
211221
.anyRequest()
212222
.authenticated()
213223
.and()
214-
.addFilterBefore(authorizationFilter(), BasicAuthenticationFilter.class)
224+
.addFilterBefore(authorizationFilter, BasicAuthenticationFilter.class)
215225
.sessionManagement()
216226
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
217227
}

src/main/java/bio/overture/ego/controller/AuthController.java

+1-21
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
import bio.overture.ego.token.signer.TokenSigner;
3535
import bio.overture.ego.utils.Tokens;
3636
import io.swagger.annotations.Api;
37-
import io.swagger.annotations.ApiOperation;
38-
import java.security.Principal;
39-
import java.util.Map;
4037
import java.util.Objects;
4138
import javax.servlet.http.HttpServletResponse;
4239
import lombok.NonNull;
@@ -49,11 +46,7 @@
4946
import org.springframework.http.ResponseEntity;
5047
import org.springframework.security.core.context.SecurityContextHolder;
5148
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
52-
import org.springframework.security.oauth2.common.OAuth2AccessToken;
53-
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
54-
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
5549
import org.springframework.util.StringUtils;
56-
import org.springframework.web.HttpRequestMethodNotSupportedException;
5750
import org.springframework.web.bind.annotation.*;
5851
import springfox.documentation.annotations.ApiIgnore;
5952

@@ -70,11 +63,9 @@ public class AuthController {
7063
private final GoogleTokenService googleTokenService;
7164
private final TokenSigner tokenSigner;
7265
private final RefreshContextService refreshContextService;
73-
private final TokenEndpoint tokenEndpoint;
7466

7567
@Autowired
7668
public AuthController(
77-
@NonNull TokenEndpoint tokenEndpoint,
7869
@NonNull TokenService tokenService,
7970
@NonNull GoogleTokenService googleTokenService,
8071
@NonNull TokenSigner tokenSigner,
@@ -83,17 +74,6 @@ public AuthController(
8374
this.googleTokenService = googleTokenService;
8475
this.tokenSigner = tokenSigner;
8576
this.refreshContextService = refreshContextService;
86-
this.tokenEndpoint = tokenEndpoint;
87-
}
88-
89-
// This spring tokenEndpoint controller is proxied so that Springfox can include this in the
90-
// swagger-ui under the Auth controller
91-
@ApiOperation(value = POST_ACCESS_TOKEN)
92-
@RequestMapping(value = "/token", method = RequestMethod.POST)
93-
public ResponseEntity<OAuth2AccessToken> postAccessToken(
94-
Principal principal, @RequestParam Map<String, String> parameters)
95-
throws HttpRequestMethodNotSupportedException {
96-
return this.tokenEndpoint.postAccessToken(principal, parameters);
9777
}
9878

9979
@RequestMapping(method = GET, value = "/google/token")
@@ -111,7 +91,7 @@ public ResponseEntity<OAuth2AccessToken> postAccessToken(
11191
@ResponseStatus(value = OK)
11292
@SneakyThrows
11393
public @ResponseBody boolean verifyJWToken(@RequestHeader(value = "token") final String token) {
114-
if (StringUtils.isEmpty(token)) {
94+
if (!StringUtils.hasText(token)) {
11595
throw new InvalidTokenException("ScopedAccessToken is empty");
11696
}
11797

src/main/java/bio/overture/ego/grpc/interceptor/ApplicationAuthInterceptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private Optional<Application> getAppInfo(String token) {
9393
val claims = tokenService.getTokenAppInfo(token);
9494
return claims == null
9595
? Optional.empty()
96-
: applicationService.findByClientId(claims.getClientId());
96+
: applicationService.getClientApplication(claims.getClientId());
9797
}
9898

9999
@Getter

src/main/java/bio/overture/ego/model/exceptions/SSOAuthenticationFailureHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import lombok.val;
1717
import org.apache.http.client.utils.URIBuilder;
1818
import org.springframework.security.core.AuthenticationException;
19-
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
2019
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
2120
import org.springframework.stereotype.Component;
2221

src/main/java/bio/overture/ego/security/AuthorizationStrategyConfig.java

-44
This file was deleted.

0 commit comments

Comments
 (0)