Skip to content

Commit b264a2c

Browse files
authored
Merge pull request #33 from ydb-platform/oauth2_examples
Added OAuth2 token exchange examples
2 parents 125bb46 + f299b77 commit b264a2c

File tree

6 files changed

+213
-2
lines changed

6 files changed

+213
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>tech.ydb.examples.auth</groupId>
7+
<artifactId>ydb-sdk-auth-examples</artifactId>
8+
<version>1.1.0-SNAPSHOT</version>
9+
<relativePath>../../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>ydb-java-example-auth-oauth2-token-credentials</artifactId>
13+
<name>YDB OAuth2 Token Example</name>
14+
<packaging>jar</packaging>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>tech.ydb</groupId>
19+
<artifactId>ydb-sdk-table</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>tech.ydb.auth</groupId>
23+
<artifactId>ydb-oauth2-provider</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.apache.logging.log4j</groupId>
27+
<artifactId>log4j-slf4j-impl</artifactId>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<finalName>ydb-oauth2-token-example</finalName>
33+
<plugins>
34+
<!-- copy dependencies to libs folder -->
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-dependency-plugin</artifactId>
38+
</plugin>
39+
<!-- add libs folder to classpath -->
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-jar-plugin</artifactId>
43+
<configuration>
44+
<archive>
45+
<manifest>
46+
<addClasspath>true</addClasspath>
47+
<classpathPrefix>libs/</classpathPrefix>
48+
<mainClass>tech.ydb.example.Main</mainClass>
49+
</manifest>
50+
</archive>
51+
</configuration>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package tech.ydb.example;
2+
3+
4+
import tech.ydb.auth.OAuth2TokenExchangeProvider;
5+
import tech.ydb.auth.OAuth2TokenSource;
6+
import tech.ydb.core.grpc.GrpcTransport;
7+
import tech.ydb.table.SessionRetryContext;
8+
import tech.ydb.table.TableClient;
9+
import tech.ydb.table.query.DataQueryResult;
10+
import tech.ydb.table.result.ResultSetReader;
11+
import tech.ydb.table.transaction.TxControl;
12+
13+
14+
public final class Main {
15+
public static void main(String[] args) {
16+
if (args.length != 3) {
17+
System.err.println("Usage: java -jar ydb-oauth2-token-example "
18+
+ "<connection-string> <oauth2-endpoint> <oauth2-token-value>");
19+
return;
20+
}
21+
String connectionString = args[0];
22+
String oauth2Endpoint = args[1];
23+
String refreshToken = args[2];
24+
25+
OAuth2TokenSource tokenSource = OAuth2TokenSource.fromValue(refreshToken);
26+
OAuth2TokenExchangeProvider authProvider = OAuth2TokenExchangeProvider.newBuilder(oauth2Endpoint, tokenSource)
27+
.withScope("demo-scope") // customize of OAuth2 request
28+
.build();
29+
30+
try (GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
31+
.withAuthProvider(authProvider)
32+
.build()) {
33+
try (TableClient tableClient = TableClient.newClient(transport).build()) {
34+
SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build();
35+
36+
DataQueryResult dataQueryResult = retryCtx.supplyResult(
37+
session -> session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
38+
).join().getValue();
39+
40+
ResultSetReader rsReader = dataQueryResult.getResultSet(0);
41+
while (rsReader.next()) {
42+
System.out.println(rsReader.getColumn(0).getInt32());
43+
}
44+
}
45+
}
46+
}
47+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>tech.ydb.examples.auth</groupId>
7+
<artifactId>ydb-sdk-auth-examples</artifactId>
8+
<version>1.1.0-SNAPSHOT</version>
9+
<relativePath>../../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>ydb-java-example-auth-oauth2-key-credentials</artifactId>
13+
<name>YDB OAuth2 Private Key Example</name>
14+
<packaging>jar</packaging>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>tech.ydb</groupId>
19+
<artifactId>ydb-sdk-table</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>tech.ydb.auth</groupId>
23+
<artifactId>ydb-oauth2-provider</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.apache.logging.log4j</groupId>
27+
<artifactId>log4j-slf4j-impl</artifactId>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<finalName>ydb-oauth2-private-key-example</finalName>
33+
<plugins>
34+
<!-- copy dependencies to libs folder -->
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-dependency-plugin</artifactId>
38+
</plugin>
39+
<!-- add libs folder to classpath -->
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-jar-plugin</artifactId>
43+
<configuration>
44+
<archive>
45+
<manifest>
46+
<addClasspath>true</addClasspath>
47+
<classpathPrefix>libs/</classpathPrefix>
48+
<mainClass>tech.ydb.example.Main</mainClass>
49+
</manifest>
50+
</archive>
51+
</configuration>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package tech.ydb.example;
2+
3+
4+
import java.io.File;
5+
6+
import tech.ydb.auth.OAuth2TokenExchangeProvider;
7+
import tech.ydb.auth.OAuth2TokenSource;
8+
import tech.ydb.core.grpc.GrpcTransport;
9+
import tech.ydb.table.SessionRetryContext;
10+
import tech.ydb.table.TableClient;
11+
import tech.ydb.table.query.DataQueryResult;
12+
import tech.ydb.table.result.ResultSetReader;
13+
import tech.ydb.table.transaction.TxControl;
14+
15+
16+
public final class Main {
17+
public static void main(String[] args) {
18+
if (args.length != 3) {
19+
System.err.println("Usage: java -jar ydb-oauth2-private-key-example "
20+
+ "<connection-string> <oauth2-endpoint> <rsa-private-key.pem>");
21+
return;
22+
}
23+
String connectionString = args[0];
24+
String oauth2Endpoint = args[1];
25+
String keyPemPath = args[2];
26+
27+
OAuth2TokenSource tokenSource = OAuth2TokenSource.withPrivateKeyPemFile(new File(keyPemPath))
28+
.withIssuer("test-issuer") // customize of JWT token
29+
.build();
30+
31+
OAuth2TokenExchangeProvider authProvider = OAuth2TokenExchangeProvider.newBuilder(oauth2Endpoint, tokenSource)
32+
.withScope("demo-scope") // customize of OAuth2 request
33+
.build();
34+
35+
try (GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
36+
.withAuthProvider(authProvider)
37+
.build()) {
38+
try (TableClient tableClient = TableClient.newClient(transport).build()) {
39+
SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build();
40+
41+
DataQueryResult dataQueryResult = retryCtx.supplyResult(
42+
session -> session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
43+
).join().getValue();
44+
45+
ResultSetReader rsReader = dataQueryResult.getResultSet(0);
46+
while (rsReader.next()) {
47+
System.out.println(rsReader.getColumn(0).getInt32());
48+
}
49+
}
50+
}
51+
}
52+
}

auth/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
<artifactId>ydb-sdk-auth-examples</artifactId>
1616
<name>YDB Auth examples</name>
1717
<description>Examples of various auth providers for YDB</description>
18-
18+
1919
<modules>
2020
<module>access_token_credentials</module>
2121
<module>anonymous_credentials</module>
2222
<module>environ</module>
2323
<module>metadata_credentials</module>
2424
<module>service_account_credentials</module>
2525
<module>static_credentials</module>
26+
<module>oauth2_token_exchange/jwt_token</module>
27+
<module>oauth2_token_exchange/private_key</module>
2628
</modules>
2729
</project>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<log4j.version>2.22.1</log4j.version>
1919
<jcommander.version>1.82</jcommander.version>
2020

21-
<ydb.sdk.version>2.2.1</ydb.sdk.version>
21+
<ydb.sdk.version>2.2.3</ydb.sdk.version>
2222
</properties>
2323

2424
<modules>

0 commit comments

Comments
 (0)