UNPKG

@sigstore/cli

Version:
44 lines (43 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OAuthIdentityProvider = void 0; const tslib_1 = require("tslib"); const open_1 = tslib_1.__importDefault(require("open")); const client_1 = require("./client"); const server_1 = require("./server"); class OAuthIdentityProvider { constructor(options) { this.issuer = options.issuer; this.clientID = options.clientID; this.clientSecret = options.clientSecret; let serverOpts; if (options.redirectURL) { const url = new URL(options.redirectURL); serverOpts = { hostname: url.hostname, port: Number(url.port) }; } else { serverOpts = { hostname: 'localhost', port: 0 }; } this.server = new server_1.CallbackServer(serverOpts); } async getToken() { // Start server to receive OAuth callback const serverURL = await this.server.start(); // Initialize OAuth client const client = await (0, client_1.initializeOAuthClient)({ issuer: this.issuer, redirectURL: serverURL, clientID: this.clientID, clientSecret: this.clientSecret, }); // Open browser to OAuth login page (0, open_1.default)(client.authorizationUrl); /* istanbul ignore next */ if (!this.server.callback) { throw new Error('callback server not started'); } // Wait for callback and exchange auth code for ID token return this.server.callback.then((authCode) => client.getIDToken(authCode)); } } exports.OAuthIdentityProvider = OAuthIdentityProvider;