aboutsummaryrefslogtreecommitdiff
path: root/src/server/apis/google/GoogleApiServerUtils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/apis/google/GoogleApiServerUtils.ts')
-rw-r--r--src/server/apis/google/GoogleApiServerUtils.ts71
1 files changed, 30 insertions, 41 deletions
diff --git a/src/server/apis/google/GoogleApiServerUtils.ts b/src/server/apis/google/GoogleApiServerUtils.ts
index 4453b83bf..fc4656bdd 100644
--- a/src/server/apis/google/GoogleApiServerUtils.ts
+++ b/src/server/apis/google/GoogleApiServerUtils.ts
@@ -1,11 +1,11 @@
-import { google } from "googleapis";
-import { OAuth2Client, Credentials, OAuth2ClientOptions } from "google-auth-library";
-import { Opt } from "../../../fields/Doc";
-import { GaxiosResponse } from "gaxios";
-import request = require('request-promise');
-import * as qs from "query-string";
-import { Database } from "../../database";
-import { GoogleCredentialsLoader } from "./CredentialsLoader";
+import { google } from 'googleapis';
+import { OAuth2Client, Credentials, OAuth2ClientOptions } from 'google-auth-library';
+import { Opt } from '../../../fields/Doc';
+import { GaxiosResponse } from 'gaxios';
+import request from 'request-promise';
+import * as qs from 'query-string';
+import { Database } from '../../database';
+import { GoogleCredentialsLoader } from './CredentialsLoader';
/**
* Scopes give Google users fine granularity of control
@@ -13,33 +13,23 @@ import { GoogleCredentialsLoader } from "./CredentialsLoader";
* This is the somewhat overkill list of what Dash requests
* from the user.
*/
-const scope = [
- 'documents.readonly',
- 'documents',
- 'presentations',
- 'presentations.readonly',
- 'drive',
- 'drive.file',
- 'photoslibrary',
- 'photoslibrary.appendonly',
- 'photoslibrary.sharing',
- 'userinfo.profile'
-].map(relative => `https://www.googleapis.com/auth/${relative}`);
+const scope = ['documents.readonly', 'documents', 'presentations', 'presentations.readonly', 'drive', 'drive.file', 'photoslibrary', 'photoslibrary.appendonly', 'photoslibrary.sharing', 'userinfo.profile'].map(
+ relative => `https://www.googleapis.com/auth/${relative}`
+);
/**
* This namespace manages server side authentication for Google API queries, either
* from the standard v1 APIs or the Google Photos REST API.
*/
export namespace GoogleApiServerUtils {
-
/**
* As we expand out to more Google APIs that are accessible from
* the 'googleapis' module imported above, this enum will record
* the list and provide a unified string representation of each API.
*/
export enum Service {
- Documents = "Documents",
- Slides = "Slides",
+ Documents = 'Documents',
+ Slides = 'Slides',
}
/**
@@ -51,7 +41,7 @@ export namespace GoogleApiServerUtils {
let oAuthOptions: OAuth2ClientOptions;
/**
- * This is a global authorization client that is never
+ * This is a global authorization client that is never
* passed around, and whose credentials are never set.
* Its job is purely to generate new authentication urls
* (users will follow to get to Google's permissions GUI)
@@ -64,7 +54,7 @@ export namespace GoogleApiServerUtils {
* This function is called once before the server is started,
* reading in Dash's project-specific credentials (client secret
* and client id) for later repeated access. It also sets up the
- * global, intentionally unauthenticated worker OAuth2 client instance.
+ * global, intentionally unauthenticated worker OAuth2 client instance.
*/
export function processProjectCredentials(): void {
const { client_secret, client_id, redirect_uris } = GoogleCredentialsLoader.ProjectCredentials;
@@ -72,7 +62,7 @@ export namespace GoogleApiServerUtils {
oAuthOptions = {
clientId: client_id,
clientSecret: client_secret,
- redirectUri: redirect_uris[0]
+ redirectUri: redirect_uris[0],
};
worker = generateClient();
}
@@ -98,7 +88,7 @@ export namespace GoogleApiServerUtils {
* A literal union type indicating the valid actions for these 'googleapis'
* requestions
*/
- export type Action = "create" | "retrieve" | "update";
+ export type Action = 'create' | 'retrieve' | 'update';
/**
* An interface defining any entity on which one can invoke
@@ -135,7 +125,7 @@ export namespace GoogleApiServerUtils {
return resolve();
}
let routed: Opt<Endpoint>;
- const parameters: any = { auth, version: "v1" };
+ const parameters: any = { auth, version: 'v1' };
switch (sector) {
case Service.Documents:
routed = google.docs(parameters).documents;
@@ -165,7 +155,7 @@ export namespace GoogleApiServerUtils {
}
let client = authenticationClients.get(userId);
if (!client) {
- authenticationClients.set(userId, client = generateClient(credentials));
+ authenticationClients.set(userId, (client = generateClient(credentials)));
} else if (refreshed) {
client.setCredentials(credentials);
}
@@ -206,11 +196,11 @@ export namespace GoogleApiServerUtils {
* with a Dash user in the googleAuthentication table of the database.
* @param authenticationCode the Google-provided authentication code that the user copied
* from Google's permissions UI and pasted into the overlay.
- *
+ *
* EXAMPLE CODE: 4/sgF2A5uGg4xASHf7VQDnLtdqo3mUlfQqLSce_HYz5qf1nFtHj9YTeGs
- *
+ *
* @returns the information necessary to authenticate a client side google photos request
- * and display basic user information in the overlay on successful authentication.
+ * and display basic user information in the overlay on successful authentication.
* This can be expanded as needed by adding properties to the interface GoogleAuthenticationResult.
*/
export async function processNewUser(userId: string, authenticationCode: string): Promise<EnrichedCredentials> {
@@ -231,7 +221,7 @@ export namespace GoogleApiServerUtils {
/**
* This type represents the union of the full set of OAuth2 credentials
* and all of a Google user's publically available information. This is the strucure
- * of the JSON object we ultimately store in the googleAuthentication table of the database.
+ * of the JSON object we ultimately store in the googleAuthentication table of the database.
*/
export type EnrichedCredentials = Credentials & { userInfo: UserInfo };
@@ -259,14 +249,14 @@ export namespace GoogleApiServerUtils {
* It's pretty cool: the credentials id_token is split into thirds by periods.
* The middle third contains a base64-encoded JSON string with all the
* user info contained in the interface below. So, we isolate that middle third,
- * base64 decode with atob and parse the JSON.
+ * base64 decode with atob and parse the JSON.
* @param credentials the client credentials returned from OAuth after the user
* has executed the authentication routine
* @returns the full set of credentials in the structure in which they'll be stored
* in the database.
*/
function injectUserInfo(credentials: Credentials): EnrichedCredentials {
- const userInfo: UserInfo = JSON.parse(atob(credentials.id_token!.split(".")[1]));
+ const userInfo: UserInfo = JSON.parse(atob(credentials.id_token!.split('.')[1]));
return { ...credentials, userInfo };
}
@@ -279,7 +269,7 @@ export namespace GoogleApiServerUtils {
* @returns the credentials, or undefined if the user has no stored associated credentials,
* and a flag indicating whether or not they were refreshed during retrieval
*/
- export async function retrieveCredentials(userId: string): Promise<{ credentials: Opt<EnrichedCredentials>, refreshed: boolean }> {
+ export async function retrieveCredentials(userId: string): Promise<{ credentials: Opt<EnrichedCredentials>; refreshed: boolean }> {
let credentials = await Database.Auxiliary.GoogleAccessToken.Fetch(userId);
let refreshed = false;
if (!credentials) {
@@ -299,7 +289,7 @@ export namespace GoogleApiServerUtils {
* the Dash user id passed in. In addition to returning the credentials, it
* writes the diff to the database.
* @param credentials the credentials
- * @param userId the id of the Dash user implicitly requesting that
+ * @param userId the id of the Dash user implicitly requesting that
* his/her credentials be refreshed
* @returns the updated credentials
*/
@@ -310,19 +300,18 @@ export namespace GoogleApiServerUtils {
refreshToken: credentials.refresh_token,
client_id,
client_secret,
- grant_type: "refresh_token"
+ grant_type: 'refresh_token',
})}`;
const { access_token, expires_in } = await new Promise<any>(async resolve => {
const response = await request.post(url, headerParameters);
resolve(JSON.parse(response));
});
// expires_in is in seconds, but we're building the new expiry date in milliseconds
- const expiry_date = new Date().getTime() + (expires_in * 1000);
+ const expiry_date = new Date().getTime() + expires_in * 1000;
await Database.Auxiliary.GoogleAccessToken.Update(userId, access_token, expiry_date);
// update the relevant properties
credentials.access_token = access_token;
credentials.expiry_date = expiry_date;
return credentials;
}
-
-} \ No newline at end of file
+}