Skip to content

Commit 83165d2

Browse files
committed
Added support of AuthProvider to tokenProvider
1 parent 94d9582 commit 83165d2

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConnectionProperties.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.logging.Logger;
99
import java.util.regex.Pattern;
1010

11+
import tech.ydb.auth.AuthProvider;
1112
import tech.ydb.auth.TokenAuthProvider;
1213
import tech.ydb.auth.iam.CloudAuthHelper;
1314
import tech.ydb.core.auth.StaticCredentials;
@@ -205,6 +206,9 @@ public GrpcTransportBuilder applyToGrpcTransport(GrpcTransportBuilder builder) t
205206
if (provider instanceof Supplier) {
206207
Supplier<?> prov = (Supplier<?>) provider;
207208
builder = builder.withAuthProvider((rpc) -> () -> prov.get().toString());
209+
} else if (provider instanceof AuthProvider) {
210+
AuthProvider prov = (AuthProvider) provider;
211+
builder = builder.withAuthProvider(prov);
208212
} else if (provider instanceof String) {
209213
String className = (String) provider;
210214
if (!FQCN.matcher(className).matches()) {
@@ -226,7 +230,7 @@ public GrpcTransportBuilder applyToGrpcTransport(GrpcTransportBuilder builder) t
226230
| IllegalArgumentException | InvocationTargetException ex) {
227231
throw new SQLException("Cannot construct tokenProvider " + className, ex);
228232
}
229-
} else {
233+
} else if (provider != null) {
230234
throw new SQLException("Cannot parse tokenProvider " + provider.getClass().getName());
231235
}
232236
}

jdbc/src/test/java/tech/ydb/jdbc/settings/YdbConnectionPropertiesTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.jupiter.api.Test;
99

1010
import tech.ydb.auth.AuthIdentity;
11+
import tech.ydb.auth.AuthProvider;
1112
import tech.ydb.core.grpc.GrpcTransport;
1213
import tech.ydb.core.grpc.GrpcTransportBuilder;
1314
import tech.ydb.jdbc.impl.helper.ExceptionAssert;
@@ -20,7 +21,18 @@ public class YdbConnectionPropertiesTest {
2021
private static final String YDB_URL = "grpc://localhost/local";
2122

2223
@Test
23-
public void tokenProviderObject() throws SQLException {
24+
public void tokenProviderTest() throws SQLException {
25+
Properties props = new Properties();
26+
props.put("tokenProvider", (AuthProvider) () -> () -> "AUTH_PROVIDER");
27+
YdbConnectionProperties cp = new YdbConnectionProperties(null, null, props);
28+
GrpcTransportBuilder builder = cp.applyToGrpcTransport(GrpcTransport.forConnectionString(YDB_URL));
29+
try (AuthIdentity identity = builder.getAuthProvider().createAuthIdentity(null)) {
30+
Assertions.assertEquals("AUTH_PROVIDER", identity.getToken());
31+
}
32+
}
33+
34+
@Test
35+
public void tokenProviderSupplierTest() throws SQLException {
2436
Properties props = new Properties();
2537
props.put("tokenProvider", (Supplier<String>) () -> "SUPPLIER");
2638
YdbConnectionProperties cp = new YdbConnectionProperties(null, null, props);

0 commit comments

Comments
 (0)