All files / src/googleOAuth2/providers googleOAuth2ClientProvider.ts

78.46% Statements 51/65
56.52% Branches 26/46
100% Functions 10/10
77.41% Lines 48/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 1628x 8x       8x 8x 8x   8x     8x     8x             7x 7x   7x       5x       7x         7x   7x 5x             5x         2x   2x 2x   1x   1x   1x 1x 1x 1x   1x   1x                                   6x   6x           6x         6x       6x       6x   6x       6x   6x 5x     1x         1x       6x       6x     6x   6x                     5x   5x       5x       6x    
import { google } from 'googleapis';
import { GaxiosError } from 'gaxios';
import { OAuth2Client } from 'googleapis-common';
import { Credentials } from 'google-auth-library';
import GoogleOAuth2Identifiers from '../types/googleOAuth2Identifiers';
import Constants from '../../constants';
import { FetchError } from 'node-fetch'
import { DependencyInjector } from '../../dependencyInjector';
import ILogger from '../../core/contracts/ILogger';
import { injectables } from '../../core/types/injectables';
import GoogleOAuth2TokensRepository from '../repositories/googleOAuth2TokensRepository';
import IUsesGoogleOAuth2 from '../contracts/IUsesGoogleOAuth2';
import { injectable } from 'inversify';
 
@injectable()
export default class GoogleOAuth2ClientProvider implements IUsesGoogleOAuth2 {
    private readonly logger;
    private readonly googleOAuth2TokensRepository;
 
    private oauth2Client: OAuth2Client;
 
    public constructor() {
        this.logger = DependencyInjector.Singleton.resolve<ILogger>(injectables.ILogger);
        this.googleOAuth2TokensRepository = DependencyInjector.Singleton.resolve<GoogleOAuth2TokensRepository>(injectables.GoogleOAuth2TokensRepository);
 
        this.oauth2Client = null!;
    }
 
    public get client() {
        return this.oauth2Client;
    }
 
    public async useOAuth2IdentifiersAsync(identifiers: GoogleOAuth2Identifiers) {
        this.oauth2Client = new google.auth.OAuth2(
            process.env.GOOGLE_OAUTH2_CLIENT_ID,
            process.env.GOOGLE_OAUTH2_CLIENT_SECRET,
            identifiers.redirectUri ?? Constants.defaultRedirectUri);
 
        this.oauth2Client.on('tokens', (tokens) => this.tryHandleTokensAsync(tokens));
 
        if (identifiers.accessToken !== undefined) {
            const tokens: Credentials = {
                scope: Constants.scopes.join(' '),
                token_type: "Bearer",
                access_token: identifiers.accessToken,
                refresh_token: identifiers.refreshToken
            };
 
            await this.tryHandleTokensAsync(tokens);
        }
    }
 
    public async tryAuthorizeAsync(code: string) {
        this.logger.log(`Received authorization request`, { authorization_code: code });
 
        try {
            const response = await this.oauth2Client.getToken(code); // triggers 'tokens' event
 
            this.logger.log(`Authorization successful`, { access_token: response.tokens.access_token });
 
            return response.tokens;
        } catch(ex) {
            if (ex instanceof GaxiosError) {
                const error = String(ex.response?.data.error);
                const error_description = String(ex.response?.data.error_description);
                const innerError = new Error(`Axios error encountered during authorization: ${error} (${error_description})`);
 
                this.logger.error(innerError, { authorization_code: code });
 
                throw innerError;
            } else IEif (ex instanceof FetchError) {
                Iif (ex.type == 'system') {
                    const innerError = new Error(`System error ${ex.errno} encountered during network fetch: ${ex.message}`);
 
                    this.logger.error(innerError, { authorization_code: code });
 
                    throw innerError;
                }
            }
            
            this.logger.error(ex as Error, { authorization_code: code });
 
            throw ex;
        }
    }
 
    private async tryHandleTokensAsync(tokens: Credentials) {
        const refreshToken = await this.tryResolveRefreshTokenOrNullAsync(tokens);
 
        const refreshableTokens: Credentials = {
            ...tokens,
 
            refresh_token: refreshToken,
        };
 
        this.logger.log(`Using OAuth2 Client tokens`, {
            access_token: tokens.access_token,
            ...(tokens.refresh_token !== undefined) && { refresh_token: tokens.refresh_token}
        });
 
        this.authenticate(refreshableTokens);
    }
 
    private async tryResolveRefreshTokenOrNullAsync(tokens: Credentials) {
        Iif (tokens.access_token === undefined || tokens.access_token === null) {
            throw new Error(`No access token received`);
        }
 
        const userEmail = await this.resolveEmailOrNullAsync(tokens.access_token);
 
        Iif (userEmail === null) {
            return tokens.refresh_token ?? null;
        }
 
        const refreshToken = tokens.refresh_token ?? await this.resolvePersistedRefreshToken(userEmail);
 
        if (refreshToken === undefined) {
            return null;
        }
 
        await this.googleOAuth2TokensRepository.createOrUpdateAsync(
            userEmail,
            tokens.access_token,
            refreshToken);
 
        return refreshToken;
    }
 
    private async resolveEmailOrNullAsync(accessToken?: string) {
        Iif(accessToken === undefined) {
            return null;
        }
 
        try  {
            // https://cloud.google.com/nodejs/docs/reference/google-auth-library/latest#checking-accesstoken-information
            // This method will throw if the token is invalid.
            const tokenInfo = await this.oauth2Client.getTokenInfo(accessToken);
 
            return tokenInfo.email ?? null;
        } catch(ex) {
            const error = ex as Error;
 
            this.logger.warn(`Failed to get token info: ${error.message ?? String(ex) }`, { access_token: accessToken });
 
            return null;
        }
    }
 
    private async resolvePersistedRefreshToken(userEmail: string) {
        const persistedIdentifiers = await this.googleOAuth2TokensRepository.getOrNullAsync(userEmail);
 
        Iif (persistedIdentifiers !== null) {
            this.logger.log(`Using persisted OAuth2 refresh token`, { refresh_token: persistedIdentifiers.refreshToken });
        }
        
        return persistedIdentifiers?.refreshToken;
    }
 
    private authenticate(tokens: Credentials) {
        this.oauth2Client.setCredentials(tokens);
    }
}