From 20540a35be82c34cc3962de4f957d1aa43f8a2b0 Mon Sep 17 00:00:00 2001 From: Sam Wilkins Date: Thu, 26 Sep 2019 22:17:15 -0400 Subject: finally transferred google credential management to database --- src/server/apis/google/GoogleApiServerUtils.ts | 27 +++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/server/apis') 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 { 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((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 }); }); }); -- cgit v1.2.3-70-g09d2