forked from microsoft/botbuilder-js
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93d6f1d
commit dedd0ef
Showing
8 changed files
with
46 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 28 additions & 31 deletions
59
libraries/botframework-connector/src/auth/tokenCredentials.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,42 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
import { HttpHeaders, Constants, WebResourceLike, ServiceClientCredentials } from "@azure/core-http"; | ||
import { HttpHeaders, Constants, WebResourceLike, ServiceClientCredentials } from '@azure/core-http'; | ||
|
||
const HeaderConstants = Constants.HeaderConstants; | ||
const DEFAULT_AUTHORIZATION_SCHEME = "Bearer"; | ||
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; | ||
token: string; | ||
authorizationScheme: string = DEFAULT_AUTHORIZATION_SCHEME; | ||
|
||
/** | ||
* Creates a new TokenCredentials object. | ||
* | ||
* @constructor | ||
* @param {string} token The token. | ||
* @param {string} [authorizationScheme] The authorization scheme. | ||
*/ | ||
constructor(token: string, authorizationScheme: string = DEFAULT_AUTHORIZATION_SCHEME) { | ||
if (!token) { | ||
throw new Error("token cannot be null or undefined."); | ||
/** | ||
* Creates a new TokenCredentials object. | ||
* | ||
* @class | ||
* @param {string} token The token. | ||
* @param {string} [authorizationScheme] The authorization scheme. | ||
*/ | ||
constructor(token: string, authorizationScheme: string = DEFAULT_AUTHORIZATION_SCHEME) { | ||
if (!token) { | ||
throw new Error('token cannot be null or undefined.'); | ||
} | ||
this.token = token; | ||
this.authorizationScheme = authorizationScheme; | ||
} | ||
this.token = token; | ||
this.authorizationScheme = authorizationScheme; | ||
} | ||
|
||
/** | ||
* Signs a request with the Authentication header. | ||
* | ||
* @param {WebResourceLike} webResource The WebResourceLike to be signed. | ||
* @return {Promise<WebResourceLike>} The signed request object. | ||
*/ | ||
signRequest(webResource: WebResourceLike) { | ||
if (!webResource.headers) webResource.headers = new HttpHeaders(); | ||
webResource.headers.set( | ||
HeaderConstants.AUTHORIZATION, | ||
`${this.authorizationScheme} ${this.token}` | ||
); | ||
return Promise.resolve(webResource); | ||
} | ||
/** | ||
* Signs a request with the Authentication header. | ||
* | ||
* @param {WebResourceLike} webResource The WebResourceLike to be signed. | ||
* @returns {Promise<WebResourceLike>} The signed request object. | ||
*/ | ||
signRequest(webResource: WebResourceLike) { | ||
if (!webResource.headers) webResource.headers = new HttpHeaders(); | ||
webResource.headers.set(HeaderConstants.AUTHORIZATION, `${this.authorizationScheme} ${this.token}`); | ||
return Promise.resolve(webResource); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters