import { HttpHeaders } from "../httpHeaders" ; import { Constants } from "../util/constants" ; import { WebResourceLike } from "../webResource" ; import { ServiceClientCredentials } from "./serviceClientCredentials" ; const HeaderConstants = Constants . HeaderConstants ; const DEFAULT_AUTHORIZATION_SCHEME = "Bearer" ; /**
 * A credentials object that uses a token string and a authorzation scheme to authenticate.
 */ export class TokenCredentials implements ServiceClientCredentials { token : string ; authorizationScheme : string = DEFAULT_AUTHORIZATION_SCHEME ; constructor param { string } token param { string } authorizationScheme constructor ( token , authorizationScheme = DEFAULT_AUTHORIZATION_SCHEME ) { if ( ! token ) { throw new Error ( "token cannot be null or undefined." ) ; } this . token = token ; this . authorizationScheme = authorizationScheme ; } param { WebResourceLike } webResource return { Promise < WebResourceLike > } signRequest ( webResource ) { if ( ! webResource . headers ) webResource . headers = new HttpHeaders ( ) ; webResource . headers . set ( HeaderConstants . AUTHORIZATION , `${ this . authorizationScheme } ${ this . token }` ) ; return Promise . resolve ( webResource ) ; } }