# Class: CredentialTokenProvider

## Constructor

#### new CredentialTokenProvider(token, bearer)

  Class used to provide credentials for Token Based Authentication.

  ##### Parameters:

  Name | Type | Argument | Description
  --- | --- | --- | ---
  `token` | string |  |
  `bearer` | string | <optional> |

## Members

#### token :string

  Token to be used used for authentication.

  ##### Type:

  - string

## Methods

#### onAuthenticationChallenge(challenge, callback) → {void}

  Function called after credentials were required.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `challenge` | Object | It is not used right now.
  `callback` | function | Function to be called after credentials are retrieved.

  Inherited From:
    - AvayaClientServices.Config.AbstractCredentialProvider#onAuthenticationChallenge

  ##### Returns:

  { void }

### Callbacks

#### addOnInvalidCredentialsCallback(callback) → {void}

  Adds callback on invalidCredentialsProvided.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `callback` | AvayaClientServices.Config.CredentialTokenProvider#onInvalidCredentialsProvided |

  Inherited From:
    - AvayaClientServices.Config.CredentialTokenProvider#addOnInvalidCredentialsCallback

  ##### Returns:

  { void }

#### removeOnInvalidCredentialsCallback(callback) → {void}

  Removes callback from invalidCredentialsProvided.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `callback` | AvayaClientServices.Config.AbstractCredentialProvider#onInvalidCredentialsProvided |

  Inherited From:
    - AvayaClientServices.Config.AbstractCredentialProvider#removeOnInvalidCredentialsCallback

  ##### Returns:

  { void }

## Type Definitions

#### onInvalidCredentialsProvided(invalidCredentialsEvent) → {void}

  Interface for callback function to be invoked when provided credentials are invalid.

  ##### Examples

  Using callback to retry authentication using existing credentials provider:

      credentialProvider.addOnInvalidCredentialsCallback(function (event) {
          // When consumer application keeps a reference to the credential
          // provider used to configure services, update corresponding field
          // e. g. token or bearer:
          handle.credentialProvider.token= 'secure';

          // Signal to try to re-establish the connection:
          event.retry(handle.credentialProvider); // Optional argument could
                                                  // be omitted only when using
                                                  // the same reference as in
                                                  // the user configuration.
      });

  Using callback to retry authentication using new credential provider:

      credentialProvider.addOnInvalidCredentialsCallback(function (event) {
          // When there is no reference to existing credentials provider,
          // create a new one first. It is allowed to switch to different
          // kind of the provider.
          const credentialProvider = new AvayaClientServices.Config.CredentialTokenProvider(
              'secret',
              'CustomType',
          );

          // It is advised to set up a similar callback when using a new instance:
          credentialProvider.onInvalidCredentialsProvided(function (event) {
              console.error(event.getFailureCount());
          });

          // Signal to save new credentials and try to re-establish the connection:
          event.retry(credentialProvider);
      });

  Using callback to cancel authentication:

      credentialProvider.addOnInvalidCredentialsCallback(function (event) {
          event.cancel();
      });

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `invalidCredentialsEvent` | AvayaClientServices.Config.InvalidCredentialsEvent | Event containing retry and cancel methods, and failure count.

  ##### Returns:

  { void }