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: remove callbacks for sync APIs #191

Merged
merged 1 commit into from
Nov 25, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
68 changes: 17 additions & 51 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,42 +379,22 @@ export class GoogleAuth {
/**
* Create a credentials instance using the given input options.
* @param {object=} json The input object.
* @param {function=} callback Optional callback.
* @returns Promise that resolves with the OAuth2Client (if no callback is
* passed)
* @returns JWT or UserRefresh Client with data
*/
// TODO: Remove the overloads and just keep this a sync API
fromJSON(json: JWTInput): JWT|UserRefreshClient;
fromJSON(json: JWTInput, callback: CredentialCallback): void;
fromJSON(json: JWTInput, callback?: CredentialCallback): JWT|UserRefreshClient
|void {
fromJSON(json: JWTInput): JWT|UserRefreshClient {
let client: UserRefreshClient|JWT;
try {
if (!json) {
throw new Error(
'Must pass in a JSON object containing the Google auth settings.');
}

this.jsonContent = json;
if (json.type === 'authorized_user') {
client = new UserRefreshClient();
} else {
client = new JWT();
}
client.fromJSON(json);
if (callback) {
return callback(null, client);
} else {
return client;
}

} catch (e) {
if (callback) {
return callback(e);
} else {
throw e;
}
if (!json) {
throw new Error(
'Must pass in a JSON object containing the Google auth settings.');
}
this.jsonContent = json;
if (json.type === 'authorized_user') {
client = new UserRefreshClient();
} else {
client = new JWT();
}
client.fromJSON(json);
return client;
}

/**
Expand Down Expand Up @@ -462,26 +442,12 @@ export class GoogleAuth {
/**
* Create a credentials instance using the given API key string.
* @param {string} - The API key string
* @param {function=} - Optional callback function
* @returns A JWT loaded from the key
*/
fromAPIKey(
apiKey: string,
callback?: (err?: Error|null, client?: JWT) => void): void|JWT {
fromAPIKey(apiKey: string): JWT {
const client = new JWT();
try {
client.fromAPIKey(apiKey);
if (callback) {
callback(null, client);
} else {
return client;
}
} catch (e) {
if (callback) {
callback(e);
} else {
throw e;
}
}
client.fromAPIKey(apiKey);
return client;
}

/**
Expand Down
91 changes: 32 additions & 59 deletions src/auth/jwtaccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,74 +57,47 @@ export class JWTAccess {
* @param {function} metadataCb a callback invoked with the jwt request metadata.
* @returns a Promise that resolves with the request metadata response
*/
getRequestMetadata(authURI: string): RequestMetadataResponse;
getRequestMetadata(authURI: string, callback: RequestMetadataCallback): void;
getRequestMetadata(authURI: string, callback?: RequestMetadataCallback):
void|RequestMetadataResponse {
try {
const iat = Math.floor(new Date().getTime() / 1000);
const exp = iat + 3600; // 3600 seconds = 1 hour
getRequestMetadata(authURI: string): RequestMetadataResponse {
const iat = Math.floor(new Date().getTime() / 1000);
const exp = iat + 3600; // 3600 seconds = 1 hour

// The payload used for signed JWT headers has:
// iss == sub == <client email>
// aud == <the authorization uri>
const payload =
{iss: this.email, sub: this.email, aud: authURI, exp, iat};
const assertion = {
header: {alg: 'RS256'} as jws.Header,
payload,
secret: this.key
};
// The payload used for signed JWT headers has:
// iss == sub == <client email>
// aud == <the authorization uri>
const payload = {iss: this.email, sub: this.email, aud: authURI, exp, iat};
const assertion = {
header: {alg: 'RS256'} as jws.Header,
payload,
secret: this.key
};

// Sign the jwt and invoke metadataCb with it.
const signedJWT =
jws.sign({header: {alg: 'RS256'}, payload, secret: this.key});
const result = {
res: null,
headers: {Authorization: 'Bearer ' + signedJWT}
};
if (callback) {
callback(null, result.headers, null);
} else {
return result;
}
} catch (e) {
if (callback) {
callback(e);
} else {
throw e;
}
}
// Sign the jwt and invoke metadataCb with it.
const signedJWT =
jws.sign({header: {alg: 'RS256'}, payload, secret: this.key});
return {headers: {Authorization: 'Bearer ' + signedJWT}};
}

/**
* Create a JWTAccess credentials instance using the given input options.
* @param {object=} json The input object.
* @param {function=} callback Optional callback.
*/
fromJSON(json: JWTInput, callback?: (err: Error|null) => void) {
try {
if (!json) {
throw new Error(
'Must pass in a JSON object containing the service account auth settings.');
}
if (!json.client_email) {
throw new Error(
'The incoming JSON object does not contain a client_email field');
}
if (!json.private_key) {
throw new Error(
'The incoming JSON object does not contain a private_key field');
}
// Extract the relevant information from the json key file.
this.email = json.client_email;
this.key = json.private_key;
this.projectId = json.project_id;
if (callback) callback(null);
} catch (e) {
if (callback) return callback(e);
throw e;
fromJSON(json: JWTInput): void {
if (!json) {
throw new Error(
'Must pass in a JSON object containing the service account auth settings.');
}
if (!json.client_email) {
throw new Error(
'The incoming JSON object does not contain a client_email field');
}
if (!json.private_key) {
throw new Error(
'The incoming JSON object does not contain a private_key field');
}
// Extract the relevant information from the json key file.
this.email = json.client_email;
this.key = json.private_key;
this.projectId = json.project_id;
}

/**
Expand Down
56 changes: 18 additions & 38 deletions src/auth/jwtclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,24 @@ export class JWT extends OAuth2Client {
/**
* Create a JWT credentials instance using the given input options.
* @param {object=} json The input object.
* @param {function=} callback Optional callback.
*/
fromJSON(json: JWTInput, callback?: (err?: Error) => void): void {
try {
if (!json) {
throw new Error(
'Must pass in a JSON object containing the service account auth settings.');
}
if (!json.client_email) {
throw new Error(
'The incoming JSON object does not contain a client_email field');
}
if (!json.private_key) {
throw new Error(
'The incoming JSON object does not contain a private_key field');
}
// Extract the relevant information from the json key file.
this.email = json.client_email;
this.key = json.private_key;
this.projectId = json.project_id;
} catch (e) {
if (callback) {
callback(e);
} else {
throw e;
}
fromJSON(json: JWTInput): void {
if (!json) {
throw new Error(
'Must pass in a JSON object containing the service account auth settings.');
}
if (!json.client_email) {
throw new Error(
'The incoming JSON object does not contain a client_email field');
}
if (!json.private_key) {
throw new Error(
'The incoming JSON object does not contain a private_key field');
}
if (callback) callback();
// Extract the relevant information from the json key file.
this.email = json.client_email;
this.key = json.private_key;
this.projectId = json.project_id;
}

/**
Expand Down Expand Up @@ -221,22 +211,12 @@ export class JWT extends OAuth2Client {
/**
* Creates a JWT credentials instance using an API Key for authentication.
* @param {string} apiKey - the API Key in string form.
* @param {function=} callback - Optional callback to be invoked after
* initialization.
*/
fromAPIKey(apiKey: string, callback?: (err?: Error) => void): void {
fromAPIKey(apiKey: string): void {
if (!isString(apiKey)) {
const e = new Error('Must provide an API Key string.');
if (callback) {
return setImmediate(callback, e);
} else {
throw e;
}
throw new Error('Must provide an API Key string.');
}
this.apiKey = apiKey;
if (callback) {
return callback();
}
}

/**
Expand Down
58 changes: 24 additions & 34 deletions src/auth/refreshclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,32 @@ export class UserRefreshClient extends OAuth2Client {
* Create a UserRefreshClient credentials instance using the given input
* options.
* @param {object=} json The input object.
* @param {function=} callback Optional callback.
*/
fromJSON(json: JWTInput, callback?: (err?: Error) => void) {
try {
if (!json) {
throw new Error(
'Must pass in a JSON object containing the user refresh token');
}
if (json.type !== 'authorized_user') {
throw new Error(
'The incoming JSON object does not have the "authorized_user" type');
}
if (!json.client_id) {
throw new Error(
'The incoming JSON object does not contain a client_id field');
}
if (!json.client_secret) {
throw new Error(
'The incoming JSON object does not contain a client_secret field');
}
if (!json.refresh_token) {
throw new Error(
'The incoming JSON object does not contain a refresh_token field');
}
this._clientId = json.client_id;
this._clientSecret = json.client_secret;
this._refreshToken = json.refresh_token;
this.credentials.refresh_token = json.refresh_token;
} catch (e) {
if (callback) {
callback(e);
} else {
throw e;
}
fromJSON(json: JWTInput): void {
if (!json) {
throw new Error(
'Must pass in a JSON object containing the user refresh token');
}
if (json.type !== 'authorized_user') {
throw new Error(
'The incoming JSON object does not have the "authorized_user" type');
}
if (!json.client_id) {
throw new Error(
'The incoming JSON object does not contain a client_id field');
}
if (!json.client_secret) {
throw new Error(
'The incoming JSON object does not contain a client_secret field');
}
if (!json.refresh_token) {
throw new Error(
'The incoming JSON object does not contain a refresh_token field');
}
if (callback) callback();
this._clientId = json.client_id;
this._clientSecret = json.client_secret;
this._refreshToken = json.refresh_token;
this.credentials.refresh_token = json.refresh_token;
}

/**
Expand Down
Loading