Skip to content

Commit ab6b249

Browse files
nsundaragvenzl
andauthored
Update JdbcTokenAuthentication.java (oracle-samples#284)
* Update JdbcTokenAuthentication.java * Update JdbcTokenAuthentication.java * Update JdbcTokenAuthentication.java * Update JdbcTokenAuthentication.java * Update JdbcTokenAuthentication.java * Update JdbcTokenAuthentication.java * Update JdbcTokenAuthentication.java --------- Co-authored-by: Gerald Venzl <gerald.venzl@oracle.com>
1 parent f4aa01d commit ab6b249

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

java/jdbc/ConnectionSamples/JdbcTokenAuthentication.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
*/
4444

4545
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
46+
import com.oracle.bmc.auth.AbstractAuthenticationDetailsProvider;
47+
import com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider;
48+
import com.oracle.bmc.auth.ResourcePrincipalAuthenticationDetailsProvider;
4649
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
4750
import com.oracle.bmc.identitydataplane.DataplaneClient;
4851
import com.oracle.bmc.identitydataplane.model.GenerateScopedAccessTokenDetails;
@@ -157,11 +160,18 @@ private static AccessToken createAccessToken() {
157160
* @return Base 64 encoding of a JWT access token
158161
*/
159162
private static String requestToken(PublicKey publicKey) {
160-
161-
// Read the configuration identified by the OCI_PROFILE
162-
final AuthenticationDetailsProvider authentication;
163+
164+
final AbstractAuthenticationDetailsProvider authentication;
165+
166+
// Instance principal and resource principal authentication are also supported, and
167+
// can be used as shown below.
168+
// authentication = InstancePrincipalsAuthenticationDetailsProvider.builder().build();
169+
// authentication = ResourcePrincipalAuthenticationDetailsProvider.builder().build();
170+
163171
try {
164-
authentication = new ConfigFileAuthenticationDetailsProvider(OCI_PROFILE);
172+
// In this code sample, authentication is shown using a config file.
173+
// Read the configuration identified by the OCI_PROFILE
174+
authentication = new ConfigFileAuthenticationDetailsProvider(OCI_PROFILE);
165175
}
166176
catch (IOException ioException) {
167177
// Not recovering if the profile can not be read
@@ -175,9 +185,18 @@ private static String requestToken(PublicKey publicKey) {
175185

176186
// This scope uses the * character to identify all databases in the cloud
177187
// tenancy of the authenticated user. The * could be replaced with the OCID
178-
// of a compartment, or of a particular database within a compartment
188+
// of a compartment, or of a particular database within a compartment.
189+
// Refer to the examples below.
179190
String scope = "urn:oracle:db::id::*";
180191

192+
// A scope that authorizes access to all databases within a compartment has
193+
// the form: urn:oracle:db::id::<compartment-ocid>
194+
// String scope = "urn:oracle:db::id::ocid1.compartment.oc1..xxxxxxxx"
195+
196+
// A scope that authorizes access to a single database within a compartment
197+
// has the form: urn:oracle:db::id::<compartment-ocid>::<database-ocid>
198+
// String scope = "urn:oracle:db::id::ocid1.compartment.oc1..xxxxxx::ocid1.autonomousdatabase.oc1.phx.xxxxxx"
199+
181200
// Create a GenerateScopedAccessTokenDetails object with the public key
182201
// and the scope
183202
GenerateScopedAccessTokenDetails tokenDetails =
@@ -187,7 +206,7 @@ private static String requestToken(PublicKey publicKey) {
187206
.build();
188207

189208
// Request an access token using a DataplaneClient
190-
try (DataplaneClient client = new DataplaneClient(authentication)) {
209+
try (DataplaneClient client = DataplaneClient.builder().build(authentication)) {
191210
return client.generateScopedAccessToken(
192211
GenerateScopedAccessTokenRequest.builder()
193212
.generateScopedAccessTokenDetails(tokenDetails)

0 commit comments

Comments
 (0)