Skip to content

Commit

Permalink
feat: convert v1 init command to v2 configure command.
Browse files Browse the repository at this point in the history
  • Loading branch information
pbheemag committed Feb 17, 2020
1 parent 593420e commit 8268de8
Show file tree
Hide file tree
Showing 86 changed files with 5,034 additions and 2,232 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $ npm install -g ask-cli-x
Before you can start using the ASK CLI, configure your ASK (and AWS) credentials:

```
$ askx init
$ askx configure
```

You’ll be prompted to sign into your Amazon developer account. If you choose to have your skill hosted by AWS, you’ll have the option of linking your AWS account as well.
Expand Down
4 changes: 2 additions & 2 deletions bin/askx.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (!require('semver').gte(process.version, '8.3.0')) {
require('module-alias/register');
const commander = require('commander');

require('@src/commands/init').createCommand(commander);
require('@src/commands/configure').createCommand(commander);
require('@src/commands/deploy').createCommand(commander);
require('@src/commands/v2new').createCommand(commander);

Expand All @@ -21,7 +21,7 @@ commander
.version(require('../package.json').version)
.parse(process.argv);

const ALLOWED_ASK_ARGV_2 = ['api', 'init', 'deploy', 'new', 'util', 'help', '-v', '--version', '-h', '--help'];
const ALLOWED_ASK_ARGV_2 = ['api', 'configure', 'deploy', 'new', 'util', 'help', '-v', '--version', '-h', '--help'];
if (process.argv[2] && ALLOWED_ASK_ARGV_2.indexOf(process.argv[2]) === -1) {
console.log('Command not recognized. Please run "askx" to check the user instructions.');
}
21 changes: 0 additions & 21 deletions lib/api/api-wrapper.js

This file was deleted.

126 changes: 0 additions & 126 deletions lib/api/request-wrapper.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/builtins/deploy-delegates/cfn-deployer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function invoke(reporter, options, callback) {
const { profile, alexaRegion, skillId, skillName, code, userConfig, deployState } = options;
const awsProfile = awsUtil.getAWSProfile(profile);
if (!awsProfile) {
return callback(`Profile [${profile}] doesn't have AWS profile linked to it. Please run "ask init" to re-configure your porfile.`);
return callback(`Profile [${profile}] doesn't have AWS profile linked to it. Please run "ask configure" to re-configure your porfile.`);
}

let currentRegionDeployState = deployState[alexaRegion];
Expand Down
2 changes: 1 addition & 1 deletion lib/builtins/deploy-delegates/lambda-deployer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function invoke(reporter, options, callback) {
const { profile, alexaRegion, skillId, skillName, code, userConfig, deployState } = options;
const awsProfile = awsUtil.getAWSProfile(profile);
if (!stringUtils.isNonBlankString(awsProfile)) {
return callback(`Profile [${profile}] doesn't have AWS profile linked to it. Please run "ask init" to re-configure your profile.`);
return callback(`Profile [${profile}] doesn't have AWS profile linked to it. Please run "ask configure" to re-configure your porfile.`);
}
let currentRegionDeployState = deployState[alexaRegion];
if (!currentRegionDeployState) {
Expand Down
5 changes: 5 additions & 0 deletions lib/clients/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ function request(options, operation, doDebug, callback) {
return;
}

const proxyUrl = process.env.ASK_CLI_PROXY;
if (stringUtils.isNonBlankString(proxyUrl)) {
requestOptions.proxy = proxyUrl;
}

// Set user-agent for each CLI request
if (!requestOptions.headers) {
requestOptions.headers = {};
Expand Down
118 changes: 118 additions & 0 deletions lib/clients/lwa-auth-code-client/index.js
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 };
}
};
21 changes: 17 additions & 4 deletions lib/clients/smapi-client/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const R = require('ramda');
const async = require('async');
const querystring = require('querystring');
const AuthorizationController = require('@src/controllers/authorization-controller');
const httpClient = require('@src/clients/http-client');
const oauthWrapper = require('@src/utils/oauth-wrapper');
const CONSTANTS = require('@src/utils/constants');

const accountLinkingApi = require('./resources/account-linking');
Expand Down Expand Up @@ -73,7 +73,14 @@ module.exports = class SmapiClient {
headers: EMPTY_HEADERS,
body: NULL_PAYLOAD,
};
oauthWrapper.tokenRefreshAndRead(requestOptions, this.profile, () => {
const authorizationController = new AuthorizationController({
auth_client_type: 'LWA'
});
authorizationController.tokenRefreshAndRead(this.profile, (tokenErr, token) => {
if (tokenErr) {
return callback(tokenErr);
}
requestOptions.headers.authorization = token;
httpClient.request(requestOptions, 'REDIRECT_URL', this.doDebug, (reqErr, reqResponse) => {
if (reqErr) {
return callback(reqErr);
Expand Down Expand Up @@ -142,8 +149,14 @@ module.exports = class SmapiClient {
body: payload,
json: !!payload
};
// TODO: Improve oauthWrapper module since current tokenRefreshAndRead is short circuit style.
oauthWrapper.tokenRefreshAndRead(requestOptions, this.profile, () => {
const authorizationController = new AuthorizationController({
auth_client_type: 'LWA'
});
authorizationController.tokenRefreshAndRead(this.profile, (tokenErr, token) => {
if (tokenErr) {
return callback(tokenErr);
}
requestOptions.headers.authorization = token;
httpClient.request(requestOptions, apiName, this.doDebug, (reqErr, reqResponse) => {
if (reqErr) {
return callback(reqErr);
Expand Down
Loading

0 comments on commit 8268de8

Please sign in to comment.