import * as assert from 'assert' ; import * as nock from 'nock' ; import * as qs from 'querystring' ; import { GetAccessTokenResponse } from '../src/auth/oauth2client' ; import { OAuthErrorResponse } from '../src/auth/oauth2common' ; import { StsSuccessfulResponse } from '../src/auth/stscredentials' ; import { IamGenerateAccessTokenResponse , ProjectInfo , } from '../src/auth/baseexternalclient' ; interface CloudRequestError { error : { code : number ; message : string ; status : string ; } ; } interface NockMockStsToken { statusCode : number ; response : StsSuccessfulResponse | OAuthErrorResponse ; request : { [ key ] : any ; } ; additionalHeaders ? : { [ key ] : string ; } ; } interface NockMockGenerateAccessToken { statusCode : number ; token : string ; response : IamGenerateAccessTokenResponse | CloudRequestError ; scopes : string [ ] ; } const defaultProjectNumber = '123456' ; const poolId = 'POOL_ID' ; const providerId = 'PROVIDER_ID' ; const baseUrl = 'https://sts.googleapis.com' ; const path = '/v1/token' ; export const saEmail = 'service-1234@service-name.iam.gserviceaccount.com' ; const saBaseUrl = 'https://iamcredentials.googleapis.com' ; const saPath = `/v1/projects/-/serviceAccounts/${ saEmail }:generateAccessToken` ; export function mockStsTokenExchange ( nockParams ) { const scope = nock ( baseUrl ) ; nockParams . forEach ( nockMockStsToken => { const headers = Object . assign ( { 'content-type' : 'application/x-www-form-urlencoded' , } , nockMockStsToken . additionalHeaders || { } ) ; scope . post ( path , qs . stringify ( nockMockStsToken . request ) , { reqheaders : headers , } ) . reply ( nockMockStsToken . statusCode , nockMockStsToken . response ) ; } ) ; return scope ; } export function mockGenerateAccessToken ( nockParams ) { const scope = nock ( saBaseUrl ) ; nockParams . forEach ( nockMockGenerateAccessToken => { const token = nockMockGenerateAccessToken . token ; scope . post ( saPath , { scope : nockMockGenerateAccessToken . scopes , } , { reqheaders : { Authorization : `Bearer ${ token }` , 'Content-Type' : 'application/json' , } , } ) . reply ( nockMockGenerateAccessToken . statusCode , nockMockGenerateAccessToken . response ) ; } ) ; return scope ; } export function getAudience ( projectNumber = defaultProjectNumber ) { return ( `//iam.googleapis.com/projects/${ projectNumber }` + `/locations/global/workloadIdentityPools/${ poolId }/` + `providers/${ providerId }` ) ; } export function getTokenUrl ( ) { return `${ baseUrl }${ path }` ; } export function getServiceAccountImpersonationUrl ( ) { return `${ saBaseUrl }${ saPath }` ; } export function assertGaxiosResponsePresent ( resp ) { const gaxiosResponse = resp . res || { } ; assert ( 'data' in gaxiosResponse && 'status' in gaxiosResponse ) ; } export function mockCloudResourceManager ( projectNumber , accessToken , statusCode , response ) { return nock ( 'https://cloudresourcemanager.googleapis.com' ) . get ( `/v1/projects/${ projectNumber }` , undefined , { reqheaders : { Authorization : `Bearer ${ accessToken }` , } , } ) . reply ( statusCode , response ) ; }