@usync/oauth2 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ # @usync/oauth2
2
+
3
+ [![NPM](https://img.shields.io/npm/v/@usync/oauth2.svg)](https://npm.im/@usync/oauth2)
4
+ ![License](https://img.shields.io/npm/l/@usync/oauth2.svg)
5
+ [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@usync/oauth2)
6
+
7
+ Handling OAuth2 login and generate access tokens.
@@ -0,0 +1,7 @@
1
+ export declare const OAUTH2_NEED_REFRESH = 1;
2
+ export declare const OAUTH2_AUTH_ERROR = 2;
3
+ export declare const OAUTH2_UNAUTHORIZED = 3;
4
+ export declare class OAuth2Error extends Error {
5
+ code: number;
6
+ constructor(code: number, message?: string);
7
+ }
@@ -0,0 +1,7 @@
1
+ import { OAuth2Authorizer } from './providers';
2
+ import { IOAuth2Account, IOAuth2Options } from './types';
3
+ export * from './common';
4
+ export * from './providers';
5
+ export * from './types';
6
+ export declare function getAuthorizer(options: IOAuth2Options, auth: IOAuth2Account): import("./providers").DropboxAuthorizer | import("./providers").GoogleAuthorizer | import("./providers").MicrosoftAuthorizer;
7
+ export declare function ensureAccessToken(authorizer: OAuth2Authorizer, handleOAuth2?: (url: string) => Promise<string>): Promise<string>;
package/dist/index.js ADDED
@@ -0,0 +1,963 @@
1
+ import { nanoid as ye } from "nanoid";
2
+ const ve = 1, Y = 2, ne = 3;
3
+ class D extends Error {
4
+ constructor(p, m) {
5
+ super(m || `OAuth2Error: code=${p}`), this.code = p;
6
+ }
7
+ }
8
+ var Re = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i, pe = Math.ceil, V = Math.floor, B = "[BigNumber Error] ", Ue = B + "Number primitive has more than 15 significant digits: ", $ = 1e14, E = 14, de = 9007199254740991, we = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13], J = 1e7, I = 1e9;
9
+ function Ie(_) {
10
+ var p, m, w, h = d.prototype = { constructor: d, toString: null, valueOf: null }, g = new d(1), A = 20, N = 4, C = -7, M = 21, Q = -1e7, j = 1e7, K = !1, te = 1, Z = 0, ce = {
11
+ prefix: "",
12
+ groupSize: 3,
13
+ secondaryGroupSize: 0,
14
+ groupSeparator: ",",
15
+ decimalSeparator: ".",
16
+ fractionGroupSize: 0,
17
+ fractionGroupSeparator: " ",
18
+ // non-breaking space
19
+ suffix: ""
20
+ }, b = "0123456789abcdefghijklmnopqrstuvwxyz", fe = !0;
21
+ function d(e, t) {
22
+ var i, f, r, o, l, n, s, a, c = this;
23
+ if (!(c instanceof d)) return new d(e, t);
24
+ if (t == null) {
25
+ if (e && e._isBigNumber === !0) {
26
+ c.s = e.s, !e.c || e.e > j ? c.c = c.e = null : e.e < Q ? c.c = [c.e = 0] : (c.e = e.e, c.c = e.c.slice());
27
+ return;
28
+ }
29
+ if ((n = typeof e == "number") && e * 0 == 0) {
30
+ if (c.s = 1 / e < 0 ? (e = -e, -1) : 1, e === ~~e) {
31
+ for (o = 0, l = e; l >= 10; l /= 10, o++) ;
32
+ o > j ? c.c = c.e = null : (c.e = o, c.c = [e]);
33
+ return;
34
+ }
35
+ a = String(e);
36
+ } else {
37
+ if (!Re.test(a = String(e))) return w(c, a, n);
38
+ c.s = a.charCodeAt(0) == 45 ? (a = a.slice(1), -1) : 1;
39
+ }
40
+ (o = a.indexOf(".")) > -1 && (a = a.replace(".", "")), (l = a.search(/e/i)) > 0 ? (o < 0 && (o = l), o += +a.slice(l + 1), a = a.substring(0, l)) : o < 0 && (o = a.length);
41
+ } else {
42
+ if (U(t, 2, b.length, "Base"), t == 10 && fe)
43
+ return c = new d(e), F(c, A + c.e + 1, N);
44
+ if (a = String(e), n = typeof e == "number") {
45
+ if (e * 0 != 0) return w(c, a, n, t);
46
+ if (c.s = 1 / e < 0 ? (a = a.slice(1), -1) : 1, d.DEBUG && a.replace(/^0\.0*|\./, "").length > 15)
47
+ throw Error(Ue + e);
48
+ } else
49
+ c.s = a.charCodeAt(0) === 45 ? (a = a.slice(1), -1) : 1;
50
+ for (i = b.slice(0, t), o = l = 0, s = a.length; l < s; l++)
51
+ if (i.indexOf(f = a.charAt(l)) < 0) {
52
+ if (f == ".") {
53
+ if (l > o) {
54
+ o = s;
55
+ continue;
56
+ }
57
+ } else if (!r && (a == a.toUpperCase() && (a = a.toLowerCase()) || a == a.toLowerCase() && (a = a.toUpperCase()))) {
58
+ r = !0, l = -1, o = 0;
59
+ continue;
60
+ }
61
+ return w(c, String(e), n, t);
62
+ }
63
+ n = !1, a = m(a, t, 10, c.s), (o = a.indexOf(".")) > -1 ? a = a.replace(".", "") : o = a.length;
64
+ }
65
+ for (l = 0; a.charCodeAt(l) === 48; l++) ;
66
+ for (s = a.length; a.charCodeAt(--s) === 48; ) ;
67
+ if (a = a.slice(l, ++s)) {
68
+ if (s -= l, n && d.DEBUG && s > 15 && (e > de || e !== V(e)))
69
+ throw Error(Ue + c.s * e);
70
+ if ((o = o - l - 1) > j)
71
+ c.c = c.e = null;
72
+ else if (o < Q)
73
+ c.c = [c.e = 0];
74
+ else {
75
+ if (c.e = o, c.c = [], l = (o + 1) % E, o < 0 && (l += E), l < s) {
76
+ for (l && c.c.push(+a.slice(0, l)), s -= E; l < s; )
77
+ c.c.push(+a.slice(l, l += E));
78
+ l = E - (a = a.slice(l)).length;
79
+ } else
80
+ l -= s;
81
+ for (; l--; a += "0") ;
82
+ c.c.push(+a);
83
+ }
84
+ } else
85
+ c.c = [c.e = 0];
86
+ }
87
+ d.clone = Ie, d.ROUND_UP = 0, d.ROUND_DOWN = 1, d.ROUND_CEIL = 2, d.ROUND_FLOOR = 3, d.ROUND_HALF_UP = 4, d.ROUND_HALF_DOWN = 5, d.ROUND_HALF_EVEN = 6, d.ROUND_HALF_CEIL = 7, d.ROUND_HALF_FLOOR = 8, d.EUCLID = 9, d.config = d.set = function(e) {
88
+ var t, i;
89
+ if (e != null)
90
+ if (typeof e == "object") {
91
+ if (e.hasOwnProperty(t = "DECIMAL_PLACES") && (i = e[t], U(i, 0, I, t), A = i), e.hasOwnProperty(t = "ROUNDING_MODE") && (i = e[t], U(i, 0, 8, t), N = i), e.hasOwnProperty(t = "EXPONENTIAL_AT") && (i = e[t], i && i.pop ? (U(i[0], -I, 0, t), U(i[1], 0, I, t), C = i[0], M = i[1]) : (U(i, -I, I, t), C = -(M = i < 0 ? -i : i))), e.hasOwnProperty(t = "RANGE"))
92
+ if (i = e[t], i && i.pop)
93
+ U(i[0], -I, -1, t), U(i[1], 1, I, t), Q = i[0], j = i[1];
94
+ else if (U(i, -I, I, t), i)
95
+ Q = -(j = i < 0 ? -i : i);
96
+ else
97
+ throw Error(B + t + " cannot be zero: " + i);
98
+ if (e.hasOwnProperty(t = "CRYPTO"))
99
+ if (i = e[t], i === !!i)
100
+ if (i)
101
+ if (typeof crypto < "u" && crypto && (crypto.getRandomValues || crypto.randomBytes))
102
+ K = i;
103
+ else
104
+ throw K = !i, Error(B + "crypto unavailable");
105
+ else
106
+ K = i;
107
+ else
108
+ throw Error(B + t + " not true or false: " + i);
109
+ if (e.hasOwnProperty(t = "MODULO_MODE") && (i = e[t], U(i, 0, 9, t), te = i), e.hasOwnProperty(t = "POW_PRECISION") && (i = e[t], U(i, 0, I, t), Z = i), e.hasOwnProperty(t = "FORMAT"))
110
+ if (i = e[t], typeof i == "object") ce = i;
111
+ else throw Error(B + t + " not an object: " + i);
112
+ if (e.hasOwnProperty(t = "ALPHABET"))
113
+ if (i = e[t], typeof i == "string" && !/^.?$|[+\-.\s]|(.).*\1/.test(i))
114
+ fe = i.slice(0, 10) == "0123456789", b = i;
115
+ else
116
+ throw Error(B + t + " invalid: " + i);
117
+ } else
118
+ throw Error(B + "Object expected: " + e);
119
+ return {
120
+ DECIMAL_PLACES: A,
121
+ ROUNDING_MODE: N,
122
+ EXPONENTIAL_AT: [C, M],
123
+ RANGE: [Q, j],
124
+ CRYPTO: K,
125
+ MODULO_MODE: te,
126
+ POW_PRECISION: Z,
127
+ FORMAT: ce,
128
+ ALPHABET: b
129
+ };
130
+ }, d.isBigNumber = function(e) {
131
+ if (!e || e._isBigNumber !== !0) return !1;
132
+ if (!d.DEBUG) return !0;
133
+ var t, i, f = e.c, r = e.e, o = e.s;
134
+ e: if ({}.toString.call(f) == "[object Array]") {
135
+ if ((o === 1 || o === -1) && r >= -I && r <= I && r === V(r)) {
136
+ if (f[0] === 0) {
137
+ if (r === 0 && f.length === 1) return !0;
138
+ break e;
139
+ }
140
+ if (t = (r + 1) % E, t < 1 && (t += E), String(f[0]).length == t) {
141
+ for (t = 0; t < f.length; t++)
142
+ if (i = f[t], i < 0 || i >= $ || i !== V(i)) break e;
143
+ if (i !== 0) return !0;
144
+ }
145
+ }
146
+ } else if (f === null && r === null && (o === null || o === 1 || o === -1))
147
+ return !0;
148
+ throw Error(B + "Invalid BigNumber: " + e);
149
+ }, d.maximum = d.max = function() {
150
+ return xe(arguments, -1);
151
+ }, d.minimum = d.min = function() {
152
+ return xe(arguments, 1);
153
+ }, d.random = (function() {
154
+ var e = 9007199254740992, t = Math.random() * e & 2097151 ? function() {
155
+ return V(Math.random() * e);
156
+ } : function() {
157
+ return (Math.random() * 1073741824 | 0) * 8388608 + (Math.random() * 8388608 | 0);
158
+ };
159
+ return function(i) {
160
+ var f, r, o, l, n, s = 0, a = [], c = new d(g);
161
+ if (i == null ? i = A : U(i, 0, I), l = pe(i / E), K)
162
+ if (crypto.getRandomValues) {
163
+ for (f = crypto.getRandomValues(new Uint32Array(l *= 2)); s < l; )
164
+ n = f[s] * 131072 + (f[s + 1] >>> 11), n >= 9e15 ? (r = crypto.getRandomValues(new Uint32Array(2)), f[s] = r[0], f[s + 1] = r[1]) : (a.push(n % 1e14), s += 2);
165
+ s = l / 2;
166
+ } else if (crypto.randomBytes) {
167
+ for (f = crypto.randomBytes(l *= 7); s < l; )
168
+ n = (f[s] & 31) * 281474976710656 + f[s + 1] * 1099511627776 + f[s + 2] * 4294967296 + f[s + 3] * 16777216 + (f[s + 4] << 16) + (f[s + 5] << 8) + f[s + 6], n >= 9e15 ? crypto.randomBytes(7).copy(f, s) : (a.push(n % 1e14), s += 7);
169
+ s = l / 7;
170
+ } else
171
+ throw K = !1, Error(B + "crypto unavailable");
172
+ if (!K)
173
+ for (; s < l; )
174
+ n = t(), n < 9e15 && (a[s++] = n % 1e14);
175
+ for (l = a[--s], i %= E, l && i && (n = we[E - i], a[s] = V(l / n) * n); a[s] === 0; a.pop(), s--) ;
176
+ if (s < 0)
177
+ a = [o = 0];
178
+ else {
179
+ for (o = -1; a[0] === 0; a.splice(0, 1), o -= E) ;
180
+ for (s = 1, n = a[0]; n >= 10; n /= 10, s++) ;
181
+ s < E && (o -= E - s);
182
+ }
183
+ return c.e = o, c.c = a, c;
184
+ };
185
+ })(), d.sum = function() {
186
+ for (var e = 1, t = arguments, i = new d(t[0]); e < t.length; ) i = i.plus(t[e++]);
187
+ return i;
188
+ }, m = /* @__PURE__ */ (function() {
189
+ var e = "0123456789";
190
+ function t(i, f, r, o) {
191
+ for (var l, n = [0], s, a = 0, c = i.length; a < c; ) {
192
+ for (s = n.length; s--; n[s] *= f) ;
193
+ for (n[0] += o.indexOf(i.charAt(a++)), l = 0; l < n.length; l++)
194
+ n[l] > r - 1 && (n[l + 1] == null && (n[l + 1] = 0), n[l + 1] += n[l] / r | 0, n[l] %= r);
195
+ }
196
+ return n.reverse();
197
+ }
198
+ return function(i, f, r, o, l) {
199
+ var n, s, a, c, u, k, O, S, P = i.indexOf("."), v = A, T = N;
200
+ for (P >= 0 && (c = Z, Z = 0, i = i.replace(".", ""), S = new d(f), k = S.pow(i.length - P), Z = c, S.c = t(
201
+ W(q(k.c), k.e, "0"),
202
+ 10,
203
+ r,
204
+ e
205
+ ), S.e = S.c.length), O = t(i, f, r, l ? (n = b, e) : (n = e, b)), a = c = O.length; O[--c] == 0; O.pop()) ;
206
+ if (!O[0]) return n.charAt(0);
207
+ if (P < 0 ? --a : (k.c = O, k.e = a, k.s = o, k = p(k, S, v, T, r), O = k.c, u = k.r, a = k.e), s = a + v + 1, P = O[s], c = r / 2, u = u || s < 0 || O[s + 1] != null, u = T < 4 ? (P != null || u) && (T == 0 || T == (k.s < 0 ? 3 : 2)) : P > c || P == c && (T == 4 || u || T == 6 && O[s - 1] & 1 || T == (k.s < 0 ? 8 : 7)), s < 1 || !O[0])
208
+ i = u ? W(n.charAt(1), -v, n.charAt(0)) : n.charAt(0);
209
+ else {
210
+ if (O.length = s, u)
211
+ for (--r; ++O[--s] > r; )
212
+ O[s] = 0, s || (++a, O = [1].concat(O));
213
+ for (c = O.length; !O[--c]; ) ;
214
+ for (P = 0, i = ""; P <= c; i += n.charAt(O[P++])) ;
215
+ i = W(i, a, n.charAt(0));
216
+ }
217
+ return i;
218
+ };
219
+ })(), p = /* @__PURE__ */ (function() {
220
+ function e(f, r, o) {
221
+ var l, n, s, a, c = 0, u = f.length, k = r % J, O = r / J | 0;
222
+ for (f = f.slice(); u--; )
223
+ s = f[u] % J, a = f[u] / J | 0, l = O * s + a * k, n = k * s + l % J * J + c, c = (n / o | 0) + (l / J | 0) + O * a, f[u] = n % o;
224
+ return c && (f = [c].concat(f)), f;
225
+ }
226
+ function t(f, r, o, l) {
227
+ var n, s;
228
+ if (o != l)
229
+ s = o > l ? 1 : -1;
230
+ else
231
+ for (n = s = 0; n < o; n++)
232
+ if (f[n] != r[n]) {
233
+ s = f[n] > r[n] ? 1 : -1;
234
+ break;
235
+ }
236
+ return s;
237
+ }
238
+ function i(f, r, o, l) {
239
+ for (var n = 0; o--; )
240
+ f[o] -= n, n = f[o] < r[o] ? 1 : 0, f[o] = n * l + f[o] - r[o];
241
+ for (; !f[0] && f.length > 1; f.splice(0, 1)) ;
242
+ }
243
+ return function(f, r, o, l, n) {
244
+ var s, a, c, u, k, O, S, P, v, T, x, R, re, he, ue, z, ie, G = f.s == r.s ? 1 : -1, L = f.c, y = r.c;
245
+ if (!L || !L[0] || !y || !y[0])
246
+ return new d(
247
+ // Return NaN if either NaN, or both Infinity or 0.
248
+ !f.s || !r.s || (L ? y && L[0] == y[0] : !y) ? NaN : (
249
+ // Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0.
250
+ L && L[0] == 0 || !y ? G * 0 : G / 0
251
+ )
252
+ );
253
+ for (P = new d(G), v = P.c = [], a = f.e - r.e, G = o + a + 1, n || (n = $, a = H(f.e / E) - H(r.e / E), G = G / E | 0), c = 0; y[c] == (L[c] || 0); c++) ;
254
+ if (y[c] > (L[c] || 0) && a--, G < 0)
255
+ v.push(1), u = !0;
256
+ else {
257
+ for (he = L.length, z = y.length, c = 0, G += 2, k = V(n / (y[0] + 1)), k > 1 && (y = e(y, k, n), L = e(L, k, n), z = y.length, he = L.length), re = z, T = L.slice(0, z), x = T.length; x < z; T[x++] = 0) ;
258
+ ie = y.slice(), ie = [0].concat(ie), ue = y[0], y[1] >= n / 2 && ue++;
259
+ do {
260
+ if (k = 0, s = t(y, T, z, x), s < 0) {
261
+ if (R = T[0], z != x && (R = R * n + (T[1] || 0)), k = V(R / ue), k > 1)
262
+ for (k >= n && (k = n - 1), O = e(y, k, n), S = O.length, x = T.length; t(O, T, S, x) == 1; )
263
+ k--, i(O, z < S ? ie : y, S, n), S = O.length, s = 1;
264
+ else
265
+ k == 0 && (s = k = 1), O = y.slice(), S = O.length;
266
+ if (S < x && (O = [0].concat(O)), i(T, O, x, n), x = T.length, s == -1)
267
+ for (; t(y, T, z, x) < 1; )
268
+ k++, i(T, z < x ? ie : y, x, n), x = T.length;
269
+ } else s === 0 && (k++, T = [0]);
270
+ v[c++] = k, T[0] ? T[x++] = L[re] || 0 : (T = [L[re]], x = 1);
271
+ } while ((re++ < he || T[0] != null) && G--);
272
+ u = T[0] != null, v[0] || v.splice(0, 1);
273
+ }
274
+ if (n == $) {
275
+ for (c = 1, G = v[0]; G >= 10; G /= 10, c++) ;
276
+ F(P, o + (P.e = c + a * E - 1) + 1, l, u);
277
+ } else
278
+ P.e = a, P.r = +u;
279
+ return P;
280
+ };
281
+ })();
282
+ function ae(e, t, i, f) {
283
+ var r, o, l, n, s;
284
+ if (i == null ? i = N : U(i, 0, 8), !e.c) return e.toString();
285
+ if (r = e.c[0], l = e.e, t == null)
286
+ s = q(e.c), s = f == 1 || f == 2 && (l <= C || l >= M) ? oe(s, l) : W(s, l, "0");
287
+ else if (e = F(new d(e), t, i), o = e.e, s = q(e.c), n = s.length, f == 1 || f == 2 && (t <= o || o <= C)) {
288
+ for (; n < t; s += "0", n++) ;
289
+ s = oe(s, o);
290
+ } else if (t -= l + (f === 2 && o > l), s = W(s, o, "0"), o + 1 > n) {
291
+ if (--t > 0) for (s += "."; t--; s += "0") ;
292
+ } else if (t += o - n, t > 0)
293
+ for (o + 1 == n && (s += "."); t--; s += "0") ;
294
+ return e.s < 0 && r ? "-" + s : s;
295
+ }
296
+ function xe(e, t) {
297
+ for (var i, f, r = 1, o = new d(e[0]); r < e.length; r++)
298
+ f = new d(e[r]), (!f.s || (i = ee(o, f)) === t || i === 0 && o.s === t) && (o = f);
299
+ return o;
300
+ }
301
+ function le(e, t, i) {
302
+ for (var f = 1, r = t.length; !t[--r]; t.pop()) ;
303
+ for (r = t[0]; r >= 10; r /= 10, f++) ;
304
+ return (i = f + i * E - 1) > j ? e.c = e.e = null : i < Q ? e.c = [e.e = 0] : (e.e = i, e.c = t), e;
305
+ }
306
+ w = /* @__PURE__ */ (function() {
307
+ var e = /^(-?)0([xbo])(?=\w[\w.]*$)/i, t = /^([^.]+)\.$/, i = /^\.([^.]+)$/, f = /^-?(Infinity|NaN)$/, r = /^\s*\+(?=[\w.])|^\s+|\s+$/g;
308
+ return function(o, l, n, s) {
309
+ var a, c = n ? l : l.replace(r, "");
310
+ if (f.test(c))
311
+ o.s = isNaN(c) ? null : c < 0 ? -1 : 1;
312
+ else {
313
+ if (!n && (c = c.replace(e, function(u, k, O) {
314
+ return a = (O = O.toLowerCase()) == "x" ? 16 : O == "b" ? 2 : 8, !s || s == a ? k : u;
315
+ }), s && (a = s, c = c.replace(t, "$1").replace(i, "0.$1")), l != c))
316
+ return new d(c, a);
317
+ if (d.DEBUG)
318
+ throw Error(B + "Not a" + (s ? " base " + s : "") + " number: " + l);
319
+ o.s = null;
320
+ }
321
+ o.c = o.e = null;
322
+ };
323
+ })();
324
+ function F(e, t, i, f) {
325
+ var r, o, l, n, s, a, c, u = e.c, k = we;
326
+ if (u) {
327
+ e: {
328
+ for (r = 1, n = u[0]; n >= 10; n /= 10, r++) ;
329
+ if (o = t - r, o < 0)
330
+ o += E, l = t, s = u[a = 0], c = V(s / k[r - l - 1] % 10);
331
+ else if (a = pe((o + 1) / E), a >= u.length)
332
+ if (f) {
333
+ for (; u.length <= a; u.push(0)) ;
334
+ s = c = 0, r = 1, o %= E, l = o - E + 1;
335
+ } else
336
+ break e;
337
+ else {
338
+ for (s = n = u[a], r = 1; n >= 10; n /= 10, r++) ;
339
+ o %= E, l = o - E + r, c = l < 0 ? 0 : V(s / k[r - l - 1] % 10);
340
+ }
341
+ if (f = f || t < 0 || // Are there any non-zero digits after the rounding digit?
342
+ // The expression n % pows10[d - j - 1] returns all digits of n to the right
343
+ // of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714.
344
+ u[a + 1] != null || (l < 0 ? s : s % k[r - l - 1]), f = i < 4 ? (c || f) && (i == 0 || i == (e.s < 0 ? 3 : 2)) : c > 5 || c == 5 && (i == 4 || f || i == 6 && // Check whether the digit to the left of the rounding digit is odd.
345
+ (o > 0 ? l > 0 ? s / k[r - l] : 0 : u[a - 1]) % 10 & 1 || i == (e.s < 0 ? 8 : 7)), t < 1 || !u[0])
346
+ return u.length = 0, f ? (t -= e.e + 1, u[0] = k[(E - t % E) % E], e.e = -t || 0) : u[0] = e.e = 0, e;
347
+ if (o == 0 ? (u.length = a, n = 1, a--) : (u.length = a + 1, n = k[E - o], u[a] = l > 0 ? V(s / k[r - l] % k[l]) * n : 0), f)
348
+ for (; ; )
349
+ if (a == 0) {
350
+ for (o = 1, l = u[0]; l >= 10; l /= 10, o++) ;
351
+ for (l = u[0] += n, n = 1; l >= 10; l /= 10, n++) ;
352
+ o != n && (e.e++, u[0] == $ && (u[0] = 1));
353
+ break;
354
+ } else {
355
+ if (u[a] += n, u[a] != $) break;
356
+ u[a--] = 0, n = 1;
357
+ }
358
+ for (o = u.length; u[--o] === 0; u.pop()) ;
359
+ }
360
+ e.e > j ? e.c = e.e = null : e.e < Q && (e.c = [e.e = 0]);
361
+ }
362
+ return e;
363
+ }
364
+ function X(e) {
365
+ var t, i = e.e;
366
+ return i === null ? e.toString() : (t = q(e.c), t = i <= C || i >= M ? oe(t, i) : W(t, i, "0"), e.s < 0 ? "-" + t : t);
367
+ }
368
+ return h.absoluteValue = h.abs = function() {
369
+ var e = new d(this);
370
+ return e.s < 0 && (e.s = 1), e;
371
+ }, h.comparedTo = function(e, t) {
372
+ return ee(this, new d(e, t));
373
+ }, h.decimalPlaces = h.dp = function(e, t) {
374
+ var i, f, r, o = this;
375
+ if (e != null)
376
+ return U(e, 0, I), t == null ? t = N : U(t, 0, 8), F(new d(o), e + o.e + 1, t);
377
+ if (!(i = o.c)) return null;
378
+ if (f = ((r = i.length - 1) - H(this.e / E)) * E, r = i[r]) for (; r % 10 == 0; r /= 10, f--) ;
379
+ return f < 0 && (f = 0), f;
380
+ }, h.dividedBy = h.div = function(e, t) {
381
+ return p(this, new d(e, t), A, N);
382
+ }, h.dividedToIntegerBy = h.idiv = function(e, t) {
383
+ return p(this, new d(e, t), 0, 1);
384
+ }, h.exponentiatedBy = h.pow = function(e, t) {
385
+ var i, f, r, o, l, n, s, a, c, u = this;
386
+ if (e = new d(e), e.c && !e.isInteger())
387
+ throw Error(B + "Exponent not an integer: " + X(e));
388
+ if (t != null && (t = new d(t)), n = e.e > 14, !u.c || !u.c[0] || u.c[0] == 1 && !u.e && u.c.length == 1 || !e.c || !e.c[0])
389
+ return c = new d(Math.pow(+X(u), n ? e.s * (2 - se(e)) : +X(e))), t ? c.mod(t) : c;
390
+ if (s = e.s < 0, t) {
391
+ if (t.c ? !t.c[0] : !t.s) return new d(NaN);
392
+ f = !s && u.isInteger() && t.isInteger(), f && (u = u.mod(t));
393
+ } else {
394
+ if (e.e > 9 && (u.e > 0 || u.e < -1 || (u.e == 0 ? u.c[0] > 1 || n && u.c[1] >= 24e7 : u.c[0] < 8e13 || n && u.c[0] <= 9999975e7)))
395
+ return o = u.s < 0 && se(e) ? -0 : 0, u.e > -1 && (o = 1 / o), new d(s ? 1 / o : o);
396
+ Z && (o = pe(Z / E + 2));
397
+ }
398
+ for (n ? (i = new d(0.5), s && (e.s = 1), a = se(e)) : (r = Math.abs(+X(e)), a = r % 2), c = new d(g); ; ) {
399
+ if (a) {
400
+ if (c = c.times(u), !c.c) break;
401
+ o ? c.c.length > o && (c.c.length = o) : f && (c = c.mod(t));
402
+ }
403
+ if (r) {
404
+ if (r = V(r / 2), r === 0) break;
405
+ a = r % 2;
406
+ } else if (e = e.times(i), F(e, e.e + 1, 1), e.e > 14)
407
+ a = se(e);
408
+ else {
409
+ if (r = +X(e), r === 0) break;
410
+ a = r % 2;
411
+ }
412
+ u = u.times(u), o ? u.c && u.c.length > o && (u.c.length = o) : f && (u = u.mod(t));
413
+ }
414
+ return f ? c : (s && (c = g.div(c)), t ? c.mod(t) : o ? F(c, Z, N, l) : c);
415
+ }, h.integerValue = function(e) {
416
+ var t = new d(this);
417
+ return e == null ? e = N : U(e, 0, 8), F(t, t.e + 1, e);
418
+ }, h.isEqualTo = h.eq = function(e, t) {
419
+ return ee(this, new d(e, t)) === 0;
420
+ }, h.isFinite = function() {
421
+ return !!this.c;
422
+ }, h.isGreaterThan = h.gt = function(e, t) {
423
+ return ee(this, new d(e, t)) > 0;
424
+ }, h.isGreaterThanOrEqualTo = h.gte = function(e, t) {
425
+ return (t = ee(this, new d(e, t))) === 1 || t === 0;
426
+ }, h.isInteger = function() {
427
+ return !!this.c && H(this.e / E) > this.c.length - 2;
428
+ }, h.isLessThan = h.lt = function(e, t) {
429
+ return ee(this, new d(e, t)) < 0;
430
+ }, h.isLessThanOrEqualTo = h.lte = function(e, t) {
431
+ return (t = ee(this, new d(e, t))) === -1 || t === 0;
432
+ }, h.isNaN = function() {
433
+ return !this.s;
434
+ }, h.isNegative = function() {
435
+ return this.s < 0;
436
+ }, h.isPositive = function() {
437
+ return this.s > 0;
438
+ }, h.isZero = function() {
439
+ return !!this.c && this.c[0] == 0;
440
+ }, h.minus = function(e, t) {
441
+ var i, f, r, o, l = this, n = l.s;
442
+ if (e = new d(e, t), t = e.s, !n || !t) return new d(NaN);
443
+ if (n != t)
444
+ return e.s = -t, l.plus(e);
445
+ var s = l.e / E, a = e.e / E, c = l.c, u = e.c;
446
+ if (!s || !a) {
447
+ if (!c || !u) return c ? (e.s = -t, e) : new d(u ? l : NaN);
448
+ if (!c[0] || !u[0])
449
+ return u[0] ? (e.s = -t, e) : new d(c[0] ? l : (
450
+ // IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity
451
+ N == 3 ? -0 : 0
452
+ ));
453
+ }
454
+ if (s = H(s), a = H(a), c = c.slice(), n = s - a) {
455
+ for ((o = n < 0) ? (n = -n, r = c) : (a = s, r = u), r.reverse(), t = n; t--; r.push(0)) ;
456
+ r.reverse();
457
+ } else
458
+ for (f = (o = (n = c.length) < (t = u.length)) ? n : t, n = t = 0; t < f; t++)
459
+ if (c[t] != u[t]) {
460
+ o = c[t] < u[t];
461
+ break;
462
+ }
463
+ if (o && (r = c, c = u, u = r, e.s = -e.s), t = (f = u.length) - (i = c.length), t > 0) for (; t--; c[i++] = 0) ;
464
+ for (t = $ - 1; f > n; ) {
465
+ if (c[--f] < u[f]) {
466
+ for (i = f; i && !c[--i]; c[i] = t) ;
467
+ --c[i], c[f] += $;
468
+ }
469
+ c[f] -= u[f];
470
+ }
471
+ for (; c[0] == 0; c.splice(0, 1), --a) ;
472
+ return c[0] ? le(e, c, a) : (e.s = N == 3 ? -1 : 1, e.c = [e.e = 0], e);
473
+ }, h.modulo = h.mod = function(e, t) {
474
+ var i, f, r = this;
475
+ return e = new d(e, t), !r.c || !e.s || e.c && !e.c[0] ? new d(NaN) : !e.c || r.c && !r.c[0] ? new d(r) : (te == 9 ? (f = e.s, e.s = 1, i = p(r, e, 0, 3), e.s = f, i.s *= f) : i = p(r, e, 0, te), e = r.minus(i.times(e)), !e.c[0] && te == 1 && (e.s = r.s), e);
476
+ }, h.multipliedBy = h.times = function(e, t) {
477
+ var i, f, r, o, l, n, s, a, c, u, k, O, S, P, v, T = this, x = T.c, R = (e = new d(e, t)).c;
478
+ if (!x || !R || !x[0] || !R[0])
479
+ return !T.s || !e.s || x && !x[0] && !R || R && !R[0] && !x ? e.c = e.e = e.s = null : (e.s *= T.s, !x || !R ? e.c = e.e = null : (e.c = [0], e.e = 0)), e;
480
+ for (f = H(T.e / E) + H(e.e / E), e.s *= T.s, s = x.length, u = R.length, s < u && (S = x, x = R, R = S, r = s, s = u, u = r), r = s + u, S = []; r--; S.push(0)) ;
481
+ for (P = $, v = J, r = u; --r >= 0; ) {
482
+ for (i = 0, k = R[r] % v, O = R[r] / v | 0, l = s, o = r + l; o > r; )
483
+ a = x[--l] % v, c = x[l] / v | 0, n = O * a + c * k, a = k * a + n % v * v + S[o] + i, i = (a / P | 0) + (n / v | 0) + O * c, S[o--] = a % P;
484
+ S[o] = i;
485
+ }
486
+ return i ? ++f : S.splice(0, 1), le(e, S, f);
487
+ }, h.negated = function() {
488
+ var e = new d(this);
489
+ return e.s = -e.s || null, e;
490
+ }, h.plus = function(e, t) {
491
+ var i, f = this, r = f.s;
492
+ if (e = new d(e, t), t = e.s, !r || !t) return new d(NaN);
493
+ if (r != t)
494
+ return e.s = -t, f.minus(e);
495
+ var o = f.e / E, l = e.e / E, n = f.c, s = e.c;
496
+ if (!o || !l) {
497
+ if (!n || !s) return new d(r / 0);
498
+ if (!n[0] || !s[0]) return s[0] ? e : new d(n[0] ? f : r * 0);
499
+ }
500
+ if (o = H(o), l = H(l), n = n.slice(), r = o - l) {
501
+ for (r > 0 ? (l = o, i = s) : (r = -r, i = n), i.reverse(); r--; i.push(0)) ;
502
+ i.reverse();
503
+ }
504
+ for (r = n.length, t = s.length, r - t < 0 && (i = s, s = n, n = i, t = r), r = 0; t; )
505
+ r = (n[--t] = n[t] + s[t] + r) / $ | 0, n[t] = $ === n[t] ? 0 : n[t] % $;
506
+ return r && (n = [r].concat(n), ++l), le(e, n, l);
507
+ }, h.precision = h.sd = function(e, t) {
508
+ var i, f, r, o = this;
509
+ if (e != null && e !== !!e)
510
+ return U(e, 1, I), t == null ? t = N : U(t, 0, 8), F(new d(o), e, t);
511
+ if (!(i = o.c)) return null;
512
+ if (r = i.length - 1, f = r * E + 1, r = i[r]) {
513
+ for (; r % 10 == 0; r /= 10, f--) ;
514
+ for (r = i[0]; r >= 10; r /= 10, f++) ;
515
+ }
516
+ return e && o.e + 1 > f && (f = o.e + 1), f;
517
+ }, h.shiftedBy = function(e) {
518
+ return U(e, -de, de), this.times("1e" + e);
519
+ }, h.squareRoot = h.sqrt = function() {
520
+ var e, t, i, f, r, o = this, l = o.c, n = o.s, s = o.e, a = A + 4, c = new d("0.5");
521
+ if (n !== 1 || !l || !l[0])
522
+ return new d(!n || n < 0 && (!l || l[0]) ? NaN : l ? o : 1 / 0);
523
+ if (n = Math.sqrt(+X(o)), n == 0 || n == 1 / 0 ? (t = q(l), (t.length + s) % 2 == 0 && (t += "0"), n = Math.sqrt(+t), s = H((s + 1) / 2) - (s < 0 || s % 2), n == 1 / 0 ? t = "5e" + s : (t = n.toExponential(), t = t.slice(0, t.indexOf("e") + 1) + s), i = new d(t)) : i = new d(n + ""), i.c[0]) {
524
+ for (s = i.e, n = s + a, n < 3 && (n = 0); ; )
525
+ if (r = i, i = c.times(r.plus(p(o, r, a, 1))), q(r.c).slice(0, n) === (t = q(i.c)).slice(0, n))
526
+ if (i.e < s && --n, t = t.slice(n - 3, n + 1), t == "9999" || !f && t == "4999") {
527
+ if (!f && (F(r, r.e + A + 2, 0), r.times(r).eq(o))) {
528
+ i = r;
529
+ break;
530
+ }
531
+ a += 4, n += 4, f = 1;
532
+ } else {
533
+ (!+t || !+t.slice(1) && t.charAt(0) == "5") && (F(i, i.e + A + 2, 1), e = !i.times(i).eq(o));
534
+ break;
535
+ }
536
+ }
537
+ return F(i, i.e + A + 1, N, e);
538
+ }, h.toExponential = function(e, t) {
539
+ return e != null && (U(e, 0, I), e++), ae(this, e, t, 1);
540
+ }, h.toFixed = function(e, t) {
541
+ return e != null && (U(e, 0, I), e = e + this.e + 1), ae(this, e, t);
542
+ }, h.toFormat = function(e, t, i) {
543
+ var f, r = this;
544
+ if (i == null)
545
+ e != null && t && typeof t == "object" ? (i = t, t = null) : e && typeof e == "object" ? (i = e, e = t = null) : i = ce;
546
+ else if (typeof i != "object")
547
+ throw Error(B + "Argument not an object: " + i);
548
+ if (f = r.toFixed(e, t), r.c) {
549
+ var o, l = f.split("."), n = +i.groupSize, s = +i.secondaryGroupSize, a = i.groupSeparator || "", c = l[0], u = l[1], k = r.s < 0, O = k ? c.slice(1) : c, S = O.length;
550
+ if (s && (o = n, n = s, s = o, S -= o), n > 0 && S > 0) {
551
+ for (o = S % n || n, c = O.substr(0, o); o < S; o += n) c += a + O.substr(o, n);
552
+ s > 0 && (c += a + O.slice(o)), k && (c = "-" + c);
553
+ }
554
+ f = u ? c + (i.decimalSeparator || "") + ((s = +i.fractionGroupSize) ? u.replace(
555
+ new RegExp("\\d{" + s + "}\\B", "g"),
556
+ "$&" + (i.fractionGroupSeparator || "")
557
+ ) : u) : c;
558
+ }
559
+ return (i.prefix || "") + f + (i.suffix || "");
560
+ }, h.toFraction = function(e) {
561
+ var t, i, f, r, o, l, n, s, a, c, u, k, O = this, S = O.c;
562
+ if (e != null && (n = new d(e), !n.isInteger() && (n.c || n.s !== 1) || n.lt(g)))
563
+ throw Error(B + "Argument " + (n.isInteger() ? "out of range: " : "not an integer: ") + X(n));
564
+ if (!S) return new d(O);
565
+ for (t = new d(g), a = i = new d(g), f = s = new d(g), k = q(S), o = t.e = k.length - O.e - 1, t.c[0] = we[(l = o % E) < 0 ? E + l : l], e = !e || n.comparedTo(t) > 0 ? o > 0 ? t : a : n, l = j, j = 1 / 0, n = new d(k), s.c[0] = 0; c = p(n, t, 0, 1), r = i.plus(c.times(f)), r.comparedTo(e) != 1; )
566
+ i = f, f = r, a = s.plus(c.times(r = a)), s = r, t = n.minus(c.times(r = t)), n = r;
567
+ return r = p(e.minus(i), f, 0, 1), s = s.plus(r.times(a)), i = i.plus(r.times(f)), s.s = a.s = O.s, o = o * 2, u = p(a, f, o, N).minus(O).abs().comparedTo(
568
+ p(s, i, o, N).minus(O).abs()
569
+ ) < 1 ? [a, f] : [s, i], j = l, u;
570
+ }, h.toNumber = function() {
571
+ return +X(this);
572
+ }, h.toPrecision = function(e, t) {
573
+ return e != null && U(e, 1, I), ae(this, e, t, 2);
574
+ }, h.toString = function(e) {
575
+ var t, i = this, f = i.s, r = i.e;
576
+ return r === null ? f ? (t = "Infinity", f < 0 && (t = "-" + t)) : t = "NaN" : (e == null ? t = r <= C || r >= M ? oe(q(i.c), r) : W(q(i.c), r, "0") : e === 10 && fe ? (i = F(new d(i), A + r + 1, N), t = W(q(i.c), i.e, "0")) : (U(e, 2, b.length, "Base"), t = m(W(q(i.c), r, "0"), 10, e, f, !0)), f < 0 && i.c[0] && (t = "-" + t)), t;
577
+ }, h.valueOf = h.toJSON = function() {
578
+ return X(this);
579
+ }, h._isBigNumber = !0, h[Symbol.toStringTag] = "BigNumber", h[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")] = h.valueOf, _ != null && d.set(_), d;
580
+ }
581
+ function H(_) {
582
+ var p = _ | 0;
583
+ return _ > 0 || _ === p ? p : p - 1;
584
+ }
585
+ function q(_) {
586
+ for (var p, m, w = 1, h = _.length, g = _[0] + ""; w < h; ) {
587
+ for (p = _[w++] + "", m = E - p.length; m--; p = "0" + p) ;
588
+ g += p;
589
+ }
590
+ for (h = g.length; g.charCodeAt(--h) === 48; ) ;
591
+ return g.slice(0, h + 1 || 1);
592
+ }
593
+ function ee(_, p) {
594
+ var m, w, h = _.c, g = p.c, A = _.s, N = p.s, C = _.e, M = p.e;
595
+ if (!A || !N) return null;
596
+ if (m = h && !h[0], w = g && !g[0], m || w) return m ? w ? 0 : -N : A;
597
+ if (A != N) return A;
598
+ if (m = A < 0, w = C == M, !h || !g) return w ? 0 : !h ^ m ? 1 : -1;
599
+ if (!w) return C > M ^ m ? 1 : -1;
600
+ for (N = (C = h.length) < (M = g.length) ? C : M, A = 0; A < N; A++) if (h[A] != g[A]) return h[A] > g[A] ^ m ? 1 : -1;
601
+ return C == M ? 0 : C > M ^ m ? 1 : -1;
602
+ }
603
+ function U(_, p, m, w) {
604
+ if (_ < p || _ > m || _ !== V(_))
605
+ throw Error(B + (w || "Argument") + (typeof _ == "number" ? _ < p || _ > m ? " out of range: " : " not an integer: " : " not a primitive number: ") + String(_));
606
+ }
607
+ function se(_) {
608
+ var p = _.c.length - 1;
609
+ return H(_.e / E) == p && _.c[p] % 2 != 0;
610
+ }
611
+ function oe(_, p) {
612
+ return (_.length > 1 ? _.charAt(0) + "." + _.slice(1) : _) + (p < 0 ? "e" : "e+") + p;
613
+ }
614
+ function W(_, p, m) {
615
+ var w, h;
616
+ if (p < 0) {
617
+ for (h = m + "."; ++p; h += m) ;
618
+ _ = h + _;
619
+ } else if (w = _.length, ++p > w) {
620
+ for (h = m, p -= w; --p; h += m) ;
621
+ _ += h;
622
+ } else p < w && (_ = _.slice(0, p) + "." + _.slice(p));
623
+ return _;
624
+ }
625
+ Ie();
626
+ const Le = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
627
+ let Pe;
628
+ function De() {
629
+ return Pe ||= Le.slice(0, -2) + "-_", Pe;
630
+ }
631
+ function Ce(_, p, m) {
632
+ let w = "", h = 0;
633
+ for (; h < _.length; ) {
634
+ const g = _[h++], A = _[h++], N = _[h++];
635
+ if (w += p[g >> 2], w += p[(g & 3) << 4 | (A || 0) >> 4], A == null || (w += p[(A & 15) << 2 | (N || 0) >> 6]), N == null)
636
+ break;
637
+ w += p[N & 63];
638
+ }
639
+ return w;
640
+ }
641
+ function Be(_) {
642
+ return Ce(_, De(), !1);
643
+ }
644
+ function ke() {
645
+ return ye(8);
646
+ }
647
+ function Oe() {
648
+ return ye(64);
649
+ }
650
+ async function Ee(_) {
651
+ const p = "S256", m = await crypto.subtle.digest(
652
+ "SHA-256",
653
+ new TextEncoder().encode(_)
654
+ );
655
+ return {
656
+ codeChallenge: Be(new Uint8Array(m)),
657
+ codeChallengeMethod: p
658
+ };
659
+ }
660
+ class Ae {
661
+ constructor(p) {
662
+ this.options = p;
663
+ }
664
+ async getToken() {
665
+ const p = await this.options.storage.get("accessToken");
666
+ if (!p) throw new D(ne);
667
+ if (p.expiresAt > Date.now())
668
+ throw new D(ve);
669
+ return p.token;
670
+ }
671
+ }
672
+ const Ne = class Ne extends Ae {
673
+ async buildAuthUrl() {
674
+ this.session = {
675
+ state: ke(),
676
+ codeVerifier: Oe()
677
+ };
678
+ const { codeChallenge: p, codeChallengeMethod: m } = await Ee(
679
+ this.session.codeVerifier
680
+ ), w = new URL("https://www.dropbox.com/oauth2/authorize");
681
+ return Object.entries({
682
+ client_id: this.options.clientId,
683
+ code_challenge: p,
684
+ code_challenge_method: m,
685
+ redirect_uri: this.options.redirectUrl,
686
+ response_type: "code",
687
+ token_access_type: "offline",
688
+ scope: this.options.scope,
689
+ state: this.session.state
690
+ }).forEach(([h, g]) => {
691
+ g && w.searchParams.set(h, g);
692
+ }), w.href;
693
+ }
694
+ async finishAuth(p) {
695
+ if (!this.session || this.session.state !== p.searchParams.get("state"))
696
+ throw new D(Y, "state doesn't match");
697
+ const m = p.searchParams.get("code");
698
+ if (!m) throw new D(Y, "Invalid code");
699
+ const w = new URLSearchParams();
700
+ Object.entries({
701
+ client_id: this.options.clientId,
702
+ client_secret: this.options.clientSecret,
703
+ code: m,
704
+ code_verifier: this.session.codeVerifier,
705
+ grant_type: "authorization_code",
706
+ redirect_uri: this.options.redirectUrl
707
+ }).forEach(([A, N]) => {
708
+ w.append(A, N);
709
+ });
710
+ const h = await fetch("https://api.dropbox.com/oauth2/token", {
711
+ method: "POST",
712
+ body: w
713
+ }), g = await h.json();
714
+ if (!h.ok) throw { status: h.status, data: g };
715
+ if (!g.refresh_token)
716
+ throw new D(Y, "Failed to get refresh_token");
717
+ return await this.options.storage.set("refreshToken", g.refresh_token), await this.options.storage.set("accessToken", {
718
+ token: g.access_token,
719
+ expiresAt: Date.now() + g.expires_in * 1e3
720
+ }), g.access_token;
721
+ }
722
+ async refreshToken() {
723
+ const p = new URLSearchParams(), m = await this.options.storage.get("refreshToken");
724
+ if (!m)
725
+ throw new D(ne, "Invalid refresh token");
726
+ Object.entries({
727
+ client_id: this.options.clientId,
728
+ client_secret: this.options.clientSecret,
729
+ grant_type: "refresh_token",
730
+ refresh_token: m
731
+ }).forEach(([g, A]) => {
732
+ p.append(g, A);
733
+ });
734
+ const w = await fetch("https://api.dropbox.com/oauth2/token", {
735
+ method: "POST",
736
+ body: p
737
+ }), h = await w.json();
738
+ if (!w.ok) throw { status: w.status, data: h };
739
+ return await this.options.storage.set("accessToken", {
740
+ token: h.access_token,
741
+ expiresAt: Date.now() + h.expires_in * 1e3
742
+ }), h.access_token;
743
+ }
744
+ };
745
+ Ne.Scopes = {
746
+ account: "account_info.read"
747
+ };
748
+ let ge = Ne;
749
+ const Te = class Te extends Ae {
750
+ async buildAuthUrl() {
751
+ this.session = {
752
+ state: ke(),
753
+ codeVerifier: Oe()
754
+ };
755
+ const { codeChallenge: p, codeChallengeMethod: m } = await Ee(
756
+ this.session.codeVerifier
757
+ ), w = new URL("https://accounts.google.com/o/oauth2/v2/auth");
758
+ return Object.entries({
759
+ access_type: "offline",
760
+ client_id: this.options.clientId,
761
+ code_challenge: p,
762
+ code_challenge_method: m,
763
+ include_granted_scopes: "true",
764
+ prompt: "consent",
765
+ redirect_uri: this.options.redirectUrl,
766
+ response_type: "code",
767
+ scope: this.options.scope,
768
+ state: this.session.state
769
+ }).forEach(([h, g]) => {
770
+ g && w.searchParams.set(h, g);
771
+ }), w.href;
772
+ }
773
+ async finishAuth(p) {
774
+ if (!this.session || this.session.state !== p.searchParams.get("state"))
775
+ throw new D(Y, "state doesn't match");
776
+ const m = p.searchParams.get("code");
777
+ if (!m) throw new D(Y, "Invalid code");
778
+ const w = new URLSearchParams();
779
+ Object.entries({
780
+ client_id: this.options.clientId,
781
+ client_secret: this.options.clientSecret,
782
+ code: m,
783
+ code_verifier: this.session.codeVerifier,
784
+ grant_type: "authorization_code",
785
+ redirect_uri: this.options.redirectUrl
786
+ }).forEach(([A, N]) => {
787
+ w.append(A, N);
788
+ });
789
+ const h = await fetch("https://oauth2.googleapis.com/token", {
790
+ method: "POST",
791
+ body: w
792
+ }), g = await h.json();
793
+ if (!h.ok) throw { status: h.status, data: g };
794
+ if (!g.refresh_token)
795
+ throw new D(Y, "Failed to get refresh_token");
796
+ return await this.options.storage.set("refreshToken", g.refresh_token), await this.options.storage.set("accessToken", {
797
+ token: g.access_token,
798
+ expiresAt: Date.now() + g.expires_in * 1e3
799
+ }), g.access_token;
800
+ }
801
+ async refreshToken() {
802
+ const p = new URLSearchParams(), m = await this.options.storage.get("refreshToken");
803
+ if (!m)
804
+ throw new D(ne, "Invalid refresh token");
805
+ Object.entries({
806
+ client_id: this.options.clientId,
807
+ client_secret: this.options.clientSecret,
808
+ grant_type: "refresh_token",
809
+ refresh_token: m
810
+ }).forEach(([g, A]) => {
811
+ p.append(g, A);
812
+ });
813
+ const w = await fetch("https://oauth2.googleapis.com/token", {
814
+ method: "POST",
815
+ body: p
816
+ }), h = await w.json();
817
+ if (!w.ok) throw { status: w.status, data: h };
818
+ return await this.options.storage.set("accessToken", {
819
+ token: h.access_token,
820
+ expiresAt: Date.now() + h.expires_in * 1e3
821
+ }), h.access_token;
822
+ }
823
+ };
824
+ Te.Scopes = {
825
+ account: "https://www.googleapis.com/auth/userinfo.profile",
826
+ "drive.appdata": "https://www.googleapis.com/auth/drive.appdata",
827
+ imap: "https://mail.google.com/"
828
+ };
829
+ let _e = Te;
830
+ const Se = class Se extends Ae {
831
+ async buildAuthUrl() {
832
+ this.session = {
833
+ state: ke(),
834
+ codeVerifier: Oe()
835
+ };
836
+ const { codeChallenge: p, codeChallengeMethod: m } = await Ee(
837
+ this.session.codeVerifier
838
+ ), w = new URL(
839
+ "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
840
+ );
841
+ return Object.entries({
842
+ client_id: this.options.clientId,
843
+ code_challenge: p,
844
+ code_challenge_method: m,
845
+ redirect_uri: this.options.redirectUrl,
846
+ response_mode: "query",
847
+ response_type: "code",
848
+ scope: this.options.scope,
849
+ state: this.session.state
850
+ }).forEach(([h, g]) => {
851
+ g && w.searchParams.set(h, g);
852
+ }), w.href;
853
+ }
854
+ async finishAuth(p) {
855
+ if (!this.session || this.session.state !== p.searchParams.get("state"))
856
+ throw new D(Y, "state doesn't match");
857
+ const m = p.searchParams.get("code");
858
+ if (!m) throw new D(Y, "Invalid code");
859
+ const w = new URLSearchParams();
860
+ Object.entries({
861
+ client_id: this.options.clientId,
862
+ client_secret: this.options.clientSecret,
863
+ code: m,
864
+ code_verifier: this.session.codeVerifier,
865
+ grant_type: "authorization_code",
866
+ redirect_uri: this.options.redirectUrl,
867
+ scope: this.options.scope
868
+ }).forEach(([A, N]) => {
869
+ N && w.append(A, N);
870
+ });
871
+ const h = await fetch(
872
+ "https://login.microsoftonline.com/common/oauth2/v2.0/token",
873
+ {
874
+ method: "POST",
875
+ body: w
876
+ }
877
+ ), g = await h.json();
878
+ if (!h.ok) throw { status: h.status, data: g };
879
+ if (!g.refresh_token)
880
+ throw new D(Y, "Failed to get refresh_token");
881
+ return await this.options.storage.set("refreshToken", g.refresh_token), await this.options.storage.set("accessToken", {
882
+ token: g.access_token,
883
+ expiresAt: Date.now() + g.expires_in * 1e3
884
+ }), g.access_token;
885
+ }
886
+ async refreshToken() {
887
+ const p = new URLSearchParams(), m = await this.options.storage.get("refreshToken");
888
+ if (!m)
889
+ throw new D(ne, "Invalid refresh token");
890
+ Object.entries({
891
+ client_id: this.options.clientId,
892
+ client_secret: this.options.clientSecret,
893
+ grant_type: "refresh_token",
894
+ refresh_token: m,
895
+ scope: this.options.scope
896
+ }).forEach(([g, A]) => {
897
+ A && p.append(g, A);
898
+ });
899
+ const w = await fetch(
900
+ "https://login.microsoftonline.com/common/oauth2/v2.0/token",
901
+ {
902
+ method: "POST",
903
+ body: p
904
+ }
905
+ ), h = await w.json();
906
+ if (!w.ok) throw { status: w.status, data: h };
907
+ return await this.options.storage.set("accessToken", {
908
+ token: h.access_token,
909
+ expiresAt: Date.now() + h.expires_in * 1e3
910
+ }), h.access_token;
911
+ }
912
+ };
913
+ Se.Scopes = {
914
+ /** Ref: https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth */
915
+ imap: "offline_access https://outlook.office.com/IMAP.AccessAsUser.All",
916
+ onedrive: "openid profile Files.ReadWrite.AppFolder offline_access"
917
+ };
918
+ let me = Se;
919
+ const Me = {
920
+ dropbox: ge,
921
+ google: _e,
922
+ microsoft: me
923
+ };
924
+ function Ge(_, p) {
925
+ return new Me[p.provider](_);
926
+ }
927
+ async function qe(_, p) {
928
+ let m;
929
+ try {
930
+ m = await _.getToken();
931
+ } catch (w) {
932
+ if (!(w instanceof D))
933
+ throw w;
934
+ switch (w.code) {
935
+ case ne: {
936
+ if (!p) throw w;
937
+ const h = await _.buildAuthUrl(), g = await p(h);
938
+ m = await _.finishAuth(new URL(g));
939
+ break;
940
+ }
941
+ case ve: {
942
+ m = await _.refreshToken();
943
+ break;
944
+ }
945
+ default:
946
+ throw w;
947
+ }
948
+ }
949
+ return m;
950
+ }
951
+ export {
952
+ ge as DropboxAuthorizer,
953
+ _e as GoogleAuthorizer,
954
+ me as MicrosoftAuthorizer,
955
+ Y as OAUTH2_AUTH_ERROR,
956
+ ve as OAUTH2_NEED_REFRESH,
957
+ ne as OAUTH2_UNAUTHORIZED,
958
+ Ae as OAuth2Authorizer,
959
+ Me as OAuth2Authorizers,
960
+ D as OAuth2Error,
961
+ qe as ensureAccessToken,
962
+ Ge as getAuthorizer
963
+ };
@@ -0,0 +1,9 @@
1
+ import { IOAuth2Options } from '../types';
2
+ export declare abstract class OAuth2Authorizer {
3
+ protected options: IOAuth2Options;
4
+ abstract buildAuthUrl(): Promise<string>;
5
+ abstract finishAuth(url: URL): Promise<string>;
6
+ abstract refreshToken(): Promise<string>;
7
+ constructor(options: IOAuth2Options);
8
+ getToken(): Promise<string>;
9
+ }
@@ -0,0 +1,13 @@
1
+ import { OAuth2Authorizer } from './base.ts';
2
+ export declare class DropboxAuthorizer extends OAuth2Authorizer {
3
+ private session;
4
+ /**
5
+ * Ref: https://www.dropbox.com/developers/documentation/http/documentation
6
+ */
7
+ static Scopes: {
8
+ account: string;
9
+ };
10
+ buildAuthUrl(): Promise<string>;
11
+ finishAuth(url: URL): Promise<string>;
12
+ refreshToken(): Promise<string>;
13
+ }
@@ -0,0 +1,18 @@
1
+ import { OAuth2Authorizer } from './base.ts';
2
+ /**
3
+ * Ref: https://developers.google.com/identity/protocols/oauth2/web-server
4
+ */
5
+ export declare class GoogleAuthorizer extends OAuth2Authorizer {
6
+ private session;
7
+ /**
8
+ * Ref: https://developers.google.com/identity/protocols/oauth2/scopes
9
+ */
10
+ static Scopes: {
11
+ account: string;
12
+ 'drive.appdata': string;
13
+ imap: string;
14
+ };
15
+ buildAuthUrl(): Promise<string>;
16
+ finishAuth(url: URL): Promise<string>;
17
+ refreshToken(): Promise<string>;
18
+ }
@@ -0,0 +1,10 @@
1
+ import { DropboxAuthorizer } from './dropbox';
2
+ import { GoogleAuthorizer } from './google';
3
+ import { MicrosoftAuthorizer } from './microsoft';
4
+ export * from './base';
5
+ export { DropboxAuthorizer, GoogleAuthorizer, MicrosoftAuthorizer };
6
+ export declare const OAuth2Authorizers: {
7
+ dropbox: typeof DropboxAuthorizer;
8
+ google: typeof GoogleAuthorizer;
9
+ microsoft: typeof MicrosoftAuthorizer;
10
+ };
@@ -0,0 +1,15 @@
1
+ import { OAuth2Authorizer } from './base.ts';
2
+ /**
3
+ * Ref: https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow
4
+ */
5
+ export declare class MicrosoftAuthorizer extends OAuth2Authorizer {
6
+ private session;
7
+ static Scopes: {
8
+ /** Ref: https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth */
9
+ imap: string;
10
+ onedrive: string;
11
+ };
12
+ buildAuthUrl(): Promise<string>;
13
+ finishAuth(url: URL): Promise<string>;
14
+ refreshToken(): Promise<string>;
15
+ }
@@ -0,0 +1,16 @@
1
+ import { OAuth2Authorizers } from './providers';
2
+ export interface IStorage {
3
+ get<T>(key: string): Promise<T | undefined>;
4
+ set<T>(key: string, value: T): Promise<void>;
5
+ }
6
+ export interface IOAuth2Options {
7
+ clientId: string;
8
+ clientSecret: string;
9
+ redirectUrl: string;
10
+ storage: IStorage;
11
+ scope?: string;
12
+ }
13
+ export interface IOAuth2Account {
14
+ provider: keyof typeof OAuth2Authorizers;
15
+ user: string;
16
+ }
package/dist/util.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export declare function getState(): string;
2
+ /**
3
+ * Ref: https://datatracker.ietf.org/doc/html/rfc7636#section-4.1
4
+ * high-entropy cryptographic random STRING using the
5
+ * unreserved characters [A-Z] / [a-z] / [0-9] / "-" / "." / "_" / "~"
6
+ * from Section 2.3 of [RFC3986], with a minimum length of 43 characters
7
+ * and a maximum length of 128 characters.
8
+ */
9
+ export declare function getCodeVerifier(): string;
10
+ export declare function getCodeChallenge(codeVerifier: string): Promise<{
11
+ codeChallenge: string;
12
+ codeChallengeMethod: string;
13
+ }>;
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@usync/oauth2",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "import": "./dist/index.js",
8
+ "types": "./dist/index.d.ts"
9
+ }
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "publishConfig": {
15
+ "access": "public",
16
+ "registry": "https://registry.npmjs.org/"
17
+ },
18
+ "dependencies": {
19
+ "nanoid": "^5.1.6"
20
+ },
21
+ "scripts": {
22
+ "clean": "del-cli dist tsconfig.tsbuildinfo",
23
+ "build:types": "tsc",
24
+ "build:js": "vite build",
25
+ "build": "pnpm clean && pnpm /^build:/"
26
+ }
27
+ }