@google/clasp
Version:
Develop Apps Script Projects locally
70 lines (69 loc) • 3.15 kB
JavaScript
// Copyright 2025 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
//
// https://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 inquirer from 'inquirer';
import { intl } from '../intl.js';
import { AuthorizationCodeFlow, parseAuthResponseUrl } from './auth_code_flow.js';
/**
* Implements the Authorization Code Flow for environments where a local
* server cannot be started (e.g., serverless functions, some CI environments).
* It prompts the user to manually open the authorization URL in a browser
* on another device and then paste the resulting redirect URL (containing
* the authorization code) back into the CLI.
*/
export class ServerlessAuthorizationCodeFlow extends AuthorizationCodeFlow {
constructor(oauth2client) {
super(oauth2client);
}
/**
* Returns a hardcoded redirect URI.
* This URI is typically configured in the OAuth client settings in GCP Console
* and is used by Google's authorization server to redirect the user after
* successful authorization.
* @returns {Promise<string>} The redirect URI.
*/
async getRedirectUri() {
return 'http://localhost:8888';
}
/**
* Prompts the user to manually open the authorization URL in a browser
* on another device and then paste the resulting redirect URL (which contains
* the authorization code) back into the CLI.
* @param {string} authorizationUrl - The URL to display to the user for authorization.
* @returns {Promise<string>} The authorization code extracted from the URL pasted by the user.
* @throws {Error} If the pasted URL contains an error or no code.
*/
async promptAndReturnCode(authorizationUrl) {
const urlMessage = intl.formatMessage({ id: "7EHKbR", defaultMessage: [{ type: 0, value: "\uD83D\uDD11 Authorize clasp by visiting this url: " }, { type: 1, value: "url" }] }, {
url: authorizationUrl,
});
console.log(urlMessage);
const promptMessage = intl.formatMessage({ id: "xADuBP", defaultMessage: [{ type: 0, value: "After authorizing, copy the URL from your browser and paste it here:" }] });
const answer = await inquirer.prompt([
{
message: promptMessage,
name: 'url',
type: 'input',
},
]);
const { code, error } = parseAuthResponseUrl(answer.url);
if (error) {
throw new Error(error);
}
if (!code) {
const msg = intl.formatMessage({ id: "XvVmSR", defaultMessage: [{ type: 0, value: "Missing code in responde URL" }] });
throw new Error(msg);
}
return code;
}
}