This repository has been archived by the owner on Nov 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from acquia/oauth-automatic
Automatically fetch OAuth Bearer tokens.
- Loading branch information
Showing
16 changed files
with
663 additions
and
371 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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const axios = require('axios'); | ||
const qs = require('qs'); | ||
|
||
module.exports = class OAuth { | ||
constructor(basePath, OAuthOptions) { | ||
this.basePath = basePath; | ||
this.tokenInformation = Object.assign({}, OAuthOptions); | ||
this.tokenInformation.grant_type = 'password'; // eslint-disable-line camelcase | ||
} | ||
/** | ||
* Get an OAuth Token. | ||
* @return {promise} | ||
* The resolved promise of fetching the oauth token. | ||
*/ | ||
getToken() { | ||
const currentTime = new Date().getTime(); | ||
// Resolve if token already exists and is fresh | ||
if (this.tokenInformation.access_token && ( | ||
this.hasOwnProperty('tokenExpireTime') && | ||
this.tokenExpireTime > currentTime | ||
)) { | ||
return Promise.resolve(); | ||
} | ||
// If token is already being fetched, use that one. | ||
else if (this.bearerPromise) { | ||
return this.bearerPromise; | ||
} | ||
// If token has already been fetched switch grant_type to refresh_token. | ||
else if (this.tokenInformation.access_token) { | ||
this.tokenInformation.grant_type = 'refresh_token'; // eslint-disable-line camelcase | ||
} | ||
|
||
this.bearerPromise = axios({ | ||
method: 'post', | ||
url: `${this.basePath}/oauth/token`, | ||
data: qs.stringify(this.tokenInformation) | ||
}) | ||
.then(response => { | ||
delete this.bearerPromise; | ||
let t = new Date(); | ||
t.setSeconds(+t.getSeconds() + response.data.expires_in); | ||
this.tokenExpireTime = t.getTime(); | ||
Object.assign(this.tokenInformation, response.data); | ||
return response.data; | ||
}) | ||
.catch(e => { | ||
delete this.bearerPromise; | ||
return Promise.reject(e); | ||
}); | ||
|
||
return this.bearerPromise; | ||
} | ||
}; |
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
Oops, something went wrong.