|
31 | 31 | import static com.datastax.oss.driver.api.core.config.DefaultDriverOption.SSL_TRUSTSTORE_PATH;
|
32 | 32 |
|
33 | 33 | import com.codahale.metrics.MetricRegistry;
|
| 34 | +import com.datastax.db.driver.api.plugin.auth.OIDCAuthProvider; |
34 | 35 | import com.datastax.dse.driver.api.core.config.DseDriverOption;
|
35 | 36 | import com.datastax.dse.driver.internal.core.auth.DseGssApiAuthProvider;
|
36 | 37 | import com.datastax.oss.common.sink.AbstractSinkTask;
|
|
66 | 67 | import com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;
|
67 | 68 | import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
|
68 | 69 | import com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory;
|
| 70 | +import com.nimbusds.oauth2.sdk.AccessTokenResponse; |
| 71 | +import com.nimbusds.oauth2.sdk.ClientCredentialsGrant; |
| 72 | +import com.nimbusds.oauth2.sdk.ParseException; |
| 73 | +import com.nimbusds.oauth2.sdk.TokenErrorResponse; |
| 74 | +import com.nimbusds.oauth2.sdk.TokenRequest; |
| 75 | +import com.nimbusds.oauth2.sdk.TokenResponse; |
| 76 | +import com.nimbusds.oauth2.sdk.auth.ClientAuthentication; |
| 77 | +import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod; |
| 78 | +import com.nimbusds.oauth2.sdk.auth.ClientSecretBasic; |
| 79 | +import com.nimbusds.oauth2.sdk.auth.Secret; |
| 80 | +import com.nimbusds.oauth2.sdk.http.HTTPRequest; |
| 81 | +import com.nimbusds.oauth2.sdk.id.ClientID; |
| 82 | +import com.nimbusds.oauth2.sdk.token.AccessToken; |
| 83 | +import com.nimbusds.openid.connect.sdk.OIDCTokenResponseParser; |
69 | 84 | import com.typesafe.config.Config;
|
70 | 85 | import com.typesafe.config.ConfigFactory;
|
71 | 86 | import edu.umd.cs.findbugs.annotations.NonNull;
|
72 | 87 | import edu.umd.cs.findbugs.annotations.Nullable;
|
| 88 | + |
| 89 | +import java.io.IOException; |
73 | 90 | import java.net.InetSocketAddress;
|
| 91 | +import java.net.URI; |
| 92 | +import java.net.URISyntaxException; |
74 | 93 | import java.nio.file.Path;
|
75 | 94 | import java.util.Collection;
|
76 | 95 | import java.util.HashMap;
|
77 | 96 | import java.util.List;
|
78 | 97 | import java.util.Map;
|
79 | 98 | import java.util.Optional;
|
| 99 | +import java.util.Set; |
80 | 100 | import java.util.concurrent.CompletableFuture;
|
81 | 101 | import java.util.concurrent.CompletionException;
|
82 | 102 | import java.util.concurrent.CompletionStage;
|
@@ -621,6 +641,42 @@ private static void processAuthenticatorConfig(
|
621 | 641 | AUTH_PROVIDER_SASL_PROPERTIES, ImmutableMap.of("javax.security.sasl.qop", "auth"))
|
622 | 642 | .withStringMap(DseDriverOption.AUTH_PROVIDER_LOGIN_CONFIGURATION, loginConfig);
|
623 | 643 | } else if (authConfig.getProvider() == AuthenticatorConfig.Provider.OIDC) {
|
| 644 | + ClientCredentialsGrant grant = new ClientCredentialsGrant(); |
| 645 | + |
| 646 | + ClientID clientID = new ClientID(config.getAuthenticatorConfig().getOIDCClientId()); |
| 647 | + Secret clientSecret = new Secret(config.getAuthenticatorConfig().getOIDCClientSecret()); |
| 648 | + |
| 649 | + ClientAuthentication clientAuth = new ClientSecretBasic(clientID, clientSecret); |
| 650 | + |
| 651 | + URI tokenEndpoint = config.getAuthenticatorConfig().getOIDCIssuer(); |
| 652 | + TokenRequest request = new TokenRequest(tokenEndpoint, clientAuth, grant, null); |
| 653 | + |
| 654 | + TokenResponse tokenResponse; |
| 655 | + try |
| 656 | + { |
| 657 | + tokenResponse = OIDCTokenResponseParser.parse(request.toHTTPRequest().send()); |
| 658 | + } |
| 659 | + catch (ParseException e) { |
| 660 | + throw new RuntimeException("Failed OIDC request parsing: " + e.getMessage()); |
| 661 | + } |
| 662 | + catch (IOException e) { |
| 663 | + throw new RuntimeException("Failed OIDC request: " + e.getMessage()); |
| 664 | + } |
| 665 | + |
| 666 | + if (!tokenResponse.indicatesSuccess()) { |
| 667 | + // We got an error response... |
| 668 | + TokenErrorResponse errorResponse = tokenResponse.toErrorResponse(); |
| 669 | + throw new RuntimeException("Failed OIDC token response:" + errorResponse.toString()); |
| 670 | + } |
| 671 | + |
| 672 | + AccessTokenResponse successResponse = tokenResponse.toSuccessResponse(); |
| 673 | + |
| 674 | + AccessToken accessToken = successResponse.getTokens().getAccessToken(); |
| 675 | + // RefreshToken refreshToken = successResponse.getTokens().getRefreshToken(); |
| 676 | + |
| 677 | + OIDCAuthProvider authProvider = new OIDCAuthProvider(accessToken.getValue()); |
| 678 | + |
| 679 | + configLoaderBuilder.withClass(AUTH_PROVIDER_CLASS, OIDCAuthProvider.class); |
624 | 680 | /*
|
625 | 681 | TODO: OidcApiAuthProvider.
|
626 | 682 | configLoaderBuilder
|
|
0 commit comments