UNPKG

@assistant/conversation

Version:
70 lines 2.61 kB
"use strict"; /** * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthHeaderProcessor = void 0; const google_auth_library_1 = require("google-auth-library"); /** * This class provides methods to process the auth header to streamline access to auth credentials. * * @hidden */ class AuthHeaderProcessor { constructor() { this.authClient = new google_auth_library_1.OAuth2Client(); } /** * Validates a GSI token using the provided client ID and returns the GSI token payload as * a json object. * @param gsiToken The GSI token to validate and decode. * @param clientId The Client ID to use for validation. * @returns The decoded token payload. */ async decodeGSIToken(gsiToken, clientId) { const ticket = await this.authClient.verifyIdToken({ idToken: gsiToken, audience: clientId, }); if (ticket instanceof google_auth_library_1.LoginTicket) { return ticket.getPayload(); } else { // Return value from verifIdToken is not supported throw new Error(`Unsupported format after decoding GSI ticket, expected LoginTicket` + `, found ${ticket}`); } } /** * Extracts the token value from a bearer token, stripping the type tag 'Bearer '. * * @param authHeader The auth header to parse. * @returns The value of the token, or a null string if authHeader doesn't contain a bearer token. */ extractAccessToken(authHeader) { let token = ''; if (authHeader.includes(AuthHeaderProcessor.bearerTypeTag)) { const headerParts = authHeader.split(AuthHeaderProcessor.bearerTypeTag); token = headerParts[1]; } return token; } } exports.AuthHeaderProcessor = AuthHeaderProcessor; /** * Used to extract the token value from headers that contain a bearer token. */ AuthHeaderProcessor.bearerTypeTag = 'Bearer '; //# sourceMappingURL=auth.js.map