3131import ca .uhn .fhir .rest .gclient .IOperationUntyped ;
3232import ca .uhn .fhir .rest .gclient .IOperationUntypedWithInput ;
3333import com .google .api .client .auth .oauth2 .ClientCredentialsTokenRequest ;
34+ import com .google .api .client .auth .oauth2 .ClientParametersAuthentication ;
3435import com .google .api .client .auth .oauth2 .TokenResponse ;
3536import com .google .api .client .http .BasicAuthentication ;
3637import com .google .api .client .http .GenericUrl ;
3738import com .google .api .client .http .javanet .NetHttpTransport ;
3839import com .google .api .client .json .gson .GsonFactory ;
3940import com .google .common .base .Preconditions ;
4041import com .google .common .base .Strings ;
42+ import com .google .fhir .analytics .enumeration .ClientCredentialsAuthMechanism ;
4143import java .io .IOException ;
4244import java .time .Instant ;
4345import java .util .List ;
@@ -61,6 +63,8 @@ public class FetchUtil {
6163
6264 private final String oAuthTokenEndpoint ;
6365
66+ private final ClientCredentialsAuthMechanism oAuthMechanism ;
67+
6468 private final String oAuthClientId ;
6569
6670 private final String oAuthClientSecret ;
@@ -74,13 +78,15 @@ public class FetchUtil {
7478 String sourceUser ,
7579 String sourcePw ,
7680 String oAuthTokenEndpoint ,
81+ ClientCredentialsAuthMechanism oAuthMechanism ,
7782 String oAuthClientId ,
7883 String oAuthClientSecret ,
7984 FhirContext fhirContext ) {
8085 this .fhirUrl = sourceFhirUrl ;
8186 this .sourceUser = Strings .nullToEmpty (sourceUser );
8287 this .sourcePw = Strings .nullToEmpty (sourcePw );
8388 this .oAuthTokenEndpoint = Strings .nullToEmpty (oAuthTokenEndpoint );
89+ this .oAuthMechanism = oAuthMechanism ;
8490 this .oAuthClientId = Strings .nullToEmpty (oAuthClientId );
8591 this .oAuthClientSecret = Strings .nullToEmpty (oAuthClientSecret );
8692 this .fhirContext = fhirContext ;
@@ -93,7 +99,7 @@ public class FetchUtil {
9399 log .info ("Fetching access tokens from {}" , oAuthTokenEndpoint );
94100 authInterceptor =
95101 new ClientCredentialsAuthInterceptor (
96- oAuthTokenEndpoint , oAuthClientId , oAuthClientSecret );
102+ oAuthTokenEndpoint , oAuthMechanism , oAuthClientId , oAuthClientSecret );
97103 } else if (!this .sourceUser .isEmpty ()) {
98104 authInterceptor = new BasicAuthInterceptor (this .sourceUser , sourcePw );
99105 } else {
@@ -252,16 +258,23 @@ private static class ClientCredentialsAuthInterceptor extends BearerTokenAuthInt
252258 private static final int TOKEN_REFRESH_LEEWAY_IN_SECONDS = 10 ;
253259
254260 private final String tokenEndpoint ;
261+ private final ClientCredentialsAuthMechanism oAuthMechanism ;
255262 private final String clientId ;
256263 private final String clientSecret ;
257264 private TokenResponse tokenResponse ;
258265 private Instant nextRefresh ;
259266
260- ClientCredentialsAuthInterceptor (String tokenEndpoint , String clientId , String clientSecret ) {
267+ ClientCredentialsAuthInterceptor (
268+ String tokenEndpoint ,
269+ ClientCredentialsAuthMechanism oAuthMechanism ,
270+ String clientId ,
271+ String clientSecret ) {
261272 Preconditions .checkNotNull (tokenEndpoint );
273+ Preconditions .checkNotNull (clientSecret );
262274 Preconditions .checkNotNull (clientId );
263275 Preconditions .checkNotNull (clientSecret );
264276 this .tokenEndpoint = tokenEndpoint ;
277+ this .oAuthMechanism = oAuthMechanism ;
265278 this .clientId = clientId ;
266279 this .clientSecret = clientSecret ;
267280 }
@@ -291,12 +304,24 @@ public void interceptRequest(IHttpRequest theRequest) {
291304 }
292305
293306 TokenResponse requestAccessToken () throws IOException {
294- TokenResponse response =
307+ ClientCredentialsTokenRequest clientCredentialsTokenRequest =
295308 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 ();
300325 }
301326 }
302327}
0 commit comments