31
31
import ca .uhn .fhir .rest .gclient .IOperationUntyped ;
32
32
import ca .uhn .fhir .rest .gclient .IOperationUntypedWithInput ;
33
33
import com .google .api .client .auth .oauth2 .ClientCredentialsTokenRequest ;
34
+ import com .google .api .client .auth .oauth2 .ClientParametersAuthentication ;
34
35
import com .google .api .client .auth .oauth2 .TokenResponse ;
35
36
import com .google .api .client .http .BasicAuthentication ;
36
37
import com .google .api .client .http .GenericUrl ;
37
38
import com .google .api .client .http .javanet .NetHttpTransport ;
38
39
import com .google .api .client .json .gson .GsonFactory ;
39
40
import com .google .common .base .Preconditions ;
40
41
import com .google .common .base .Strings ;
42
+ import com .google .fhir .analytics .enumeration .ClientCredentialsAuthMechanism ;
41
43
import java .io .IOException ;
42
44
import java .time .Instant ;
43
45
import java .util .List ;
@@ -61,6 +63,8 @@ public class FetchUtil {
61
63
62
64
private final String oAuthTokenEndpoint ;
63
65
66
+ private final ClientCredentialsAuthMechanism oAuthMechanism ;
67
+
64
68
private final String oAuthClientId ;
65
69
66
70
private final String oAuthClientSecret ;
@@ -74,13 +78,15 @@ public class FetchUtil {
74
78
String sourceUser ,
75
79
String sourcePw ,
76
80
String oAuthTokenEndpoint ,
81
+ ClientCredentialsAuthMechanism oAuthMechanism ,
77
82
String oAuthClientId ,
78
83
String oAuthClientSecret ,
79
84
FhirContext fhirContext ) {
80
85
this .fhirUrl = sourceFhirUrl ;
81
86
this .sourceUser = Strings .nullToEmpty (sourceUser );
82
87
this .sourcePw = Strings .nullToEmpty (sourcePw );
83
88
this .oAuthTokenEndpoint = Strings .nullToEmpty (oAuthTokenEndpoint );
89
+ this .oAuthMechanism = oAuthMechanism ;
84
90
this .oAuthClientId = Strings .nullToEmpty (oAuthClientId );
85
91
this .oAuthClientSecret = Strings .nullToEmpty (oAuthClientSecret );
86
92
this .fhirContext = fhirContext ;
@@ -93,7 +99,7 @@ public class FetchUtil {
93
99
log .info ("Fetching access tokens from {}" , oAuthTokenEndpoint );
94
100
authInterceptor =
95
101
new ClientCredentialsAuthInterceptor (
96
- oAuthTokenEndpoint , oAuthClientId , oAuthClientSecret );
102
+ oAuthTokenEndpoint , oAuthMechanism , oAuthClientId , oAuthClientSecret );
97
103
} else if (!this .sourceUser .isEmpty ()) {
98
104
authInterceptor = new BasicAuthInterceptor (this .sourceUser , sourcePw );
99
105
} else {
@@ -252,16 +258,23 @@ private static class ClientCredentialsAuthInterceptor extends BearerTokenAuthInt
252
258
private static final int TOKEN_REFRESH_LEEWAY_IN_SECONDS = 10 ;
253
259
254
260
private final String tokenEndpoint ;
261
+ private final ClientCredentialsAuthMechanism oAuthMechanism ;
255
262
private final String clientId ;
256
263
private final String clientSecret ;
257
264
private TokenResponse tokenResponse ;
258
265
private Instant nextRefresh ;
259
266
260
- ClientCredentialsAuthInterceptor (String tokenEndpoint , String clientId , String clientSecret ) {
267
+ ClientCredentialsAuthInterceptor (
268
+ String tokenEndpoint ,
269
+ ClientCredentialsAuthMechanism oAuthMechanism ,
270
+ String clientId ,
271
+ String clientSecret ) {
261
272
Preconditions .checkNotNull (tokenEndpoint );
273
+ Preconditions .checkNotNull (clientSecret );
262
274
Preconditions .checkNotNull (clientId );
263
275
Preconditions .checkNotNull (clientSecret );
264
276
this .tokenEndpoint = tokenEndpoint ;
277
+ this .oAuthMechanism = oAuthMechanism ;
265
278
this .clientId = clientId ;
266
279
this .clientSecret = clientSecret ;
267
280
}
@@ -291,12 +304,24 @@ public void interceptRequest(IHttpRequest theRequest) {
291
304
}
292
305
293
306
TokenResponse requestAccessToken () throws IOException {
294
- TokenResponse response =
307
+ ClientCredentialsTokenRequest clientCredentialsTokenRequest =
295
308
new ClientCredentialsTokenRequest (
296
- new NetHttpTransport (), new GsonFactory (), new GenericUrl (tokenEndpoint ))
297
- .setClientAuthentication (new BasicAuthentication (clientId , clientSecret ))
298
- .execute ();
299
- return response ;
309
+ new NetHttpTransport (), new GsonFactory (), new GenericUrl (tokenEndpoint ));
310
+ switch (oAuthMechanism ) {
311
+ case BASIC :
312
+ clientCredentialsTokenRequest =
313
+ clientCredentialsTokenRequest .setClientAuthentication (
314
+ new BasicAuthentication (clientId , clientSecret ));
315
+ break ;
316
+ case BODY :
317
+ clientCredentialsTokenRequest =
318
+ clientCredentialsTokenRequest .setClientAuthentication (
319
+ new ClientParametersAuthentication (clientId , clientSecret ));
320
+ break ;
321
+ case JWT :
322
+ break ;
323
+ }
324
+ return clientCredentialsTokenRequest .execute ();
300
325
}
301
326
}
302
327
}
0 commit comments