You are viewing the documentation for an older major version of the AWS SDK for JavaScript.
The modular AWS SDK for JavaScript (v3), the latest major version of AWS SDK for JavaScript, is now stable and recommended for general use. For more information, see the Migration Guide and API Reference.

Class: AWS.TokenProviderChain

Inherits:
AWS.Token show all
Defined in:
lib/token/token_provider_chain.js

Overview

Creates a token provider chain that searches for token in a list of token providers specified by the providers property.

By default, the chain will use the defaultProviders to resolve token.

Setting Providers

Each provider in the providers list should be a function that returns a AWS.Token object, or a hardcoded token object. The function form allows for delayed execution of the Token construction.

Resolving Token from a Chain

Call resolve() to return the first valid token object that can be loaded by the provider chain.

For example, to resolve a chain with a custom provider that checks a file on disk after the set of defaultProviders:

var diskProvider = new FileTokenProvider('./token.json');
var chain = new AWS.TokenProviderChain();
chain.providers.push(diskProvider);
chain.resolve();

The above code will return the diskProvider object if the file contains token and the defaultProviders do not contain any token.

Constructor Summary collapse

Property Summary collapse

Properties inherited from AWS.Token

token, expireTime, expired, expiryWindow

Method Summary collapse

Methods inherited from AWS.Token

needsRefresh, get, getPromise, refreshPromise, refresh

Constructor Details

new AWS.TokenProviderChain(providers) ⇒ void

Creates a new TokenProviderChain with a default set of providers specified by defaultProviders.

Property Details

defaultProvidersObject (static, readwrite)

The default set of providers used by a vanilla TokenProviderChain.

In the browser:

AWS.TokenProviderChain.defaultProviders = []

In Node.js:

AWS.TokenProviderChain.defaultProviders = [
  function () { return new AWS.SSOTokenProvider(); },
]

Default Value:

AWS.TokenProviderChain.defaultProviders = []

providersArray<AWS.Token, Function> (readwrite)

Returns a list of token objects or functions that return token objects. If the provider is a function, the function will be executed lazily when the provider needs to be checked for valid token. By default, this object will be set to the defaultProviders.

Returns:

  • (Array<AWS.Token, Function>)

    a list of token objects or functions that return token objects. If the provider is a function, the function will be executed lazily when the provider needs to be checked for valid token. By default, this object will be set to the defaultProviders.

See Also:

Method Details

resolve(callback) ⇒ AWS.TokenProviderChain

Resolves the provider chain by searching for the first token in providers.

Callback (callback):

  • function(err, token) { ... }

    Called when the provider resolves the chain to a token object or null if no token can be found.

    Parameters:

    • err (Error)

      the error object returned if no token is found.

    • token (AWS.Token)

      the token object resolved by the provider chain.

Returns:

resolvePromise() ⇒ Promise

Returns a 'thenable' promise. Resolves the provider chain by searching for the first token in providers.

Two callbacks can be provided to the then method on the returned promise. The first callback will be called if the promise is fulfilled, and the second callback will be called if the promise is rejected.

Examples:

Calling the resolvePromise method.

var promise = chain.resolvePromise();
promise.then(function(token) { ... }, function(err) { ... });

Callbacks:

  • function(token) { ... }

    Called if the promise is fulfilled and the provider resolves the chain to a token object

    Parameters:

    • token (AWS.Token)

      the token object resolved by the provider chain.

  • function(error) { ... }

    Called if the promise is rejected.

    Parameters:

    • err (Error)

      the error object returned if no token is found.

Returns:

  • (Promise)

    A promise that represents the state of the resolve method call.