@leapdev/auth-agent 2.0.0-alpha.6 → 2.0.0-beta.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/README.md +328 -312
- package/package.json +1 -2
- package/src/index.umd.js +6885 -0
- package/src/lib/auth-agent.d.ts +1 -0
- package/src/lib/auth-agent.js +7 -0
- package/src/lib/auth-agent.js.map +1 -1
- package/src/lib/authentication.d.ts +5 -0
- package/src/lib/authentication.js +20 -7
- package/src/lib/authentication.js.map +1 -1
- package/src/lib/utils.d.ts +3 -2
- package/src/lib/utils.js +53 -9
- package/src/lib/utils.js.map +1 -1
- package/src/AuthAgent.umd.js +0 -3
- package/src/AuthAgent.umd.js.map +0 -1
- package/src/lib/refresh-token.worker.d.ts +0 -18
- package/src/lib/refresh-token.worker.js +0 -116
- package/src/lib/refresh-token.worker.js.map +0 -1
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
declare let refreshTokenInstance: RefreshToken | undefined;
|
|
3
|
-
declare let timer: NodeJS.Timeout | undefined;
|
|
4
|
-
declare const SECONDS_BEFORE_EXPIRE = 10;
|
|
5
|
-
declare const startRefreshAccessTokenProcess: (expireIn: number) => void;
|
|
6
|
-
declare const destroyRefreshAccessTokenProcess: () => void;
|
|
7
|
-
declare class RefreshToken {
|
|
8
|
-
#private;
|
|
9
|
-
constructor(params: {
|
|
10
|
-
verifier: string;
|
|
11
|
-
clientId: string;
|
|
12
|
-
authHost: string;
|
|
13
|
-
refreshToken: string;
|
|
14
|
-
expireIn: number;
|
|
15
|
-
});
|
|
16
|
-
getExpireId: () => number;
|
|
17
|
-
renewAccessToken: () => Promise<any>;
|
|
18
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
15
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
16
|
-
};
|
|
17
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
-
};
|
|
22
|
-
var _RefreshToken_verifier, _RefreshToken_clientId, _RefreshToken_authHost, _RefreshToken_refreshToken, _RefreshToken_expireIn;
|
|
23
|
-
let refreshTokenInstance;
|
|
24
|
-
let timer;
|
|
25
|
-
const SECONDS_BEFORE_EXPIRE = 10;
|
|
26
|
-
self.onmessage = (e) => {
|
|
27
|
-
const workerData = e.data;
|
|
28
|
-
postMessage('[WORKER] Web worker onmessage established');
|
|
29
|
-
switch (workerData.connectionStatus) {
|
|
30
|
-
case 'init':
|
|
31
|
-
refreshTokenInstance = new RefreshToken({
|
|
32
|
-
authHost: workerData.authHost,
|
|
33
|
-
clientId: workerData.clientId,
|
|
34
|
-
verifier: workerData.verifier,
|
|
35
|
-
refreshToken: workerData.refreshToken,
|
|
36
|
-
expireIn: workerData.expireId,
|
|
37
|
-
});
|
|
38
|
-
startRefreshAccessTokenProcess(refreshTokenInstance.getExpireId());
|
|
39
|
-
break;
|
|
40
|
-
case 'destroy':
|
|
41
|
-
destroyRefreshAccessTokenProcess();
|
|
42
|
-
postMessage({ status: 'destroy_process' });
|
|
43
|
-
break;
|
|
44
|
-
default:
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
const startRefreshAccessTokenProcess = (expireIn) => {
|
|
49
|
-
if (timer) {
|
|
50
|
-
clearTimeout(timer);
|
|
51
|
-
}
|
|
52
|
-
if (!refreshTokenInstance) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const waitBeforeExecuting = (expireIn - SECONDS_BEFORE_EXPIRE) * 1000;
|
|
56
|
-
timer = setTimeout(() => {
|
|
57
|
-
if (!refreshTokenInstance) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
refreshTokenInstance.renewAccessToken().then((res) => {
|
|
61
|
-
const { access_token, expires_in } = res;
|
|
62
|
-
postMessage({ status: 'update_access_token', accessToken: access_token });
|
|
63
|
-
startRefreshAccessTokenProcess(expires_in);
|
|
64
|
-
});
|
|
65
|
-
}, waitBeforeExecuting);
|
|
66
|
-
};
|
|
67
|
-
const destroyRefreshAccessTokenProcess = () => {
|
|
68
|
-
if (timer) {
|
|
69
|
-
clearTimeout(timer);
|
|
70
|
-
}
|
|
71
|
-
refreshTokenInstance = undefined;
|
|
72
|
-
};
|
|
73
|
-
class RefreshToken {
|
|
74
|
-
constructor(params) {
|
|
75
|
-
_RefreshToken_verifier.set(this, void 0);
|
|
76
|
-
_RefreshToken_clientId.set(this, void 0);
|
|
77
|
-
_RefreshToken_authHost.set(this, void 0);
|
|
78
|
-
_RefreshToken_refreshToken.set(this, void 0);
|
|
79
|
-
_RefreshToken_expireIn.set(this, void 0);
|
|
80
|
-
this.getExpireId = () => {
|
|
81
|
-
return __classPrivateFieldGet(this, _RefreshToken_expireIn, "f");
|
|
82
|
-
};
|
|
83
|
-
this.renewAccessToken = () => __awaiter(this, void 0, void 0, function* () {
|
|
84
|
-
const url = `${__classPrivateFieldGet(this, _RefreshToken_authHost, "f")}/oauth/token`;
|
|
85
|
-
const body = `grant_type=refresh_token&refresh_token=${__classPrivateFieldGet(this, _RefreshToken_refreshToken, "f")}&client_id=${__classPrivateFieldGet(this, _RefreshToken_clientId, "f")}&code_verifier=${__classPrivateFieldGet(this, _RefreshToken_verifier, "f")}`;
|
|
86
|
-
const response = yield fetch(url, {
|
|
87
|
-
method: 'POST',
|
|
88
|
-
headers: {
|
|
89
|
-
Accept: 'application/json',
|
|
90
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
91
|
-
},
|
|
92
|
-
body: body,
|
|
93
|
-
});
|
|
94
|
-
const { ok } = response;
|
|
95
|
-
if (!ok) {
|
|
96
|
-
throw new Error('Unable to get access token');
|
|
97
|
-
}
|
|
98
|
-
const resBody = yield response.json();
|
|
99
|
-
if (!!resBody && !!resBody.Error) {
|
|
100
|
-
throw new Error(resBody.Error);
|
|
101
|
-
}
|
|
102
|
-
const { refresh_token, expires_in } = resBody;
|
|
103
|
-
__classPrivateFieldSet(this, _RefreshToken_refreshToken, refresh_token, "f");
|
|
104
|
-
__classPrivateFieldSet(this, _RefreshToken_expireIn, expires_in, "f");
|
|
105
|
-
return resBody;
|
|
106
|
-
});
|
|
107
|
-
const { verifier, clientId, expireIn, authHost, refreshToken } = params;
|
|
108
|
-
__classPrivateFieldSet(this, _RefreshToken_verifier, verifier, "f");
|
|
109
|
-
__classPrivateFieldSet(this, _RefreshToken_clientId, clientId, "f");
|
|
110
|
-
__classPrivateFieldSet(this, _RefreshToken_authHost, authHost, "f");
|
|
111
|
-
__classPrivateFieldSet(this, _RefreshToken_refreshToken, refreshToken, "f");
|
|
112
|
-
__classPrivateFieldSet(this, _RefreshToken_expireIn, expireIn, "f");
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
_RefreshToken_verifier = new WeakMap(), _RefreshToken_clientId = new WeakMap(), _RefreshToken_authHost = new WeakMap(), _RefreshToken_refreshToken = new WeakMap(), _RefreshToken_expireIn = new WeakMap();
|
|
116
|
-
//# sourceMappingURL=refresh-token.worker.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"refresh-token.worker.js","sourceRoot":"","sources":["../../../../../packages/auth-agent/src/lib/refresh-token.worker.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,IAAI,oBAA8C,CAAC;AACnD,IAAI,KAAiC,CAAC;AACtC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAEjC,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC,EAAQ,EAAE;IAC3B,MAAM,UAAU,GAAG,CAAC,CAAC,IAAI,CAAC;IAC1B,WAAW,CAAC,2CAA2C,CAAC,CAAC;IACzD,QAAQ,UAAU,CAAC,gBAAgB,EAAE;QACnC,KAAK,MAAM;YACT,oBAAoB,GAAG,IAAI,YAAY,CAAC;gBACtC,QAAQ,EAAE,UAAU,CAAC,QAAQ;gBAC7B,QAAQ,EAAE,UAAU,CAAC,QAAQ;gBAC7B,QAAQ,EAAE,UAAU,CAAC,QAAQ;gBAC7B,YAAY,EAAE,UAAU,CAAC,YAAY;gBACrC,QAAQ,EAAE,UAAU,CAAC,QAAQ;aAC9B,CAAC,CAAC;YACH,8BAA8B,CAAC,oBAAoB,CAAC,WAAW,EAAE,CAAC,CAAC;YACnE,MAAM;QAER,KAAK,SAAS;YACZ,gCAAgC,EAAE,CAAC;YACnC,WAAW,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC,CAAC;YAC3C,MAAM;QAER;YACE,OAAO;KACV;AACH,C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