-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert v1 init command to v2 configure command.
- Loading branch information
Showing
86 changed files
with
5,034 additions
and
2,232 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,118 @@ | ||
const { URL } = require('url'); | ||
const queryString = require('querystring'); | ||
const isAfter = require('date-fns/isAfter'); | ||
const parseISO = require('date-fns/parseISO'); | ||
const stringUtils = require('@src/utils/string-utils'); | ||
const CONSTANTS = require('@src/utils/constants'); | ||
const httpClient = require('@src/clients/http-client'); | ||
const providerChainUtils = require('@src/utils/provider-chain-utils'); | ||
|
||
module.exports = class LWAAuthCodeClient { | ||
constructor(config) { | ||
this.config = this._handleDefaultLwaAuthCodeConfiguration(config); | ||
} | ||
|
||
/** | ||
* @param {String} authCode | used for fetching accessTokens | ||
* @param {Function} callback (err, accessToken) | ||
* @returns accessToken | Used for request validation in skill development process. | ||
*/ | ||
getAccessTokenUsingAuthCode(authCode, callback) { | ||
const url = new URL(this.config.tokenPath, this.config.tokenHost); | ||
const body = { | ||
grant_type: 'authorization_code', | ||
redirect_uri: this.config.redirectUri, | ||
client_id: this.config.clientId, | ||
client_secret: this.config.clientConfirmation, | ||
code: authCode | ||
}; | ||
const options = { | ||
url: `${url}`, | ||
method: 'POST', | ||
body, | ||
json: !!body | ||
}; | ||
return httpClient.request(options, 'GET_ACCESS_TOKEN', this.config.doDebug, (err, response) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null, response.body); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {Object} token | accessToken of the profile being used currently. | ||
* @param {Function} callback (err, token) | ||
* @returns accessToken | a new access token. | ||
*/ | ||
refreshToken(token, callback) { | ||
const url = new URL(this.config.tokenPath, this.config.tokenHost); | ||
const body = { | ||
grant_type: 'refresh_token', | ||
refresh_token: token.refresh_token, | ||
client_id: this.config.clientId, | ||
client_secret: this.config.clientConfirmation | ||
}; | ||
const options = { | ||
url: `${url}`, | ||
method: 'POST', | ||
body, | ||
json: !!body | ||
}; | ||
return httpClient.request(options, 'GET_ACCESS_TOKEN_USING_REFRESH_TOKEN', this.config.doDebug, (err, response) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null, response.body); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {Object} token | ||
* @returns boolean | checks validity of a given token | ||
*/ | ||
isValidToken(token) { | ||
return !isAfter(new Date(), parseISO(token.expires_at)); | ||
} | ||
|
||
/** | ||
* @returns {String} authorization code URL | ||
*/ | ||
generateAuthorizeUrl() { | ||
const queryParams = { | ||
response_type: 'code', | ||
client_id: this.config.clientId, | ||
state: this.config.state | ||
}; | ||
if (stringUtils.isNonBlankString(this.config.scope)) { | ||
queryParams.scope = this.config.scope; | ||
} | ||
if (stringUtils.isNonBlankString(this.config.redirectUri)) { | ||
queryParams.redirect_uri = this.config.redirectUri; | ||
} | ||
const baseUrl = new URL(this.config.authorizePath, this.config.authorizeHost); | ||
return `${baseUrl}?${queryString.stringify(queryParams)}`; | ||
} | ||
|
||
/** | ||
* @param {Object} authConfig | ||
* @returns {Object} config | sets default values if some of the values are missing. | ||
* @private | ||
*/ | ||
_handleDefaultLwaAuthCodeConfiguration(authConfig) { | ||
const { doDebug, redirectUri } = authConfig; | ||
const authorizePath = CONSTANTS.LWA.DEFAULT_AUTHORIZE_PATH; | ||
const tokenPath = CONSTANTS.LWA.DEFAULT_TOKEN_PATH; | ||
|
||
// Overwrite LWA options from Environmental Variable | ||
const state = authConfig.state || Date.now(); | ||
const scope = providerChainUtils.resolveProviderChain([authConfig.scope, CONSTANTS.LWA.DEFAULT_SCOPES]); | ||
const clientId = providerChainUtils.resolveProviderChain([authConfig.clientId, process.env.ASK_LWA_CLIENT_ID, | ||
CONSTANTS.LWA.CLI_INTERNAL_ONLY_LWA_CLIENT.CLIENT_ID]); | ||
const clientConfirmation = providerChainUtils.resolveProviderChain([authConfig.clientConfirmation, process.env.ASK_LWA_CLIENT_CONFIRMATION, | ||
CONSTANTS.LWA.CLI_INTERNAL_ONLY_LWA_CLIENT.CLIENT_CONFIRMATION]); | ||
const authorizeHost = providerChainUtils.resolveProviderChain([process.env.ASK_LWA_AUTHORIZE_HOST, CONSTANTS.LWA.DEFAULT_AUTHORIZE_HOST]); | ||
const tokenHost = providerChainUtils.resolveProviderChain([process.env.ASK_LWA_TOKEN_HOST, CONSTANTS.LWA.DEFAULT_TOKEN_HOST]); | ||
return { clientId, clientConfirmation, authorizeHost, tokenHost, authorizePath, tokenPath, scope, state, redirectUri, doDebug }; | ||
} | ||
}; |
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.