brew install -y node oracle-jdk
npm install oracledb
ln -s $HOME/.oracle/instant_client/libclntsh.dylib $HOME/.oracle/nodeJS/node_modules/oracledb/build/Release
$ cat ociam_nodejs.js
const fs = require('fs');
const os = require('os');
const path = require('path');
const oracledb = require('oracledb');
oracledb.initOracleClient()
// Parse out token and private key from token
function getToken() {
// Set the IAM Token and private key path here
const tokenPath = path.join(os.homedir(), '/.oci/db-token/token');
const privateKeyPath = path.join(os.homedir(), '/.oci/db-token/oci_db_key.pem');
let token = '';
let privateKey = '';
try {
// Read token file
token = fs.readFileSync(tokenPath, 'utf8');
// Read private key file
const privateKeyFileContents = fs.readFileSync(privateKeyPath, 'utf-8');
privateKeyFileContents.split(/\r?\n/).forEach(line => {
if (line != '-----BEGIN PRIVATE KEY-----' &&
line != '-----END PRIVATE KEY-----')
privateKey = privateKey.concat(line);
});
} catch (err) {
console.error(err);
} finally {
const tokenBasedAuthData = {
token : token,
privateKey : privateKey
};
return tokenBasedAuthData;
}
}
// Determine if the access token needs to be refreshed
let accessTokenStr;
async function tokenCallback(refresh) {
if (refresh || !accessTokenStr) {
accessTokenStr = await getToken();
}
return accessTokenStr;
}
async function run() {
// Connec to Oracle AI Database and authenticate with OCI IAM db-token
const conn = await oracledb.getConnection({
accessToken : tokenCallback, // the callback returns the token object
externalAuth : true, // must specify external authentication
connectString : process.env.CONNECT_STRING // Oracle AI Database connection string
});
// Execute SQL: Show authenticated user
try {
const result = await conn.execute(`SELECT SYS_CONTEXT ('USERENV','AUTHENTICATED_IDENTITY') FROM DUAL`);
console.log(result.rows);
} finally {
await conn.close();
}
}
run().catch(console.error);
oci session authenticate --profile-name DBUSER --tenancy-name <oci_tenancy_name> --region <oci_region> --identity-provider-name <oci_identity_domain_name>
Please switch to newly opened browser window to log in!
You can also open the following URL in a web browser window to continue:
https://login.us-phoenix-1.oraclecloud.com/v1/oauth2/authorize?action=login...
Service
Completed browser authentication process!
Config written to: /Users/dbuser/.oci/config
oci iam db-token get --profile DBUSER --auth security_token --scope urn:oracle:db::id::<oci_compartment_ocid>::*
export CONNECT_STRING=DEVDB_PDB1_SSL_TOKEN
node ./ociam_nodejs.js
[ [ '<oci_identity_domain_name>/dbuser@dbauthdemo.com' ] ]

No comments:
Post a Comment