/**
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import { JsonObject } from './common' ; import { OAuth2Client , LoginTicket } from 'google-auth-library' ; hidden export class AuthHeaderProcessor { /**
     * Used to verify and decode GSI tokens.
     */ authClient : OAuth2Client ; /**
     * Used to extract the token value from headers that contain a bearer token.
     */ static readonly bearerTypeTag = 'Bearer ' ; constructor ( ) { this . authClient = new OAuth2Client ( ) ; } param gsiToken param clientId returns async decodeGSIToken ( gsiToken , clientId ) { const ticket = await this . authClient . verifyIdToken ( { idToken : gsiToken , audience : clientId , } ) ; if ( ticket instanceof LoginTicket ) { return ticket . getPayload ( ) as JsonObject ; } else { throw new Error ( `Unsupported format after decoding GSI ticket, expected LoginTicket` + `, found ${ ticket }` ) ; } } param authHeader returns extractAccessToken ( authHeader ) { let token = '' ; if ( authHeader . includes ( AuthHeaderProcessor . bearerTypeTag ) ) { const headerParts = authHeader . split ( AuthHeaderProcessor . bearerTypeTag ) ; token = headerParts [ 1 ] ; } return token ; } }