Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch to axios #182

Merged
merged 9 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
587 changes: 169 additions & 418 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@
],
"dependencies": {
"gtoken": "^2.0.1",
"axios": "^0.17.1",
"jws": "^3.1.4",
"lodash.isstring": "^4.0.1",
"lodash.merge": "^4.6.0",
"request": "^2.81.0"
"lodash.isstring": "^4.0.1"
},
"devDependencies": {
"@types/jws": "^3.1.0",

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"@types/lodash.isstring": "^4.0.2",
"@types/lodash.merge": "^4.6.2",
"@types/mocha": "^2.2.41",
"@types/nock": "^8.2.1",
"@types/node": "^8.0.47",
"@types/request": "2.0.7",
"coveralls": "^2.13.0",
"gts": "^0.5.1",
"istanbul": "^0.4.5",
Expand Down
9 changes: 3 additions & 6 deletions src/auth/authclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@
* limitations under the License.
*/

import * as request from 'request';

import {AxiosPromise, AxiosRequestConfig} from 'axios';
import {DefaultTransporter} from '../transporters';

import {Credentials} from './credentials';

export abstract class AuthClient {
transporter = new DefaultTransporter();
credentials: Credentials;

/**
* Provides an alternative request
* implementations with auth credentials.
* Provides an alternative Axios request implementation with auth credentials
*/
abstract request(opts: request.Options): request.Request|void;
abstract request<T>(opts: AxiosRequestConfig): AxiosPromise<T>;
}
113 changes: 51 additions & 62 deletions src/auth/computeclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@
* limitations under the License.
*/

import * as request from 'request';

import {BodyResponseCallback, RequestError} from './../transporters';
import {OAuth2Client} from './oauth2client';
import {AxiosError, AxiosPromise, AxiosRequestConfig} from 'axios';
import {RequestError} from './../transporters';
import {GetTokenResponse, OAuth2Client} from './oauth2client';

export interface Token {
expires_in: number;
expiry_date: number;
}

export declare type RefreshTokenCallback =
(err?: Error|null, token?: Token,
response?: request.RequestResponse|null) => void;

export class Compute extends OAuth2Client {
/**
* Google Compute Engine metadata server token endpoint.
Expand All @@ -51,7 +46,7 @@ export class Compute extends OAuth2Client {
/**
* Indicates whether the credential requires scopes to be created by calling
* createdScoped before use.
* @return {object} The cloned instance.
* @return Boolean indicating if scope is required.
*/
createScopedRequired() {
// On compute engine, scopes are specified at the compute instance's
Expand All @@ -62,64 +57,58 @@ export class Compute extends OAuth2Client {

/**
* Refreshes the access token.
* @param {object=} ignored_
* @param {function=} callback Optional callback.
* @param refreshToken Unused parameter
*/
protected refreshToken(ignored: null, callback?: BodyResponseCallback):
request.Request {
const uri = this.opts.tokenUrl || Compute._GOOGLE_OAUTH2_TOKEN_URL;
// request for new token
return this.transporter.request(
{method: 'GET', uri, json: true}, (err, body, response) => {
const token = body as Token;
if (!err && token && token.expires_in) {
token.expiry_date =
((new Date()).getTime() + (token.expires_in * 1000));
delete token.expires_in;
}
if (callback) {
callback(err, token, response);
}
});
protected async refreshToken(refreshToken?: string|
null): Promise<GetTokenResponse> {
try {

This comment was marked as spam.

This comment was marked as spam.

const url = this.opts.tokenUrl || Compute._GOOGLE_OAUTH2_TOKEN_URL;
// request for new token
const res = await this.transporter.request({url});

This comment was marked as spam.

This comment was marked as spam.

const tokens = res.data as Token;
if (tokens && tokens.expires_in) {
tokens.expiry_date =
((new Date()).getTime() + (tokens.expires_in * 1000));
delete tokens.expires_in;
}
return {tokens, res};
} catch (e) {
e.message = 'Could not refresh access token.';
throw e;
}
}

/**
* Inserts a helpful error message guiding the user toward fixing common auth
* issues.
* @param {object} err Error result.
* @param {object} result The result.
* @param {object} response The HTTP response.
* @param {Function} callback The callback.
*/
protected postRequest(
err: Error, result: {}, response: request.RequestResponse,
callback: BodyResponseCallback) {
if (response && response.statusCode) {
let helpfulMessage = null;
if (response.statusCode === 403) {
helpfulMessage =
'A Forbidden error was returned while attempting to retrieve an access ' +
'token for the Compute Engine built-in service account. This may be because the Compute ' +
'Engine instance does not have the correct permission scopes specified.';
} else if (response.statusCode === 404) {
helpfulMessage =
'A Not Found error was returned while attempting to retrieve an access' +
'token for the Compute Engine built-in service account. This may be because the Compute ' +
'Engine instance does not have any permission scopes specified.';
}
if (helpfulMessage) {
if (err && err.message) {
helpfulMessage += ' ' + err.message;
}

if (err) {
err.message = helpfulMessage;
} else {
err = new Error(helpfulMessage);
(err as RequestError).code = response.statusCode;
protected requestAsync<T>(opts: AxiosRequestConfig, retry = false):
AxiosPromise<T> {
return super.requestAsync<T>(opts, retry).catch(e => {
const res = (e as AxiosError).response;
if (res && res.status) {
let helpfulMessage = null;
if (res.status === 403) {
helpfulMessage =
'A Forbidden error was returned while attempting to retrieve an access ' +
'token for the Compute Engine built-in service account. This may be because the Compute ' +
'Engine instance does not have the correct permission scopes specified.';
} else if (res.status === 404) {
helpfulMessage =
'A Not Found error was returned while attempting to retrieve an access' +
'token for the Compute Engine built-in service account. This may be because the Compute ' +
'Engine instance does not have any permission scopes specified.';
}
if (helpfulMessage) {
if (e && e.message && !retry) {
helpfulMessage += ' ' + e.message;
}
if (e) {
e.message = helpfulMessage;
} else {
e = new Error(helpfulMessage);
(e as RequestError).code = res.status.toString();
}
}
}
}
callback(err, result, response);
throw e;
});
}
}
1 change: 1 addition & 0 deletions src/auth/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Credentials {
expiry_date?: number;
access_token?: string;
token_type?: string;
expires_in?: number;

This comment was marked as spam.

This comment was marked as spam.

}

export interface JWTInput {
Expand Down
Loading