diff options
Diffstat (limited to 'src/server/apis')
| -rw-r--r-- | src/server/apis/google/GoogleApiServerUtils.ts | 24 | 
1 files changed, 11 insertions, 13 deletions
diff --git a/src/server/apis/google/GoogleApiServerUtils.ts b/src/server/apis/google/GoogleApiServerUtils.ts index 24905896d..ad0f0e580 100644 --- a/src/server/apis/google/GoogleApiServerUtils.ts +++ b/src/server/apis/google/GoogleApiServerUtils.ts @@ -6,7 +6,6 @@ import * as request from 'request-promise';  import { Opt } from '../../../fields/Doc';  import { Database } from '../../database';  import { GoogleCredentialsLoader } from './CredentialsLoader'; -import { DashUserModel } from '../../authentication/DashUserModel';  /**   * Scopes give Google users fine granularity of control @@ -59,7 +58,6 @@ export namespace GoogleApiServerUtils {       */      export function processProjectCredentials(): void {          const { client_secret: clientSecret, client_id: clientId, redirect_uris: redirectUris } = GoogleCredentialsLoader.ProjectCredentials; -        console.log('Loaded Google redirect URIs:', redirectUris);          // initialize the global authorization client          oAuthOptions = {              clientId, @@ -120,8 +118,8 @@ export namespace GoogleApiServerUtils {       * @param userId the id of the Dash user making the request to the API       * @returns the relevant 'googleapis' wrapper, if any       */ -    export async function GetEndpoint(sector: string, user: DashUserModel): Promise<Endpoint | void> { -        const auth = await retrieveOAuthClient(user); +    export async function GetEndpoint(sector: string, userId: string): Promise<Endpoint | void> { +        const auth = await retrieveOAuthClient(userId);          if (!auth) {              return;          } @@ -147,14 +145,14 @@ export namespace GoogleApiServerUtils {       * npm-installed API wrappers that use authenticated client instances rather than access codes for       * security.       */ -    export async function retrieveOAuthClient(user: DashUserModel): Promise<OAuth2Client | void> { -        const { credentials, refreshed } = await retrieveCredentials(user); +    export async function retrieveOAuthClient(userId: string): Promise<OAuth2Client | void> { +        const { credentials, refreshed } = await retrieveCredentials(userId);          if (!credentials) {              return;          } -        let client = authenticationClients.get(user.id); +        let client = authenticationClients.get(userId);          if (!client) { -            authenticationClients.set(user.id, (client = generateClient(credentials))); +            authenticationClients.set(userId, (client = generateClient(credentials)));          } else if (refreshed) {              client.setCredentials(credentials);          } @@ -269,15 +267,15 @@ 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(user: DashUserModel): Promise<{ credentials: Opt<EnrichedCredentials>; refreshed: boolean }> { -        let credentials = await Database.Auxiliary.GoogleAccessToken.Fetch(user.id); +    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) {              return { credentials: undefined, refreshed };          }          // check for token expiry          if (credentials.expiry_date! <= new Date().getTime()) { -            credentials = { ...credentials, ...(await refreshAccessToken(credentials, user)) }; +            credentials = { ...credentials, ...(await refreshAccessToken(credentials, userId)) };              refreshed = true;          }          return { credentials, refreshed }; @@ -293,7 +291,7 @@ export namespace GoogleApiServerUtils {       * his/her credentials be refreshed       * @returns the updated credentials       */ -    async function refreshAccessToken(credentials: Credentials, user: DashUserModel): Promise<Credentials> { +    async function refreshAccessToken(credentials: Credentials, userId: string): Promise<Credentials> {          const headerParameters = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } };          const { client_id, client_secret } = GoogleCredentialsLoader.ProjectCredentials;          const params = new URLSearchParams({ @@ -309,7 +307,7 @@ export namespace GoogleApiServerUtils {          // expires_in is in seconds, but we're building the new expiry date in milliseconds          const expiry_date = new Date().getTime() + expires_in * 1000; -        await Database.Auxiliary.GoogleAccessToken.Update(user.id, access_token, expiry_date); +        await Database.Auxiliary.GoogleAccessToken.Update(userId, access_token, expiry_date);          // update the relevant properties          credentials.access_token = access_token;          credentials.expiry_date = expiry_date;  | 
