26
26
import lombok .val ;
27
27
import org .apache .commons .codec .binary .Base64 ;
28
28
import org .springframework .beans .factory .annotation .Autowired ;
29
- import org .springframework .beans .factory .annotation .Value ;
30
29
import org .springframework .context .annotation .Configuration ;
31
30
import org .springframework .http .HttpHeaders ;
32
31
import org .springframework .http .HttpMethod ;
32
+ import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
33
+ import org .springframework .security .oauth2 .client .registration .ClientRegistrationRepository ;
33
34
import org .springframework .stereotype .Service ;
34
35
import org .springframework .transaction .annotation .Transactional ;
35
36
import org .springframework .util .LinkedMultiValueMap ;
@@ -52,20 +53,12 @@ public class PassportService {
52
53
53
54
@ Autowired private CacheUtil cacheUtil ;
54
55
56
+ @ Autowired private ClientRegistrationRepository clientRegistrationRepository ;
57
+
55
58
private final String REQUESTED_TOKEN_TYPE = "urn:ga4gh:params:oauth:token-type:passport" ;
56
59
private final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token" ;
57
60
private final String GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange" ;
58
61
59
- @ Value ("${spring.security.oauth2.client.registration.passport.clientId}" )
60
- private String clientId ;
61
-
62
- @ Value ("${spring.security.oauth2.client.registration.passport.clientSecret}" )
63
- private String clientSecret ;
64
-
65
- @ Value ("${spring.security.oauth2.client.provider.passport.issuer-uri}" )
66
- private String passportIssuerUri ;
67
-
68
-
69
62
70
63
@ Autowired
71
64
public PassportService (
@@ -74,14 +67,14 @@ public PassportService(
74
67
this .visaPermissionService = visaPermissionService ;
75
68
}
76
69
77
- public List <VisaPermission > getPermissions (String authToken )
70
+ public List <VisaPermission > getPermissions (String authToken , String providerType )
78
71
throws JsonProcessingException , ParseException , JwkException {
79
72
// Validates passport auth token
80
- isValidPassport (authToken );
73
+ isValidPassport (authToken , providerType );
81
74
// Parses passport JWT token
82
75
Passport parsedPassport = parsePassport (authToken );
83
76
// Fetches visas for parsed passport
84
- List <PassportVisa > visas = getVisas (parsedPassport );
77
+ List <PassportVisa > visas = getVisas (parsedPassport , providerType );
85
78
// Fetches visa permissions for extracted visas
86
79
List <VisaPermission > visaPermissions = getVisaPermissions (visas );
87
80
// removes deduplicates from visaPermissions
@@ -90,22 +83,22 @@ public List<VisaPermission> getPermissions(String authToken)
90
83
}
91
84
92
85
// Validates passport token based on public key
93
- private void isValidPassport (@ NonNull String authToken )
94
- throws ParseException , JwkException , JsonProcessingException {
86
+ private void isValidPassport (@ NonNull String authToken , @ NonNull String providerType )
87
+ throws JwkException {
95
88
DecodedJWT jwt = JWT .decode (authToken );
96
- Jwk jwk = cacheUtil .getPassportBrokerPublicKey ().get (jwt .getKeyId ());
89
+ Jwk jwk = cacheUtil .getPassportBrokerPublicKey (providerType ).get (jwt .getKeyId ());
97
90
Algorithm algorithm = Algorithm .RSA256 ((RSAPublicKey ) jwk .getPublicKey (), null );
98
91
algorithm .verify (jwt );
99
92
}
100
93
101
94
// Extracts Visas from parsed passport object
102
- private List <PassportVisa > getVisas (Passport passport ) {
95
+ private List <PassportVisa > getVisas (Passport passport , @ NonNull String providerType ) {
103
96
List <PassportVisa > visas = new ArrayList <>();
104
97
passport .getGa4ghPassportV1 ().stream ()
105
98
.forEach (
106
99
visaJwt -> {
107
100
try {
108
- visaService .isValidVisa (visaJwt );
101
+ visaService .isValidVisa (visaJwt , providerType );
109
102
PassportVisa visa = visaService .parseVisa (visaJwt );
110
103
if (visa != null ) {
111
104
visas .add (visa );
@@ -134,8 +127,8 @@ private List<VisaPermission> getVisaPermissions(List<PassportVisa> visas) {
134
127
return visaPermissions ;
135
128
}
136
129
137
- public Set <Scope > extractScopes (@ NonNull String passportJwtToken ) throws ParseException , JwkException , JsonProcessingException {
138
- val resolvedPermissions = getPermissions (passportJwtToken );
130
+ public Set <Scope > extractScopes (@ NonNull String passportJwtToken , @ NonNull String providerType ) throws ParseException , JwkException , JsonProcessingException {
131
+ val resolvedPermissions = getPermissions (passportJwtToken , providerType );
139
132
val output = mapToSet (resolvedPermissions , AbstractPermissionService ::buildScope );
140
133
if (output .isEmpty ()) {
141
134
output .add (Scope .defaultScope ());
@@ -162,19 +155,18 @@ private List<VisaPermission> deDupeVisaPermissions(List<VisaPermission> visaPerm
162
155
return permissionsSet .stream ().collect (Collectors .toList ());
163
156
}
164
157
165
- public String getPassportToken (String accessToken ) {
158
+ public String getPassportToken (String providerId , String accessToken ) {
166
159
167
160
if (accessToken == null || accessToken .isEmpty ()) return null ;
168
161
169
- val params = passportTokenParams ( accessToken );
162
+ val clientRegistration = clientRegistrationRepository . findByRegistrationId ( providerId );
170
163
171
164
val uri = UriComponentsBuilder
172
- .fromUriString (passportIssuerUri )
173
- .path ("/token" )
174
- .queryParams (params )
165
+ .fromUriString (clientRegistration .getProviderDetails ().getTokenUri ())
166
+ .queryParams (passportTokenParams (accessToken ))
175
167
.toUriString ();
176
168
177
- val passportToken = getTemplate (clientId , clientSecret )
169
+ val passportToken = getTemplate (clientRegistration )
178
170
.exchange (uri ,
179
171
HttpMethod .POST ,
180
172
null ,
@@ -186,7 +178,7 @@ public String getPassportToken(String accessToken) {
186
178
null ;
187
179
}
188
180
189
- private RestTemplate getTemplate (String clientId , String clientSecret ) {
181
+ private RestTemplate getTemplate (ClientRegistration clientRegistration ) {
190
182
RestTemplate restTemplate = new RestTemplate ();
191
183
restTemplate
192
184
.getInterceptors ()
@@ -195,7 +187,7 @@ private RestTemplate getTemplate(String clientId, String clientSecret) {
195
187
x .getHeaders ()
196
188
.set (
197
189
HttpHeaders .AUTHORIZATION ,
198
- "Basic " + getBasicAuthHeader (clientId , clientSecret ));
190
+ "Basic " + getBasicAuthHeader (clientRegistration . getClientId (), clientRegistration . getClientSecret () ));
199
191
return z .execute (x , y );
200
192
});
201
193
return restTemplate ;
0 commit comments