44import com .nimbusds .jose .jwk .KeyUse ;
55import com .nimbusds .jose .jwk .RSAKey ;
66import com .nimbusds .jose .jwk .gen .RSAKeyGenerator ;
7+ import com .nimbusds .oauth2 .sdk .AuthorizationGrant ;
8+ import com .nimbusds .oauth2 .sdk .ClientCredentialsGrant ;
79import com .nimbusds .oauth2 .sdk .GeneralException ;
810import com .nimbusds .oauth2 .sdk .auth .ClientAuthentication ;
911import com .nimbusds .oauth2 .sdk .auth .ClientSecretBasic ;
1012import com .nimbusds .oauth2 .sdk .auth .Secret ;
1113import com .nimbusds .oauth2 .sdk .id .ClientID ;
1214import com .nimbusds .oauth2 .sdk .id .Issuer ;
15+ import com .nimbusds .oauth2 .sdk .token .BearerAccessToken ;
16+ import com .nimbusds .oauth2 .sdk .token .TokenTypeURI ;
17+ import com .nimbusds .oauth2 .sdk .tokenexchange .TokenExchangeGrant ;
1318import com .nimbusds .openid .connect .sdk .op .OIDCProviderMetadata ;
1419import io .grpc .*;
1520import io .opentdf .platform .wellknownconfiguration .GetWellKnownConfigurationRequest ;
@@ -41,6 +46,7 @@ public class SDKBuilder {
4146 private ClientAuthentication clientAuth = null ;
4247 private Boolean usePlainText ;
4348 private SSLFactory sslFactory ;
49+ private AuthorizationGrant authzGrant ;
4450
4551 private static final Logger logger = LoggerFactory .getLogger (SDKBuilder .class );
4652
@@ -49,6 +55,7 @@ public static SDKBuilder newBuilder() {
4955 builder .usePlainText = false ;
5056 builder .clientAuth = null ;
5157 builder .platformEndpoint = null ;
58+ builder .authzGrant = null ;
5259
5360 return builder ;
5461 }
@@ -99,6 +106,24 @@ public SDKBuilder platformEndpoint(String platformEndpoint) {
99106 return this ;
100107 }
101108
109+ public SDKBuilder authorizationGrant (AuthorizationGrant authzGrant ) {
110+ if (this .authzGrant != null ) {
111+ throw new RuntimeException ("Authorization grant can't be specified twice" );
112+ }
113+ this .authzGrant = authzGrant ;
114+ return this ;
115+ }
116+
117+ public SDKBuilder tokenExchange (String jwt ) {
118+ if (this .authzGrant != null ) {
119+ throw new RuntimeException ("Authorization grant can't be specified twice" );
120+ }
121+
122+ BearerAccessToken token = new BearerAccessToken (jwt );
123+ this .authzGrant = new TokenExchangeGrant (token , TokenTypeURI .ACCESS_TOKEN );
124+ return this ;
125+ }
126+
102127 public SDKBuilder clientSecret (String clientID , String clientSecret ) {
103128 ClientID cid = new ClientID (clientID );
104129 Secret cs = new Secret (clientSecret );
@@ -168,7 +193,11 @@ private GRPCAuthInterceptor getGrpcAuthInterceptor(RSAKey rsaKey) {
168193 throw new SDKException ("Error resolving the OIDC provider metadata" , e );
169194 }
170195
171- return new GRPCAuthInterceptor (clientAuth , rsaKey , providerMetadata .getTokenEndpointURI (), sslFactory );
196+ if (this .authzGrant == null ) {
197+ this .authzGrant = new ClientCredentialsGrant ();
198+ }
199+
200+ return new GRPCAuthInterceptor (clientAuth , rsaKey , providerMetadata .getTokenEndpointURI (), this .authzGrant , sslFactory );
172201 }
173202
174203 static class ServicesAndInternals {
0 commit comments