All files / libs/plan-agreements/core/src/lib/ext make-google-auth.ts

30% Statements 9/30
100% Branches 0/0
0% Functions 0/1
30% Lines 9/30

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 311x 1x 1x 1x 1x 1x 1x 1x 1x                                            
import { InternalServerErrorException, Logger } from '@nestjs/common';
import { GoogleAuth, Impersonated } from 'google-auth-library';
import { type OAuth2Client } from 'googleapis-common';
 
import { toError } from '@amalia/ext/typescript';
 
import { configuration } from '../../configuration';
 
export const makeGoogleAuth = async () => {
  const logger = new Logger(makeGoogleAuth.name);

  const auth = new GoogleAuth({
    projectId: configuration.planAgreements.projectId,
    scopes: ['https://www.googleapis.com/auth/cloud-platform'],
  });

  const authClient = await auth.getClient().catch((e) => {
    const error = toError(e);
    logger.error(error);

    throw new InternalServerErrorException('An error happened during the auth client retrieval.');
  });

  return new Impersonated({
    sourceClient: authClient,
    targetPrincipal: configuration.planAgreements.serviceAccountToImpersonate,
    lifetime: 600,
    targetScopes: ['https://www.googleapis.com/auth/documents', 'https://www.googleapis.com/auth/drive'],
  }) as unknown as OAuth2Client;
};