UNPKG

@unito/integration-cli

Version:

Integration CLI

29 lines (28 loc) 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.performOAuth2Flow = performOAuth2Flow; exports.updateToken = updateToken; const tslib_1 = require("tslib"); const oauth2_1 = tslib_1.__importDefault(require("../services/oauth2")); const errors_1 = require("../errors"); const globalConfiguration_1 = require("./globalConfiguration"); async function performOAuth2Flow(applicationCredentials, environment = globalConfiguration_1.Environment.Production, credentialPayload) { const oauthHelper = new oauth2_1.default(applicationCredentials, environment, credentialPayload); const serverUrl = await oauthHelper.startServer(); const healthCheck = await fetch(`${serverUrl}/health`); if (healthCheck.status !== 200) { const error = await healthCheck.text(); throw new Error(`OAuthServer did not start correctly.\n HealthCheck status: ${healthCheck.status}\n Response: ${error}`); } await oauthHelper.authorize(); const oauth2Response = await oauthHelper.callbackIsDone(); await oauthHelper.stopServer(); if (!oauth2Response.accessToken) { throw new errors_1.FailedToRetrieveAccessTokenError('Access token was not returned'); } return oauth2Response; } async function updateToken(applicationCredentials, refreshToken, credentialPayload, environment = globalConfiguration_1.Environment.Production) { const oauthHelper = new oauth2_1.default(applicationCredentials, environment, credentialPayload); return oauthHelper.updateToken(refreshToken); }