@usync/oauth2 0.1.2 → 0.1.3
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/LICENSE +15 -0
- package/dist/index.js +334 -341
- package/package.json +2 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 gera2ld
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
package/dist/index.js
CHANGED
|
@@ -1,348 +1,341 @@
|
|
|
1
|
-
import { nanoid as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
function _() {
|
|
15
|
-
return U(8);
|
|
1
|
+
import { nanoid as e } from "nanoid";
|
|
2
|
+
//#region src/common.ts
|
|
3
|
+
var t = 1, n = 2, r = 3, i = class extends Error {
|
|
4
|
+
constructor(e, t) {
|
|
5
|
+
super(t || `OAuth2Error: code=${e}`), this.code = e;
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/util.ts
|
|
10
|
+
function a(e) {
|
|
11
|
+
let t = "";
|
|
12
|
+
for (let n of e) t += String.fromCharCode(n);
|
|
13
|
+
return btoa(t).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
16
14
|
}
|
|
17
|
-
function
|
|
18
|
-
|
|
15
|
+
function o() {
|
|
16
|
+
return e(8);
|
|
19
17
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
codeChallenge: O(new Uint8Array(s)),
|
|
24
|
-
codeChallengeMethod: e
|
|
25
|
-
};
|
|
18
|
+
function s() {
|
|
19
|
+
return e(64);
|
|
26
20
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
_updateAccessToken(e) {
|
|
35
|
-
this.setAccessToken(e), this.options.onSetAccessToken?.(e);
|
|
36
|
-
}
|
|
37
|
-
_updateRefreshToken(e) {
|
|
38
|
-
this.setRefreshToken(e), this.options.onSetRefreshToken?.(e);
|
|
39
|
-
}
|
|
40
|
-
getAccessToken() {
|
|
41
|
-
if (!this._accessToken) throw new i(d);
|
|
42
|
-
const e = this._getValidToken(this._accessToken);
|
|
43
|
-
if (!e) throw new i(R);
|
|
44
|
-
return e;
|
|
45
|
-
}
|
|
46
|
-
getRefreshToken() {
|
|
47
|
-
const e = this._getValidToken(this._refreshToken);
|
|
48
|
-
if (!e) throw new i(d, "Invalid refresh token");
|
|
49
|
-
return e;
|
|
50
|
-
}
|
|
51
|
-
setAccessToken(e) {
|
|
52
|
-
this._accessToken = e ?? null;
|
|
53
|
-
}
|
|
54
|
-
setRefreshToken(e) {
|
|
55
|
-
this._refreshToken = e ?? null;
|
|
56
|
-
}
|
|
21
|
+
async function c(e) {
|
|
22
|
+
let t = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(e));
|
|
23
|
+
return {
|
|
24
|
+
codeChallenge: a(new Uint8Array(t)),
|
|
25
|
+
codeChallengeMethod: "S256"
|
|
26
|
+
};
|
|
57
27
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/providers/base.ts
|
|
30
|
+
var l = class {
|
|
31
|
+
constructor(e, t) {
|
|
32
|
+
this.options = e, this._accessToken = null, this._refreshToken = null, t && (this.setRefreshToken(t.refreshToken), this.setAccessToken(t.accessToken), this.session = t.session);
|
|
33
|
+
}
|
|
34
|
+
_getValidToken(e) {
|
|
35
|
+
return e && (!e.expiresAt || Date.now() < e.expiresAt) ? e.token : void 0;
|
|
36
|
+
}
|
|
37
|
+
_updateAccessToken(e) {
|
|
38
|
+
this.setAccessToken(e), this.options.onSetAccessToken?.(e);
|
|
39
|
+
}
|
|
40
|
+
_updateRefreshToken(e) {
|
|
41
|
+
this.setRefreshToken(e), this.options.onSetRefreshToken?.(e);
|
|
42
|
+
}
|
|
43
|
+
getAccessToken() {
|
|
44
|
+
if (!this._accessToken) throw new i(3);
|
|
45
|
+
let e = this._getValidToken(this._accessToken);
|
|
46
|
+
if (!e) throw new i(1);
|
|
47
|
+
return e;
|
|
48
|
+
}
|
|
49
|
+
getRefreshToken() {
|
|
50
|
+
let e = this._getValidToken(this._refreshToken);
|
|
51
|
+
if (!e) throw new i(3, "Invalid refresh token");
|
|
52
|
+
return e;
|
|
53
|
+
}
|
|
54
|
+
setAccessToken(e) {
|
|
55
|
+
this._accessToken = e ?? null;
|
|
56
|
+
}
|
|
57
|
+
setRefreshToken(e) {
|
|
58
|
+
this._refreshToken = e ?? null;
|
|
59
|
+
}
|
|
60
|
+
}, u = "https://www.dropbox.com/oauth2/authorize", d = "https://api.dropbox.com/oauth2/token", f = class extends l {
|
|
61
|
+
static {
|
|
62
|
+
this.Scopes = { account: "account_info.read" };
|
|
63
|
+
}
|
|
64
|
+
async buildAuthUrl() {
|
|
65
|
+
this.session = {
|
|
66
|
+
state: o(),
|
|
67
|
+
codeVerifier: s()
|
|
68
|
+
};
|
|
69
|
+
let { codeChallenge: e, codeChallengeMethod: t } = await c(this.session.codeVerifier), n = new URL(u);
|
|
70
|
+
return Object.entries({
|
|
71
|
+
client_id: this.options.clientId,
|
|
72
|
+
code_challenge: e,
|
|
73
|
+
code_challenge_method: t,
|
|
74
|
+
redirect_uri: this.options.redirectUrl,
|
|
75
|
+
response_type: "code",
|
|
76
|
+
token_access_type: "offline",
|
|
77
|
+
scope: this.options.scope,
|
|
78
|
+
state: this.session.state
|
|
79
|
+
}).forEach(([e, t]) => {
|
|
80
|
+
t && n.searchParams.set(e, t);
|
|
81
|
+
}), n.href;
|
|
82
|
+
}
|
|
83
|
+
async finishAuth(e) {
|
|
84
|
+
if (!this.session || this.session.state !== e.searchParams.get("state")) throw new i(2, "state doesn't match");
|
|
85
|
+
let t = e.searchParams.get("code");
|
|
86
|
+
if (!t) throw new i(2, "Invalid code");
|
|
87
|
+
let n = new URLSearchParams();
|
|
88
|
+
Object.entries({
|
|
89
|
+
client_id: this.options.clientId,
|
|
90
|
+
client_secret: this.options.clientSecret,
|
|
91
|
+
code: t,
|
|
92
|
+
code_verifier: this.session.codeVerifier,
|
|
93
|
+
grant_type: "authorization_code",
|
|
94
|
+
redirect_uri: this.options.redirectUrl
|
|
95
|
+
}).forEach(([e, t]) => {
|
|
96
|
+
t != null && n.append(e, t);
|
|
97
|
+
});
|
|
98
|
+
let r = await fetch(d, {
|
|
99
|
+
method: "POST",
|
|
100
|
+
body: n
|
|
101
|
+
}), a = await r.json();
|
|
102
|
+
if (!r.ok) throw {
|
|
103
|
+
status: r.status,
|
|
104
|
+
data: a
|
|
105
|
+
};
|
|
106
|
+
if (!a.refresh_token) throw new i(2, "Failed to get refresh_token");
|
|
107
|
+
return this._updateRefreshToken({
|
|
108
|
+
token: a.refresh_token,
|
|
109
|
+
scope: a.scope
|
|
110
|
+
}), this._updateAccessToken({
|
|
111
|
+
token: a.access_token,
|
|
112
|
+
expiresAt: Date.now() + a.expires_in * 1e3
|
|
113
|
+
}), a.access_token;
|
|
114
|
+
}
|
|
115
|
+
async refreshToken() {
|
|
116
|
+
let e = new URLSearchParams(), t = this.getRefreshToken();
|
|
117
|
+
Object.entries({
|
|
118
|
+
client_id: this.options.clientId,
|
|
119
|
+
client_secret: this.options.clientSecret,
|
|
120
|
+
grant_type: "refresh_token",
|
|
121
|
+
refresh_token: t
|
|
122
|
+
}).forEach(([t, n]) => {
|
|
123
|
+
n != null && e.append(t, n);
|
|
124
|
+
});
|
|
125
|
+
let n = await fetch(d, {
|
|
126
|
+
method: "POST",
|
|
127
|
+
body: e
|
|
128
|
+
}), r = await n.json();
|
|
129
|
+
if (!n.ok) throw {
|
|
130
|
+
status: n.status,
|
|
131
|
+
data: r
|
|
132
|
+
};
|
|
133
|
+
return this._updateAccessToken({
|
|
134
|
+
token: r.access_token,
|
|
135
|
+
expiresAt: Date.now() + r.expires_in * 1e3
|
|
136
|
+
}), r.access_token;
|
|
137
|
+
}
|
|
138
|
+
}, p = "https://accounts.google.com/o/oauth2/v2/auth", m = "https://oauth2.googleapis.com/token", h = class extends l {
|
|
139
|
+
static {
|
|
140
|
+
this.Scopes = {
|
|
141
|
+
account: "https://www.googleapis.com/auth/userinfo.profile",
|
|
142
|
+
"drive.appdata": "https://www.googleapis.com/auth/drive.appdata",
|
|
143
|
+
imap: "https://mail.google.com/"
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
async buildAuthUrl() {
|
|
147
|
+
this.session = {
|
|
148
|
+
state: o(),
|
|
149
|
+
codeVerifier: s()
|
|
150
|
+
};
|
|
151
|
+
let { codeChallenge: e, codeChallengeMethod: t } = await c(this.session.codeVerifier), n = new URL(p);
|
|
152
|
+
return Object.entries({
|
|
153
|
+
access_type: "offline",
|
|
154
|
+
client_id: this.options.clientId,
|
|
155
|
+
code_challenge: e,
|
|
156
|
+
code_challenge_method: t,
|
|
157
|
+
include_granted_scopes: "true",
|
|
158
|
+
prompt: "consent",
|
|
159
|
+
redirect_uri: this.options.redirectUrl,
|
|
160
|
+
response_type: "code",
|
|
161
|
+
scope: this.options.scope,
|
|
162
|
+
state: this.session.state
|
|
163
|
+
}).forEach(([e, t]) => {
|
|
164
|
+
t && n.searchParams.set(e, t);
|
|
165
|
+
}), n.href;
|
|
166
|
+
}
|
|
167
|
+
async finishAuth(e) {
|
|
168
|
+
if (!this.session || this.session.state !== e.searchParams.get("state")) throw new i(2, "state doesn't match");
|
|
169
|
+
let t = e.searchParams.get("code");
|
|
170
|
+
if (!t) throw new i(2, "Invalid code");
|
|
171
|
+
let n = new URLSearchParams();
|
|
172
|
+
Object.entries({
|
|
173
|
+
client_id: this.options.clientId,
|
|
174
|
+
client_secret: this.options.clientSecret,
|
|
175
|
+
code: t,
|
|
176
|
+
code_verifier: this.session.codeVerifier,
|
|
177
|
+
grant_type: "authorization_code",
|
|
178
|
+
redirect_uri: this.options.redirectUrl
|
|
179
|
+
}).forEach(([e, t]) => {
|
|
180
|
+
t != null && n.append(e, t);
|
|
181
|
+
});
|
|
182
|
+
let r = await fetch(m, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
body: n
|
|
185
|
+
}), a = await r.json();
|
|
186
|
+
if (!r.ok) throw {
|
|
187
|
+
status: r.status,
|
|
188
|
+
data: a
|
|
189
|
+
};
|
|
190
|
+
if (!a.refresh_token) throw new i(2, "Failed to get refresh_token");
|
|
191
|
+
return this._updateRefreshToken({
|
|
192
|
+
token: a.refresh_token,
|
|
193
|
+
scope: a.scope
|
|
194
|
+
}), this._updateAccessToken({
|
|
195
|
+
token: a.access_token,
|
|
196
|
+
expiresAt: Date.now() + a.expires_in * 1e3
|
|
197
|
+
}), a.access_token;
|
|
198
|
+
}
|
|
199
|
+
async refreshToken() {
|
|
200
|
+
let e = new URLSearchParams(), t = this.getRefreshToken();
|
|
201
|
+
Object.entries({
|
|
202
|
+
client_id: this.options.clientId,
|
|
203
|
+
client_secret: this.options.clientSecret,
|
|
204
|
+
grant_type: "refresh_token",
|
|
205
|
+
refresh_token: t,
|
|
206
|
+
scope: this.options.scope
|
|
207
|
+
}).forEach(([t, n]) => {
|
|
208
|
+
n != null && e.append(t, n);
|
|
209
|
+
});
|
|
210
|
+
let n = await fetch(m, {
|
|
211
|
+
method: "POST",
|
|
212
|
+
body: e
|
|
213
|
+
}), r = await n.json();
|
|
214
|
+
if (!n.ok) throw {
|
|
215
|
+
status: n.status,
|
|
216
|
+
data: r
|
|
217
|
+
};
|
|
218
|
+
return this._updateAccessToken({
|
|
219
|
+
token: r.access_token,
|
|
220
|
+
expiresAt: Date.now() + r.expires_in * 1e3
|
|
221
|
+
}), r.access_token;
|
|
222
|
+
}
|
|
223
|
+
}, g = class extends l {
|
|
224
|
+
static {
|
|
225
|
+
this.Scopes = {
|
|
226
|
+
imap: "offline_access https://outlook.office.com/IMAP.AccessAsUser.All",
|
|
227
|
+
onedrive: "openid profile Files.ReadWrite.AppFolder offline_access"
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
oauth2Url(e) {
|
|
231
|
+
return `https://login.microsoftonline.com/${this.options.provider?.microsoft?.accountType ?? "common"}/oauth2/v2.0/${e}`;
|
|
232
|
+
}
|
|
233
|
+
async buildAuthUrl() {
|
|
234
|
+
this.session = {
|
|
235
|
+
state: o(),
|
|
236
|
+
codeVerifier: s()
|
|
237
|
+
};
|
|
238
|
+
let { codeChallenge: e, codeChallengeMethod: t } = await c(this.session.codeVerifier), n = new URL(this.oauth2Url("authorize"));
|
|
239
|
+
return Object.entries({
|
|
240
|
+
client_id: this.options.clientId,
|
|
241
|
+
code_challenge: e,
|
|
242
|
+
code_challenge_method: t,
|
|
243
|
+
redirect_uri: this.options.redirectUrl,
|
|
244
|
+
response_mode: "query",
|
|
245
|
+
response_type: "code",
|
|
246
|
+
scope: this.options.scope,
|
|
247
|
+
state: this.session.state
|
|
248
|
+
}).forEach(([e, t]) => {
|
|
249
|
+
t && n.searchParams.set(e, t);
|
|
250
|
+
}), n.href;
|
|
251
|
+
}
|
|
252
|
+
async finishAuth(e) {
|
|
253
|
+
if (!this.session || this.session.state !== e.searchParams.get("state")) throw new i(2, "state doesn't match");
|
|
254
|
+
let t = e.searchParams.get("code");
|
|
255
|
+
if (!t) throw new i(2, "Invalid code");
|
|
256
|
+
let n = new URLSearchParams();
|
|
257
|
+
Object.entries({
|
|
258
|
+
client_id: this.options.clientId,
|
|
259
|
+
client_secret: this.options.clientSecret,
|
|
260
|
+
code: t,
|
|
261
|
+
code_verifier: this.session.codeVerifier,
|
|
262
|
+
grant_type: "authorization_code",
|
|
263
|
+
redirect_uri: this.options.redirectUrl,
|
|
264
|
+
scope: this.options.scope
|
|
265
|
+
}).forEach(([e, t]) => {
|
|
266
|
+
t != null && n.append(e, t);
|
|
267
|
+
});
|
|
268
|
+
let r = await fetch(this.oauth2Url("token"), {
|
|
269
|
+
method: "POST",
|
|
270
|
+
body: n
|
|
271
|
+
}), a = await r.json();
|
|
272
|
+
if (!r.ok) throw {
|
|
273
|
+
status: r.status,
|
|
274
|
+
data: a
|
|
275
|
+
};
|
|
276
|
+
if (!a.refresh_token) throw new i(2, "Failed to get refresh_token");
|
|
277
|
+
return this._updateRefreshToken({
|
|
278
|
+
token: a.refresh_token,
|
|
279
|
+
scope: a.scope
|
|
280
|
+
}), this._updateAccessToken({
|
|
281
|
+
token: a.access_token,
|
|
282
|
+
expiresAt: Date.now() + a.expires_in * 1e3
|
|
283
|
+
}), a.access_token;
|
|
284
|
+
}
|
|
285
|
+
async refreshToken() {
|
|
286
|
+
let e = new URLSearchParams(), t = this.getRefreshToken();
|
|
287
|
+
Object.entries({
|
|
288
|
+
client_id: this.options.clientId,
|
|
289
|
+
client_secret: this.options.clientSecret,
|
|
290
|
+
grant_type: "refresh_token",
|
|
291
|
+
refresh_token: t,
|
|
292
|
+
scope: this.options.scope
|
|
293
|
+
}).forEach(([t, n]) => {
|
|
294
|
+
n != null && e.append(t, n);
|
|
295
|
+
});
|
|
296
|
+
let n = await fetch(this.oauth2Url("token"), {
|
|
297
|
+
method: "POST",
|
|
298
|
+
body: e
|
|
299
|
+
}), r = await n.json();
|
|
300
|
+
if (!n.ok) throw {
|
|
301
|
+
status: n.status,
|
|
302
|
+
data: r
|
|
303
|
+
};
|
|
304
|
+
return this._updateAccessToken({
|
|
305
|
+
token: r.access_token,
|
|
306
|
+
expiresAt: Date.now() + r.expires_in * 1e3
|
|
307
|
+
}), r.access_token;
|
|
308
|
+
}
|
|
309
|
+
}, _ = {
|
|
310
|
+
dropbox: f,
|
|
311
|
+
google: h,
|
|
312
|
+
microsoft: g
|
|
212
313
|
};
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
};
|
|
218
|
-
let l = g;
|
|
219
|
-
const m = class m extends w {
|
|
220
|
-
oauth2Url(e) {
|
|
221
|
-
return `https://login.microsoftonline.com/${this.options.provider?.microsoft?.accountType ?? "common"}/oauth2/v2.0/${e}`;
|
|
222
|
-
}
|
|
223
|
-
async buildAuthUrl() {
|
|
224
|
-
this.session = {
|
|
225
|
-
state: _(),
|
|
226
|
-
codeVerifier: k()
|
|
227
|
-
};
|
|
228
|
-
const { codeChallenge: e, codeChallengeMethod: s } = await u(
|
|
229
|
-
this.session.codeVerifier
|
|
230
|
-
), o = new URL(this.oauth2Url("authorize"));
|
|
231
|
-
return Object.entries({
|
|
232
|
-
client_id: this.options.clientId,
|
|
233
|
-
code_challenge: e,
|
|
234
|
-
code_challenge_method: s,
|
|
235
|
-
redirect_uri: this.options.redirectUrl,
|
|
236
|
-
response_mode: "query",
|
|
237
|
-
response_type: "code",
|
|
238
|
-
scope: this.options.scope,
|
|
239
|
-
state: this.session.state
|
|
240
|
-
}).forEach(([n, t]) => {
|
|
241
|
-
t && o.searchParams.set(n, t);
|
|
242
|
-
}), o.href;
|
|
243
|
-
}
|
|
244
|
-
async finishAuth(e) {
|
|
245
|
-
if (!this.session || this.session.state !== e.searchParams.get("state"))
|
|
246
|
-
throw new i(a, "state doesn't match");
|
|
247
|
-
const s = e.searchParams.get("code");
|
|
248
|
-
if (!s) throw new i(a, "Invalid code");
|
|
249
|
-
const o = new URLSearchParams();
|
|
250
|
-
Object.entries({
|
|
251
|
-
client_id: this.options.clientId,
|
|
252
|
-
client_secret: this.options.clientSecret,
|
|
253
|
-
code: s,
|
|
254
|
-
code_verifier: this.session.codeVerifier,
|
|
255
|
-
grant_type: "authorization_code",
|
|
256
|
-
redirect_uri: this.options.redirectUrl,
|
|
257
|
-
scope: this.options.scope
|
|
258
|
-
}).forEach(([c, h]) => {
|
|
259
|
-
h != null && o.append(c, h);
|
|
260
|
-
});
|
|
261
|
-
const n = await fetch(this.oauth2Url("token"), {
|
|
262
|
-
method: "POST",
|
|
263
|
-
body: o
|
|
264
|
-
}), t = await n.json();
|
|
265
|
-
if (!n.ok) throw { status: n.status, data: t };
|
|
266
|
-
if (!t.refresh_token)
|
|
267
|
-
throw new i(a, "Failed to get refresh_token");
|
|
268
|
-
return this._updateRefreshToken({
|
|
269
|
-
token: t.refresh_token,
|
|
270
|
-
scope: t.scope
|
|
271
|
-
}), this._updateAccessToken({
|
|
272
|
-
token: t.access_token,
|
|
273
|
-
expiresAt: Date.now() + t.expires_in * 1e3
|
|
274
|
-
}), t.access_token;
|
|
275
|
-
}
|
|
276
|
-
async refreshToken() {
|
|
277
|
-
const e = new URLSearchParams(), s = this.getRefreshToken();
|
|
278
|
-
Object.entries({
|
|
279
|
-
client_id: this.options.clientId,
|
|
280
|
-
client_secret: this.options.clientSecret,
|
|
281
|
-
grant_type: "refresh_token",
|
|
282
|
-
refresh_token: s,
|
|
283
|
-
scope: this.options.scope
|
|
284
|
-
}).forEach(([t, c]) => {
|
|
285
|
-
c != null && e.append(t, c);
|
|
286
|
-
});
|
|
287
|
-
const o = await fetch(this.oauth2Url("token"), {
|
|
288
|
-
method: "POST",
|
|
289
|
-
body: e
|
|
290
|
-
}), n = await o.json();
|
|
291
|
-
if (!o.ok) throw { status: o.status, data: n };
|
|
292
|
-
return this._updateAccessToken({
|
|
293
|
-
token: n.access_token,
|
|
294
|
-
expiresAt: Date.now() + n.expires_in * 1e3
|
|
295
|
-
}), n.access_token;
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
m.Scopes = {
|
|
299
|
-
/** Ref: https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth */
|
|
300
|
-
imap: "offline_access https://outlook.office.com/IMAP.AccessAsUser.All",
|
|
301
|
-
onedrive: "openid profile Files.ReadWrite.AppFolder offline_access"
|
|
302
|
-
};
|
|
303
|
-
let f = m;
|
|
304
|
-
const E = {
|
|
305
|
-
dropbox: p,
|
|
306
|
-
google: l,
|
|
307
|
-
microsoft: f
|
|
308
|
-
};
|
|
309
|
-
function x(r, e) {
|
|
310
|
-
return new E[e.provider](r);
|
|
314
|
+
//#endregion
|
|
315
|
+
//#region src/index.ts
|
|
316
|
+
function v(e, t) {
|
|
317
|
+
return new _[t.provider](e);
|
|
311
318
|
}
|
|
312
|
-
async function
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
return s;
|
|
319
|
+
async function y(e, t) {
|
|
320
|
+
let n;
|
|
321
|
+
try {
|
|
322
|
+
n = e.getAccessToken();
|
|
323
|
+
} catch (r) {
|
|
324
|
+
if (!(r instanceof i)) throw r;
|
|
325
|
+
switch (r.code) {
|
|
326
|
+
case 3: {
|
|
327
|
+
if (!t) throw r;
|
|
328
|
+
let i = await t(await e.buildAuthUrl());
|
|
329
|
+
n = await e.finishAuth(new URL(i));
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
case 1:
|
|
333
|
+
n = await e.refreshToken();
|
|
334
|
+
break;
|
|
335
|
+
default: throw r;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return n;
|
|
335
339
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
l as GoogleAuthorizer,
|
|
339
|
-
f as MicrosoftAuthorizer,
|
|
340
|
-
a as OAUTH2_AUTH_ERROR,
|
|
341
|
-
R as OAUTH2_NEED_REFRESH,
|
|
342
|
-
d as OAUTH2_UNAUTHORIZED,
|
|
343
|
-
w as OAuth2Authorizer,
|
|
344
|
-
E as OAuth2Authorizers,
|
|
345
|
-
i as OAuth2Error,
|
|
346
|
-
I as ensureAccessToken,
|
|
347
|
-
x as getAuthorizer
|
|
348
|
-
};
|
|
340
|
+
//#endregion
|
|
341
|
+
export { f as DropboxAuthorizer, h as GoogleAuthorizer, g as MicrosoftAuthorizer, n as OAUTH2_AUTH_ERROR, t as OAUTH2_NEED_REFRESH, r as OAUTH2_UNAUTHORIZED, l as OAuth2Authorizer, _ as OAuth2Authorizers, i as OAuth2Error, y as ensureAccessToken, v as getAuthorizer };
|