Oracle Cloud Infrastructure SDK for TypeScript and JavaScript – Authentication Methods

I had to use different authentication methods for the OCI SDK and this post is a way of recording it so that I wont spend so much time in the future πŸ™‚

Option 1: Easy way with local config

This is the option I used the most and since I work mostly in demos and POC, it was more than enough for my needs.

const common = require("oci-common");

const configurationFilePath = "C:/Users/DANIMMAR/Documents/.oci/config";
      const configProfile = "DEFAULT";
      const  opcRequestId = "ZSDZFYMMEQIJ2HXWVI7retsergsrf";
      const provider = new common.ConfigFileAuthenticationDetailsProvider(
      configurationFilePath
); 

Option 2: Instance Principals

This is the choice when we need to authenticate from within an OCI Compute Instance.

const common = require("oci-common");

const provider = await new common.InstancePrincipalsAuthenticationDetailsProviderBuilder().build();

Option 3: Resource Principals

This is the choice when we need to authenticate from within an OCI Function.

const common = require("oci-common");

const provider = await common.ResourcePrincipalAuthenticationDetailsProvider.builder();
  

Option 4: Simple Authentication

I have not used this one, but it is similar to the config option – It gives more control over the parameters we pass to the authenticator.


// TODO: Fill in appropriate values for tenancy (str) / fingerprint (str) / passphrase(optional) (str | null) / privateKey (str) / region (common.Region)
const tenancy = "";
const user = "";
const fingerprint = "";
const passphrase = null; // optional parameter
const privateKey = ``;
const region = common.Region.US_PHOENIX_1; // Change to appropriate region

const provider = new common.SimpleAuthenticationDetailsProvider(
  tenancy,
  user,
  fingerprint,
  privateKey,
  passphrase,
  region
);

Policies

For the above authentication methods and to access the OCI individual services there are associated policies that must be in place. Please refer to the documentation.

Other Useful Links