@unito/integration-cli 0.57.2 → 0.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.eslintrc.d.ts +6 -0
- package/dist/.eslintrc.js +9 -2
- package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/src/index.ts +1 -3
- package/dist/src/commands/dev.d.ts +4 -0
- package/dist/src/commands/dev.js +40 -4
- package/dist/src/commands/init.js +2 -2
- package/dist/src/commands/login.js +1 -1
- package/dist/src/commands/oauth2.js +15 -10
- package/dist/src/commands/test.js +28 -6
- package/dist/src/errors.d.ts +8 -0
- package/dist/src/errors.js +35 -18
- package/dist/src/resources/configuration.js +0 -2
- package/dist/src/resources/credentials.d.ts +3 -0
- package/dist/src/resources/credentials.js +26 -0
- package/dist/src/resources/integrations.d.ts +1 -0
- package/dist/src/resources/integrations.js +37 -2
- package/dist/src/resources/oauth2Helper.d.ts +4 -0
- package/dist/src/resources/oauth2Helper.js +30 -0
- package/dist/src/services/integrationsPlatform.d.ts +1 -0
- package/dist/src/services/integrationsPlatform.js +5 -1
- package/dist/src/services/oauth2Helper.d.ts +77 -3
- package/dist/src/services/oauth2Helper.js +228 -29
- package/dist/test/commands/dev.test.js +21 -0
- package/dist/test/commands/init.test.js +4 -4
- package/dist/test/commands/login.test.js +1 -0
- package/dist/test/commands/oauth2.test.js +2 -1
- package/dist/test/commands/publish.test.js +1 -0
- package/dist/test/commands/test.test.js +22 -0
- package/dist/test/commands/upgrade.test.js +1 -0
- package/dist/test/oauth2Helper/oauth2Helper.test.js +111 -127
- package/dist/test/resources/integrations.test.js +37 -0
- package/dist/test/{services → resources}/oauth2Helper.test.js +12 -42
- package/oclif.manifest.json +64 -9
- package/package.json +8 -8
- package/dist/integrationGenerator/errors.d.ts +0 -2
- package/dist/integrationGenerator/errors.js +0 -6
- package/dist/integrationGenerator/index.d.ts +0 -2
- package/dist/integrationGenerator/index.js +0 -5
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.dockerignore +0 -3
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.eslintrc.js +0 -74
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.nvmrc +0 -1
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.prettierignore +0 -1
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.prettierrc +0 -7
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.unito.json +0 -1
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/Dockerfile +0 -38
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/README.md +0 -21
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/src/index.ts +0 -94
- package/dist/integrationGenerator/integrationBoilerplate/package.json +0 -43
- package/dist/integrationGenerator/integrationBoilerplate/src/logger.ts +0 -55
- package/dist/integrationGenerator/integrationBoilerplate/src/middlewares/additionalLoggingContext.ts +0 -22
- package/dist/integrationGenerator/integrationBoilerplate/src/middlewares/correlationId.ts +0 -13
- package/dist/integrationGenerator/integrationBoilerplate/src/middlewares/credentials.ts +0 -38
- package/dist/integrationGenerator/integrationBoilerplate/src/request.ts +0 -59
- package/dist/integrationGenerator/integrationBoilerplate/src/routes/index.ts +0 -11
- package/dist/integrationGenerator/integrationBoilerplate/src/routes/me.ts +0 -15
- package/dist/integrationGenerator/integrationBoilerplate/src/routes/root.ts +0 -12
- package/dist/integrationGenerator/integrationBoilerplate/tsconfig.json +0 -37
- package/dist/integrationGenerator/src/index.d.ts +0 -1
- package/dist/integrationGenerator/src/index.js +0 -5
- package/dist/integrationGenerator/src/resources/index.d.ts +0 -1
- package/dist/integrationGenerator/src/resources/index.js +0 -5
- package/dist/integrationGenerator/src/resources/integration.d.ts +0 -9
- package/dist/integrationGenerator/src/resources/integration.js +0 -60
- package/dist/integrationGenerator/test/resources/integration.test.js +0 -50
- package/dist/src/oauth2Helper/oauth2Helper.d.ts +0 -65
- package/dist/src/oauth2Helper/oauth2Helper.js +0 -255
- package/dist/src/oauth2Helper/types.d.ts +0 -22
- package/dist/src/oauth2Helper/types.js +0 -2
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.dockerignore +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.eslintrc.js +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.nvmrc +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.prettierignore +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.prettierrc +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.unito.json +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/Dockerfile +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/README.md +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/package.json +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/logger.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/middlewares/additionalLoggingContext.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/middlewares/correlationId.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/middlewares/credentials.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/request.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/routes/index.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/routes/me.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/routes/root.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/tsconfig.json +0 -0
- /package/dist/{integrationGenerator/test/resources/integration.test.d.ts → test/resources/integrations.test.d.ts} +0 -0
- /package/dist/test/{services → resources}/oauth2Helper.test.d.ts +0 -0
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const strict_1 = tslib_1.__importDefault(require("node:assert/strict"));
|
|
5
|
-
const path = tslib_1.__importStar(require("path"));
|
|
6
|
-
const sinon = tslib_1.__importStar(require("sinon"));
|
|
7
|
-
const tmp_1 = tslib_1.__importDefault(require("tmp"));
|
|
8
|
-
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
9
|
-
const Integration = tslib_1.__importStar(require("../../src/resources/integration"));
|
|
10
|
-
describe('Integration Resource Generation', () => {
|
|
11
|
-
let tempDir;
|
|
12
|
-
const boilerplatePath = path.join(__dirname, '../../integrationBoilerplate');
|
|
13
|
-
afterEach(() => {
|
|
14
|
-
sinon.restore();
|
|
15
|
-
});
|
|
16
|
-
describe('generateIntegrationSkeleton', () => {
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
tempDir = tmp_1.default.dirSync({ prefix: 'outputIntegrations-' }).name;
|
|
19
|
-
});
|
|
20
|
-
afterEach(async () => {
|
|
21
|
-
await fs_1.default.promises.rm(tempDir, { recursive: true });
|
|
22
|
-
});
|
|
23
|
-
it('should not create a new integration directory if it already exists', async () => {
|
|
24
|
-
const existingIntegrationName = 'existingIntegration';
|
|
25
|
-
await Integration.generateIntegrationSkeleton(existingIntegrationName, tempDir);
|
|
26
|
-
try {
|
|
27
|
-
await Integration.generateIntegrationSkeleton(existingIntegrationName, tempDir);
|
|
28
|
-
}
|
|
29
|
-
catch (e) {
|
|
30
|
-
(0, strict_1.default)(e.message.includes('already exists with this name'));
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
it('should create a new integration directory if it does not exist', async () => {
|
|
34
|
-
const integrationName = 'NewIntegration';
|
|
35
|
-
const result = await Integration.generateIntegrationSkeleton(integrationName, tempDir);
|
|
36
|
-
strict_1.default.equal(result, path.join(tempDir, integrationName.toLowerCase()));
|
|
37
|
-
(0, strict_1.default)(fs_1.default.existsSync(result));
|
|
38
|
-
const expectedFiles = await fs_1.default.promises.readdir(boilerplatePath);
|
|
39
|
-
expectedFiles.push('.npmrc');
|
|
40
|
-
const createdFiles = await fs_1.default.promises.readdir(result);
|
|
41
|
-
strict_1.default.deepEqual(createdFiles, expectedFiles.sort());
|
|
42
|
-
createdFiles.forEach(createdField => (0, strict_1.default)(expectedFiles.includes(createdField)));
|
|
43
|
-
});
|
|
44
|
-
it('generates an .npmrc file', async () => {
|
|
45
|
-
const result = await Integration.generateIntegrationSkeleton('foo', tempDir);
|
|
46
|
-
const npmrc = await fs_1.default.promises.readFile(`${result}/.npmrc`, 'utf-8');
|
|
47
|
-
(0, strict_1.default)(npmrc.includes('//npm.pkg.github.com/:_authToken=${UNITO_GITHUB_PKG_TOKEN}'));
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import * as openUrl from 'openurl';
|
|
2
|
-
import { Oauth2Response, Oauth2Payload } from './types';
|
|
3
|
-
import { Environment } from '../resources/globalConfiguration';
|
|
4
|
-
export declare const open: typeof openUrl;
|
|
5
|
-
declare class OAuth2Helper {
|
|
6
|
-
private environment;
|
|
7
|
-
private server;
|
|
8
|
-
private clientId;
|
|
9
|
-
private clientSecret;
|
|
10
|
-
private providerAuthorizationUrl;
|
|
11
|
-
private tokenUrl;
|
|
12
|
-
private scopes;
|
|
13
|
-
private grantType;
|
|
14
|
-
private requestContentType;
|
|
15
|
-
private responseContentType;
|
|
16
|
-
private refreshToken?;
|
|
17
|
-
private code;
|
|
18
|
-
private serverUrl;
|
|
19
|
-
private tokenRequestParameters;
|
|
20
|
-
private refreshRequestParameters;
|
|
21
|
-
/**
|
|
22
|
-
* Constructs an instance of OAuthHelper.
|
|
23
|
-
* @param clientId The client ID for your OAuth application.
|
|
24
|
-
* @param clientSecret The client secret for your OAuth application.
|
|
25
|
-
* @param authorizationUrl The URL for the authorization endpoint of the provider.
|
|
26
|
-
* @param scopes The scopes required for the OAuth authorization.
|
|
27
|
-
* @param providerTokenUrl The URL for the token endpoint of the provider.
|
|
28
|
-
*/
|
|
29
|
-
constructor(authorizationInfo: Oauth2Payload, environment?: Environment);
|
|
30
|
-
/**
|
|
31
|
-
* Handles the authorization request and redirects the user to the provider's authorization page.
|
|
32
|
-
* @param req The express Request object.
|
|
33
|
-
* @param res The express Response object.
|
|
34
|
-
*/
|
|
35
|
-
private handleAuthorize;
|
|
36
|
-
/**
|
|
37
|
-
* Handles the callback request from the provider and stores the authorization code.
|
|
38
|
-
* @param req The express Request object.
|
|
39
|
-
* @param res The express Response object.
|
|
40
|
-
*/
|
|
41
|
-
private handleCallback;
|
|
42
|
-
private encodeBody;
|
|
43
|
-
/**
|
|
44
|
-
* Handles the token request to exchange the authorization code for an access token.
|
|
45
|
-
* @param req The express Request object.
|
|
46
|
-
* @param res The express Response object.
|
|
47
|
-
*/
|
|
48
|
-
private handleToken;
|
|
49
|
-
updateToken(): Promise<Oauth2Response>;
|
|
50
|
-
/**
|
|
51
|
-
* Starts the Express server for handling OAuth callbacks.
|
|
52
|
-
* @returns The URL of the server.
|
|
53
|
-
*/
|
|
54
|
-
startServer(): Promise<string>;
|
|
55
|
-
/**
|
|
56
|
-
* Stops the Express server.
|
|
57
|
-
*/
|
|
58
|
-
stopServer(): void;
|
|
59
|
-
/**
|
|
60
|
-
* Waits for the authorization code to be set.
|
|
61
|
-
* @returns A promise that resolves when the code is set.
|
|
62
|
-
*/
|
|
63
|
-
callbackIsDone(): Promise<boolean>;
|
|
64
|
-
}
|
|
65
|
-
export default OAuth2Helper;
|
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.open = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const express_1 = tslib_1.__importDefault(require("express"));
|
|
6
|
-
const cors_1 = tslib_1.__importDefault(require("cors"));
|
|
7
|
-
const openUrl = tslib_1.__importStar(require("openurl"));
|
|
8
|
-
const ngrok_1 = tslib_1.__importDefault(require("ngrok"));
|
|
9
|
-
const IntegrationsPlatformClient = tslib_1.__importStar(require("../services/integrationsPlatform"));
|
|
10
|
-
const configurationTypes_1 = require("../configurationTypes");
|
|
11
|
-
const errors_1 = require("../errors");
|
|
12
|
-
const globalConfiguration_1 = require("../resources/globalConfiguration");
|
|
13
|
-
// It allows to stub openUrl library in the test
|
|
14
|
-
exports.open = openUrl;
|
|
15
|
-
class OAuth2Helper {
|
|
16
|
-
environment;
|
|
17
|
-
server = null;
|
|
18
|
-
clientId;
|
|
19
|
-
clientSecret;
|
|
20
|
-
providerAuthorizationUrl;
|
|
21
|
-
tokenUrl;
|
|
22
|
-
scopes;
|
|
23
|
-
grantType;
|
|
24
|
-
requestContentType;
|
|
25
|
-
responseContentType;
|
|
26
|
-
refreshToken;
|
|
27
|
-
code = '';
|
|
28
|
-
serverUrl = '';
|
|
29
|
-
tokenRequestParameters;
|
|
30
|
-
refreshRequestParameters;
|
|
31
|
-
/**
|
|
32
|
-
* Constructs an instance of OAuthHelper.
|
|
33
|
-
* @param clientId The client ID for your OAuth application.
|
|
34
|
-
* @param clientSecret The client secret for your OAuth application.
|
|
35
|
-
* @param authorizationUrl The URL for the authorization endpoint of the provider.
|
|
36
|
-
* @param scopes The scopes required for the OAuth authorization.
|
|
37
|
-
* @param providerTokenUrl The URL for the token endpoint of the provider.
|
|
38
|
-
*/
|
|
39
|
-
constructor(authorizationInfo, environment = globalConfiguration_1.Environment.Production) {
|
|
40
|
-
const { clientId, clientSecret, authorizationUrl, scopes, tokenUrl, grantType, requestContentType, responseContentType, refreshToken, refreshRequestParameters, tokenRequestParameters, } = authorizationInfo;
|
|
41
|
-
this.startServer = this.startServer.bind(this);
|
|
42
|
-
this.stopServer = this.stopServer.bind(this);
|
|
43
|
-
this.handleAuthorize = this.handleAuthorize.bind(this);
|
|
44
|
-
this.handleCallback = this.handleCallback.bind(this);
|
|
45
|
-
this.handleToken = this.handleToken.bind(this);
|
|
46
|
-
this.clientId = clientId;
|
|
47
|
-
this.clientSecret = clientSecret;
|
|
48
|
-
this.providerAuthorizationUrl = authorizationUrl;
|
|
49
|
-
this.scopes = scopes.map(scope => scope.name);
|
|
50
|
-
this.tokenUrl = tokenUrl;
|
|
51
|
-
this.grantType = grantType ?? configurationTypes_1.GrantType.AUTHORIZATION_CODE;
|
|
52
|
-
this.requestContentType = requestContentType ?? configurationTypes_1.RequestContentType.URL_ENCODED;
|
|
53
|
-
this.responseContentType = responseContentType ?? configurationTypes_1.RequestContentType.JSON;
|
|
54
|
-
this.refreshToken = refreshToken;
|
|
55
|
-
this.tokenRequestParameters = tokenRequestParameters;
|
|
56
|
-
this.refreshRequestParameters = refreshRequestParameters;
|
|
57
|
-
this.environment = environment;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Handles the authorization request and redirects the user to the provider's authorization page.
|
|
61
|
-
* @param req The express Request object.
|
|
62
|
-
* @param res The express Response object.
|
|
63
|
-
*/
|
|
64
|
-
async handleAuthorize(_req, res) {
|
|
65
|
-
if (!this.providerAuthorizationUrl) {
|
|
66
|
-
res.status(422).send({ message: 'providerAuthorizationUrl must be defined in .unito.json' });
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const authUrl = new URL(this.providerAuthorizationUrl);
|
|
70
|
-
const params = new URLSearchParams(authUrl.search);
|
|
71
|
-
if (this.clientId) {
|
|
72
|
-
params.set('client_id', this.clientId);
|
|
73
|
-
}
|
|
74
|
-
params.set('redirect_uri', `${IntegrationsPlatformClient.Servers[this.environment]}/credentials/new/oauth2/callback-cli`);
|
|
75
|
-
const state = Buffer.from(JSON.stringify({
|
|
76
|
-
cliCallbackUrl: `${this.serverUrl}/credentials/new/oauth2/callback`,
|
|
77
|
-
})).toString('base64');
|
|
78
|
-
params.set('state', state);
|
|
79
|
-
if (this.scopes) {
|
|
80
|
-
params.set('scope', this.scopes.join(' '));
|
|
81
|
-
}
|
|
82
|
-
params.set('response_type', 'code');
|
|
83
|
-
authUrl.search = params.toString();
|
|
84
|
-
exports.open.open(authUrl.toString());
|
|
85
|
-
res.sendStatus(200);
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Handles the callback request from the provider and stores the authorization code.
|
|
89
|
-
* @param req The express Request object.
|
|
90
|
-
* @param res The express Response object.
|
|
91
|
-
*/
|
|
92
|
-
async handleCallback(req, res) {
|
|
93
|
-
try {
|
|
94
|
-
const { code } = req.body.query;
|
|
95
|
-
this.code = code;
|
|
96
|
-
}
|
|
97
|
-
catch (error) {
|
|
98
|
-
// If we do not receive a code, set it 'error' to let the command know that the callback failed
|
|
99
|
-
console.error('Error during OAuth2 callback', error);
|
|
100
|
-
this.code = 'error';
|
|
101
|
-
}
|
|
102
|
-
res.sendStatus(200);
|
|
103
|
-
}
|
|
104
|
-
encodeBody(bodyData, contentType) {
|
|
105
|
-
switch (contentType) {
|
|
106
|
-
case configurationTypes_1.RequestContentType.URL_ENCODED:
|
|
107
|
-
return new URLSearchParams(bodyData).toString();
|
|
108
|
-
case configurationTypes_1.RequestContentType.JSON:
|
|
109
|
-
return JSON.stringify(bodyData);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Handles the token request to exchange the authorization code for an access token.
|
|
114
|
-
* @param req The express Request object.
|
|
115
|
-
* @param res The express Response object.
|
|
116
|
-
*/
|
|
117
|
-
async handleToken(_req, res) {
|
|
118
|
-
if (!Object.values(configurationTypes_1.RequestContentType).includes(this.requestContentType)) {
|
|
119
|
-
res.status(400).json({ error: `Request content type not supported: ${this.requestContentType}` });
|
|
120
|
-
}
|
|
121
|
-
if (this.code === 'error') {
|
|
122
|
-
throw new errors_1.FailedToRetrieveAccessTokenError('Error during OAuth2 callback - aborting');
|
|
123
|
-
}
|
|
124
|
-
const bodyData = {
|
|
125
|
-
code: this.code,
|
|
126
|
-
grant_type: this.grantType,
|
|
127
|
-
redirect_uri: `${IntegrationsPlatformClient.Servers[this.environment]}/credentials/new/oauth2/callback-cli`,
|
|
128
|
-
};
|
|
129
|
-
if (this.clientId) {
|
|
130
|
-
bodyData.client_id = this.clientId;
|
|
131
|
-
}
|
|
132
|
-
if (this.clientSecret) {
|
|
133
|
-
bodyData.client_secret = this.clientSecret;
|
|
134
|
-
}
|
|
135
|
-
const body = this.encodeBody(bodyData, this.requestContentType);
|
|
136
|
-
const fetchOptions = {
|
|
137
|
-
headers: {
|
|
138
|
-
'Content-type': this.requestContentType,
|
|
139
|
-
...(this.tokenRequestParameters?.header ?? {}),
|
|
140
|
-
},
|
|
141
|
-
body,
|
|
142
|
-
method: 'POST',
|
|
143
|
-
};
|
|
144
|
-
try {
|
|
145
|
-
const fetchResult = await fetch(this.tokenUrl, fetchOptions);
|
|
146
|
-
if (fetchResult.status !== 200) {
|
|
147
|
-
res.status(500).json({ error: `Error fetching token: ${await fetchResult.text()}` });
|
|
148
|
-
}
|
|
149
|
-
const response = await fetchResult.json();
|
|
150
|
-
const credentialsInfo = {
|
|
151
|
-
accessToken: response.access_token,
|
|
152
|
-
refreshToken: response.refresh_token,
|
|
153
|
-
};
|
|
154
|
-
res.json(credentialsInfo);
|
|
155
|
-
}
|
|
156
|
-
catch (error) {
|
|
157
|
-
res.status(500).json({ error: 'Failed to retrieve access token' });
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
async updateToken() {
|
|
161
|
-
if (!this.refreshToken) {
|
|
162
|
-
throw new errors_1.NoRefreshTokenError();
|
|
163
|
-
}
|
|
164
|
-
if (!Object.values(configurationTypes_1.RequestContentType).includes(this.requestContentType)) {
|
|
165
|
-
throw new errors_1.InvalidRequestContentTypeError(`Request content type not supported: ${this.requestContentType}`);
|
|
166
|
-
}
|
|
167
|
-
const bodyData = {
|
|
168
|
-
grant_type: 'refresh_token',
|
|
169
|
-
refresh_token: this.refreshToken,
|
|
170
|
-
};
|
|
171
|
-
if (this.clientId) {
|
|
172
|
-
bodyData.client_id = this.clientId;
|
|
173
|
-
}
|
|
174
|
-
if (this.clientSecret) {
|
|
175
|
-
bodyData.client_secret = this.clientSecret;
|
|
176
|
-
}
|
|
177
|
-
const fetchOptions = {
|
|
178
|
-
headers: {
|
|
179
|
-
'Content-type': this.requestContentType,
|
|
180
|
-
...(this.refreshRequestParameters?.header ?? {}),
|
|
181
|
-
},
|
|
182
|
-
body: this.encodeBody(bodyData, this.requestContentType),
|
|
183
|
-
method: 'POST',
|
|
184
|
-
};
|
|
185
|
-
try {
|
|
186
|
-
const fetchResult = await fetch(this.tokenUrl, fetchOptions);
|
|
187
|
-
if (fetchResult.status !== 200) {
|
|
188
|
-
throw new errors_1.FailedToRetrieveAccessTokenError(await fetchResult.text());
|
|
189
|
-
}
|
|
190
|
-
const response = await fetchResult.json();
|
|
191
|
-
const credentialsInfo = {
|
|
192
|
-
accessToken: response.access_token,
|
|
193
|
-
refreshToken: response.refresh_token,
|
|
194
|
-
};
|
|
195
|
-
return credentialsInfo;
|
|
196
|
-
}
|
|
197
|
-
catch (error) {
|
|
198
|
-
throw new errors_1.FailedToRetrieveAccessTokenError(JSON.stringify(error));
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Starts the Express server for handling OAuth callbacks.
|
|
203
|
-
* @returns The URL of the server.
|
|
204
|
-
*/
|
|
205
|
-
/* istanbul ignore next */
|
|
206
|
-
async startServer() {
|
|
207
|
-
const app = (0, express_1.default)();
|
|
208
|
-
const PORT = process.env.OAUTH2_PORT ?? 9002;
|
|
209
|
-
this.serverUrl = await ngrok_1.default.connect(Number(PORT));
|
|
210
|
-
app.use((0, cors_1.default)({ credentials: true, origin: true }));
|
|
211
|
-
app.use(express_1.default.json());
|
|
212
|
-
app.get('/health', (_req, res) => {
|
|
213
|
-
res.send('pong');
|
|
214
|
-
});
|
|
215
|
-
app.get('/credentials/new/oauth2/authorize', this.handleAuthorize);
|
|
216
|
-
app.post('/credentials/new/oauth2/callback', this.handleCallback);
|
|
217
|
-
app.get('/credentials/new/oauth2/token', this.handleToken);
|
|
218
|
-
this.server = app.listen(PORT, () => {
|
|
219
|
-
console.log(`Listening at port ${PORT}`);
|
|
220
|
-
});
|
|
221
|
-
return this.serverUrl;
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Stops the Express server.
|
|
225
|
-
*/
|
|
226
|
-
/* istanbul ignore next */
|
|
227
|
-
stopServer() {
|
|
228
|
-
if (this.server) {
|
|
229
|
-
this.server.close(() => {
|
|
230
|
-
console.log('Server has stopped');
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Waits for the authorization code to be set.
|
|
236
|
-
* @returns A promise that resolves when the code is set.
|
|
237
|
-
*/
|
|
238
|
-
/* istanbul ignore next */
|
|
239
|
-
async callbackIsDone() {
|
|
240
|
-
if (this.code) {
|
|
241
|
-
return true;
|
|
242
|
-
}
|
|
243
|
-
else {
|
|
244
|
-
return new Promise(resolve => {
|
|
245
|
-
const interval = setInterval(() => {
|
|
246
|
-
if (this.code) {
|
|
247
|
-
clearInterval(interval);
|
|
248
|
-
resolve(true);
|
|
249
|
-
}
|
|
250
|
-
}, 100);
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
exports.default = OAuth2Helper;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { Oauth2 } from '../configurationTypes';
|
|
2
|
-
export interface Oauth2Credentials {
|
|
3
|
-
clientId: string;
|
|
4
|
-
clientSecret: string;
|
|
5
|
-
}
|
|
6
|
-
export interface AuthorizePayload extends Oauth2Credentials {
|
|
7
|
-
providerAuthorizationUrl: string;
|
|
8
|
-
scopes?: string;
|
|
9
|
-
providerAccessTokenUrl: string;
|
|
10
|
-
}
|
|
11
|
-
export interface CallbackPayload {
|
|
12
|
-
code: string;
|
|
13
|
-
}
|
|
14
|
-
export interface Oauth2Response {
|
|
15
|
-
accessToken: string;
|
|
16
|
-
refreshToken?: string;
|
|
17
|
-
}
|
|
18
|
-
export interface TokenPayload extends Oauth2Credentials {
|
|
19
|
-
providerTokenUrl: string;
|
|
20
|
-
code: string;
|
|
21
|
-
}
|
|
22
|
-
export type Oauth2Payload = Oauth2 & Omit<Oauth2Response, 'accessToken'>;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|