aboutsummaryrefslogtreecommitdiff
path: root/src/server/apis/google/GoogleApiServerUtils.ts
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2019-09-26 22:17:15 -0400
committerSam Wilkins <samwilkins333@gmail.com>2019-09-26 22:17:15 -0400
commit20540a35be82c34cc3962de4f957d1aa43f8a2b0 (patch)
treea4d67686a1d323bab947fcf3a6d6c2575b576c3e /src/server/apis/google/GoogleApiServerUtils.ts
parente95387732e1fbff49ec035c3bec4b03324d814c8 (diff)
finally transferred google credential management to database
Diffstat (limited to 'src/server/apis/google/GoogleApiServerUtils.ts')
-rw-r--r--src/server/apis/google/GoogleApiServerUtils.ts27
1 files changed, 14 insertions, 13 deletions
diff --git a/src/server/apis/google/GoogleApiServerUtils.ts b/src/server/apis/google/GoogleApiServerUtils.ts
index 665dcf862..f2ce51395 100644
--- a/src/server/apis/google/GoogleApiServerUtils.ts
+++ b/src/server/apis/google/GoogleApiServerUtils.ts
@@ -103,21 +103,21 @@ export namespace GoogleApiServerUtils {
*/
export function authorize(credentials: any, userId: string): Promise<TokenResult> {
const { client_secret, client_id, redirect_uris } = credentials.installed;
- const oAuth2Client = new google.auth.OAuth2(
- client_id, client_secret, redirect_uris[0]);
-
+ const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
return new Promise<TokenResult>((resolve, reject) => {
+ // Attempting to authorize user (${userId})
Database.Auxiliary.GoogleAuthenticationToken.Fetch(userId).then(token => {
- // Check if we have previously stored a token for this userId.
if (!token) {
+ // No token registered, so awaiting input from user
return getNewToken(oAuth2Client, userId).then(resolve, reject);
}
- let parsed: Credentials = parseBuffer(token);
- if (parsed.expiry_date! < new Date().getTime()) {
- return refreshToken(parsed, client_id, client_secret, oAuth2Client, userId).then(resolve, reject);
+ if (token.expiry_date < new Date().getTime()) {
+ // Token has expired, so submitting a request for a refreshed access token
+ return refreshToken(token, client_id, client_secret, oAuth2Client, userId).then(resolve, reject);
}
- oAuth2Client.setCredentials(parsed);
- resolve({ token: parsed, client: oAuth2Client });
+ // Authentication successful!
+ oAuth2Client.setCredentials(token);
+ resolve({ token, client: oAuth2Client });
});
});
}
@@ -134,11 +134,12 @@ export namespace GoogleApiServerUtils {
};
let url = `${refreshEndpoint}?${qs.stringify(queryParameters)}`;
request.post(url, headerParameters).then(async response => {
- let parsed = JSON.parse(response);
- credentials.access_token = parsed.access_token;
- credentials.expiry_date = new Date().getTime() + (parsed.expires_in * 1000);
+ let { access_token, expires_in } = JSON.parse(response);
+ const expiry_date = new Date().getTime() + (expires_in * 1000);
+ await Database.Auxiliary.GoogleAuthenticationToken.Update(userId, access_token, expiry_date);
+ credentials.access_token = access_token;
+ credentials.expiry_date = expiry_date;
oAuth2Client.setCredentials(credentials);
- await Database.Auxiliary.GoogleAuthenticationToken.Write(userId, credentials);
resolve({ token: credentials, client: oAuth2Client });
});
});