UNPKG

4.01 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3// tslint:disable-next-line:function-name
4function RequireIntegrationFolder() {
5 return function (_target, _propertyKey, descriptor) {
6 const originalMethod = descriptor.value;
7 descriptor.value = async function () {
8 if (this.bearerConfig.isIntegrationLocation) {
9 await originalMethod.apply(this, arguments);
10 }
11 else {
12 this.error('This command must be run within a integration folder.');
13 }
14 };
15 return descriptor;
16 };
17}
18exports.RequireIntegrationFolder = RequireIntegrationFolder;
19// tslint:disable-next-line:function-name
20function RequireLinkedIntegration() {
21 return function (_target, _propertyKey, descriptor) {
22 const originalMethod = descriptor.value;
23 descriptor.value = async function () {
24 if (this.bearerConfig.hasIntegrationLinked) {
25 await originalMethod.apply(this, arguments);
26 }
27 else {
28 const error = this.colors.bold('You integration must be linked before running this command\n') +
29 this.colors.yellow(this.colors.italic('Please run: bearer link'));
30 this.error(error);
31 }
32 };
33 return descriptor;
34 };
35}
36exports.RequireLinkedIntegration = RequireLinkedIntegration;
37function ensureFreshToken() {
38 return function (_target, _propertyKey, descriptor) {
39 const originalMethod = descriptor.value;
40 descriptor.value = async function () {
41 const { authorization, ExpiresAt } = this.bearerConfig.bearerConfig;
42 if (authorization && authorization.AuthenticationResult) {
43 try {
44 if (ExpiresAt < Date.now()) {
45 this.ux.action.start('Refreshing token');
46 await refreshMyToken(this);
47 this.ux.action.stop();
48 }
49 }
50 catch (error) {
51 this.ux.action.stop(`Failed`);
52 this.error(error.message);
53 }
54 // TS is complaining with TS2445, commenting out this for now
55 // this.debug('Running original method')
56 await originalMethod.apply(this, arguments);
57 return descriptor;
58 }
59 const error = this.colors.bold('⚠️ It looks like you are not logged in\n') +
60 this.colors.yellow(this.colors.italic('Please run: bearer login'));
61 this.error(error);
62 return descriptor;
63 };
64 return descriptor;
65 };
66}
67exports.ensureFreshToken = ensureFreshToken;
68async function refreshMyToken(command) {
69 const { RefreshToken } = command.bearerConfig.bearerConfig.authorization.AuthenticationResult;
70 if (!RefreshToken) {
71 throw new UnauthorizedRefreshTokenError();
72 }
73 // try {
74 const { statusCode, body } = await command.serviceClient.refresh({ RefreshToken });
75 switch (statusCode) {
76 case 200: {
77 const { authorization: { AuthenticationResult } } = body;
78 await command.bearerConfig.storeBearerConfig({
79 ExpiresAt: Date.now() + AuthenticationResult.ExpiresIn * 1000,
80 authorization: {
81 AuthenticationResult: Object.assign({}, AuthenticationResult, { RefreshToken })
82 }
83 });
84 return true;
85 }
86 case 401: {
87 throw new UnauthorizedRefreshTokenError();
88 }
89 default: {
90 throw new UnexpectedRefreshTokenError(JSON.stringify(body, undefined, 2));
91 }
92 }
93}
94class UnauthorizedRefreshTokenError extends Error {
95 constructor() {
96 super('Something went wrong, please run bearer login and try again');
97 }
98}
99class UnexpectedRefreshTokenError extends Error {
100 constructor(body) {
101 super(`body : ${body}`);
102 }
103}