oauth.do 0.1.15 → 0.2.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/LICENSE +21 -0
- package/README.md +245 -9
- package/bin/duckdb-auth +71 -0
- package/dist/cli.js +129 -329
- package/dist/cli.js.map +1 -1
- package/dist/hono.d.ts +124 -0
- package/dist/hono.js +599 -0
- package/dist/hono.js.map +1 -0
- package/dist/index.d.ts +5 -89
- package/dist/index.js +24 -24
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +2 -1
- package/dist/node.js +108 -73
- package/dist/node.js.map +1 -1
- package/dist/react.d.ts +200 -0
- package/dist/react.js +67 -0
- package/dist/react.js.map +1 -0
- package/dist/session-hono.d.ts +143 -0
- package/dist/session-hono.js +406 -0
- package/dist/session-hono.js.map +1 -0
- package/dist/session.d.ts +83 -0
- package/dist/session.js +114 -0
- package/dist/session.js.map +1 -0
- package/dist/types-export.d.ts +90 -0
- package/dist/types-export.js +3 -0
- package/dist/types-export.js.map +1 -0
- package/package.json +83 -9
package/dist/cli.js
CHANGED
|
@@ -11,10 +11,6 @@ var __esm = (fn, res) => function __init() {
|
|
|
11
11
|
var __commonJS = (cb, mod) => function __require() {
|
|
12
12
|
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
13
13
|
};
|
|
14
|
-
var __export = (target, all) => {
|
|
15
|
-
for (var name in all)
|
|
16
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
-
};
|
|
18
14
|
var __copyProps = (to, from, except, desc) => {
|
|
19
15
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
16
|
for (let key of __getOwnPropNames(from))
|
|
@@ -32,24 +28,21 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
32
28
|
mod
|
|
33
29
|
));
|
|
34
30
|
|
|
35
|
-
// src/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
createSecureStorage: () => createSecureStorage
|
|
31
|
+
// src/utils.ts
|
|
32
|
+
function getEnv(key) {
|
|
33
|
+
if (globalThis[key]) return globalThis[key];
|
|
34
|
+
if (typeof process !== "undefined" && process.env?.[key]) return process.env[key];
|
|
35
|
+
return void 0;
|
|
36
|
+
}
|
|
37
|
+
var init_utils = __esm({
|
|
38
|
+
"src/utils.ts"() {
|
|
39
|
+
}
|
|
45
40
|
});
|
|
41
|
+
|
|
42
|
+
// src/storage.ts
|
|
46
43
|
function isNode() {
|
|
47
44
|
return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
48
45
|
}
|
|
49
|
-
function getEnv2(key) {
|
|
50
|
-
if (typeof process !== "undefined" && process.env?.[key]) return process.env[key];
|
|
51
|
-
return void 0;
|
|
52
|
-
}
|
|
53
46
|
function createSecureStorage(storagePath) {
|
|
54
47
|
if (isNode()) {
|
|
55
48
|
return new SecureFileTokenStorage(storagePath);
|
|
@@ -59,100 +52,10 @@ function createSecureStorage(storagePath) {
|
|
|
59
52
|
}
|
|
60
53
|
return new MemoryTokenStorage();
|
|
61
54
|
}
|
|
62
|
-
var
|
|
55
|
+
var SecureFileTokenStorage, MemoryTokenStorage, LocalStorageTokenStorage;
|
|
63
56
|
var init_storage = __esm({
|
|
64
57
|
"src/storage.ts"() {
|
|
65
|
-
|
|
66
|
-
KEYCHAIN_ACCOUNT = "access_token";
|
|
67
|
-
KeychainTokenStorage = class {
|
|
68
|
-
keytar = null;
|
|
69
|
-
initialized = false;
|
|
70
|
-
/**
|
|
71
|
-
* Lazily load keytar module
|
|
72
|
-
* Returns null if keytar is not available (e.g., missing native dependencies)
|
|
73
|
-
*/
|
|
74
|
-
async getKeytar() {
|
|
75
|
-
if (this.initialized) {
|
|
76
|
-
return this.keytar;
|
|
77
|
-
}
|
|
78
|
-
this.initialized = true;
|
|
79
|
-
try {
|
|
80
|
-
const imported = await import('keytar');
|
|
81
|
-
const keytarModule = imported.default || imported;
|
|
82
|
-
this.keytar = keytarModule;
|
|
83
|
-
if (typeof this.keytar.getPassword !== "function") {
|
|
84
|
-
if (getEnv2("DEBUG")) {
|
|
85
|
-
console.warn("Keytar module loaded but getPassword is not a function:", Object.keys(this.keytar));
|
|
86
|
-
}
|
|
87
|
-
this.keytar = null;
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
return this.keytar;
|
|
91
|
-
} catch (error) {
|
|
92
|
-
if (getEnv2("DEBUG")) {
|
|
93
|
-
console.warn("Keychain storage not available:", error);
|
|
94
|
-
}
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
async getToken() {
|
|
99
|
-
const keytar = await this.getKeytar();
|
|
100
|
-
if (!keytar) {
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
try {
|
|
104
|
-
const token = await keytar.getPassword(KEYCHAIN_SERVICE, KEYCHAIN_ACCOUNT);
|
|
105
|
-
return token;
|
|
106
|
-
} catch (error) {
|
|
107
|
-
if (getEnv2("DEBUG")) {
|
|
108
|
-
console.warn("Failed to get token from keychain:", error);
|
|
109
|
-
}
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
async setToken(token) {
|
|
114
|
-
try {
|
|
115
|
-
const keytar = await this.getKeytar();
|
|
116
|
-
if (!keytar) {
|
|
117
|
-
throw new Error("Keychain storage not available");
|
|
118
|
-
}
|
|
119
|
-
await keytar.setPassword(KEYCHAIN_SERVICE, KEYCHAIN_ACCOUNT, token);
|
|
120
|
-
} catch (error) {
|
|
121
|
-
if (error?.code === "MODULE_NOT_FOUND" || error?.message?.includes("Cannot find module")) {
|
|
122
|
-
throw new Error("Keychain storage not available: native module not built");
|
|
123
|
-
}
|
|
124
|
-
throw new Error(`Failed to save token to keychain: ${error}`);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
async removeToken() {
|
|
128
|
-
const keytar = await this.getKeytar();
|
|
129
|
-
if (!keytar) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
try {
|
|
133
|
-
await keytar.deletePassword(KEYCHAIN_SERVICE, KEYCHAIN_ACCOUNT);
|
|
134
|
-
} catch {
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Check if keychain storage is available on this system
|
|
139
|
-
*/
|
|
140
|
-
async isAvailable() {
|
|
141
|
-
try {
|
|
142
|
-
const keytar = await this.getKeytar();
|
|
143
|
-
if (!keytar) {
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
await keytar.getPassword(KEYCHAIN_SERVICE, "__test__");
|
|
147
|
-
return true;
|
|
148
|
-
} catch (error) {
|
|
149
|
-
if (getEnv2("DEBUG")) {
|
|
150
|
-
console.warn("Keychain not available:", error);
|
|
151
|
-
}
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
};
|
|
58
|
+
init_utils();
|
|
156
59
|
SecureFileTokenStorage = class {
|
|
157
60
|
tokenPath = null;
|
|
158
61
|
configDir = null;
|
|
@@ -191,7 +94,7 @@ var init_storage = __esm({
|
|
|
191
94
|
const fs = await import('fs/promises');
|
|
192
95
|
const stats = await fs.stat(this.tokenPath);
|
|
193
96
|
const mode = stats.mode & 511;
|
|
194
|
-
if (mode !== 384 &&
|
|
97
|
+
if (mode !== 384 && getEnv("DEBUG")) {
|
|
195
98
|
console.warn(
|
|
196
99
|
`Warning: Token file has insecure permissions (${mode.toString(8)}). Expected 600. Run: chmod 600 ${this.tokenPath}`
|
|
197
100
|
);
|
|
@@ -254,56 +157,6 @@ var init_storage = __esm({
|
|
|
254
157
|
return { type: "file", secure: true, path: this.tokenPath };
|
|
255
158
|
}
|
|
256
159
|
};
|
|
257
|
-
FileTokenStorage = class {
|
|
258
|
-
tokenPath = null;
|
|
259
|
-
configDir = null;
|
|
260
|
-
initialized = false;
|
|
261
|
-
async init() {
|
|
262
|
-
if (this.initialized) return this.tokenPath !== null;
|
|
263
|
-
this.initialized = true;
|
|
264
|
-
if (!isNode()) return false;
|
|
265
|
-
try {
|
|
266
|
-
const os = await import('os');
|
|
267
|
-
const path = await import('path');
|
|
268
|
-
this.configDir = path.join(os.homedir(), ".oauth.do");
|
|
269
|
-
this.tokenPath = path.join(this.configDir, "token");
|
|
270
|
-
return true;
|
|
271
|
-
} catch {
|
|
272
|
-
return false;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
async getToken() {
|
|
276
|
-
if (!await this.init() || !this.tokenPath) return null;
|
|
277
|
-
try {
|
|
278
|
-
const fs = await import('fs/promises');
|
|
279
|
-
const token = await fs.readFile(this.tokenPath, "utf-8");
|
|
280
|
-
return token.trim();
|
|
281
|
-
} catch {
|
|
282
|
-
return null;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
async setToken(token) {
|
|
286
|
-
if (!await this.init() || !this.tokenPath || !this.configDir) {
|
|
287
|
-
throw new Error("File storage not available");
|
|
288
|
-
}
|
|
289
|
-
try {
|
|
290
|
-
const fs = await import('fs/promises');
|
|
291
|
-
await fs.mkdir(this.configDir, { recursive: true });
|
|
292
|
-
await fs.writeFile(this.tokenPath, token, "utf-8");
|
|
293
|
-
} catch (error) {
|
|
294
|
-
console.error("Failed to save token:", error);
|
|
295
|
-
throw error;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
async removeToken() {
|
|
299
|
-
if (!await this.init() || !this.tokenPath) return;
|
|
300
|
-
try {
|
|
301
|
-
const fs = await import('fs/promises');
|
|
302
|
-
await fs.unlink(this.tokenPath);
|
|
303
|
-
} catch {
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
160
|
MemoryTokenStorage = class {
|
|
308
161
|
token = null;
|
|
309
162
|
async getToken() {
|
|
@@ -337,66 +190,6 @@ var init_storage = __esm({
|
|
|
337
190
|
localStorage.removeItem(this.key);
|
|
338
191
|
}
|
|
339
192
|
};
|
|
340
|
-
CompositeTokenStorage = class {
|
|
341
|
-
keychainStorage;
|
|
342
|
-
fileStorage;
|
|
343
|
-
preferredStorage = null;
|
|
344
|
-
constructor() {
|
|
345
|
-
this.keychainStorage = new KeychainTokenStorage();
|
|
346
|
-
this.fileStorage = new SecureFileTokenStorage();
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* Determine the best available storage backend
|
|
350
|
-
*/
|
|
351
|
-
async getPreferredStorage() {
|
|
352
|
-
if (this.preferredStorage) {
|
|
353
|
-
return this.preferredStorage;
|
|
354
|
-
}
|
|
355
|
-
if (await this.keychainStorage.isAvailable()) {
|
|
356
|
-
this.preferredStorage = this.keychainStorage;
|
|
357
|
-
return this.preferredStorage;
|
|
358
|
-
}
|
|
359
|
-
this.preferredStorage = this.fileStorage;
|
|
360
|
-
return this.preferredStorage;
|
|
361
|
-
}
|
|
362
|
-
async getToken() {
|
|
363
|
-
const keychainToken = await this.keychainStorage.getToken();
|
|
364
|
-
if (keychainToken) {
|
|
365
|
-
return keychainToken;
|
|
366
|
-
}
|
|
367
|
-
const fileToken = await this.fileStorage.getToken();
|
|
368
|
-
if (fileToken) {
|
|
369
|
-
if (await this.keychainStorage.isAvailable()) {
|
|
370
|
-
try {
|
|
371
|
-
await this.keychainStorage.setToken(fileToken);
|
|
372
|
-
await this.fileStorage.removeToken();
|
|
373
|
-
if (getEnv2("DEBUG")) {
|
|
374
|
-
console.log("Migrated token from file to keychain");
|
|
375
|
-
}
|
|
376
|
-
} catch {
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
return fileToken;
|
|
380
|
-
}
|
|
381
|
-
return null;
|
|
382
|
-
}
|
|
383
|
-
async setToken(token) {
|
|
384
|
-
const storage2 = await this.getPreferredStorage();
|
|
385
|
-
await storage2.setToken(token);
|
|
386
|
-
}
|
|
387
|
-
async removeToken() {
|
|
388
|
-
await Promise.all([this.keychainStorage.removeToken(), this.fileStorage.removeToken()]);
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Get information about the current storage backend
|
|
392
|
-
*/
|
|
393
|
-
async getStorageInfo() {
|
|
394
|
-
if (await this.keychainStorage.isAvailable()) {
|
|
395
|
-
return { type: "keychain", secure: true };
|
|
396
|
-
}
|
|
397
|
-
return { type: "file", secure: true };
|
|
398
|
-
}
|
|
399
|
-
};
|
|
400
193
|
}
|
|
401
194
|
});
|
|
402
195
|
|
|
@@ -405,13 +198,14 @@ var require_package = __commonJS({
|
|
|
405
198
|
"package.json"(exports$1, module) {
|
|
406
199
|
module.exports = {
|
|
407
200
|
name: "oauth.do",
|
|
408
|
-
version: "0.1
|
|
409
|
-
description: "OAuth authentication SDK and
|
|
201
|
+
version: "0.2.1",
|
|
202
|
+
description: "OAuth authentication SDK, React components, and Hono middleware for org.ai identity",
|
|
410
203
|
type: "module",
|
|
411
204
|
main: "./dist/index.js",
|
|
412
205
|
types: "./dist/index.d.ts",
|
|
413
206
|
bin: {
|
|
414
|
-
"oauth.do": "./dist/cli.js"
|
|
207
|
+
"oauth.do": "./dist/cli.js",
|
|
208
|
+
"duckdb-auth": "./bin/duckdb-auth"
|
|
415
209
|
},
|
|
416
210
|
exports: {
|
|
417
211
|
".": {
|
|
@@ -426,10 +220,31 @@ var require_package = __commonJS({
|
|
|
426
220
|
require: "./dist/node.js",
|
|
427
221
|
default: "./dist/node.js"
|
|
428
222
|
},
|
|
223
|
+
"./react": {
|
|
224
|
+
types: "./dist/react.d.ts",
|
|
225
|
+
import: "./dist/react.js",
|
|
226
|
+
require: "./dist/react.js",
|
|
227
|
+
default: "./dist/react.js"
|
|
228
|
+
},
|
|
229
|
+
"./hono": {
|
|
230
|
+
types: "./dist/hono.d.ts",
|
|
231
|
+
import: "./dist/hono.js",
|
|
232
|
+
require: "./dist/hono.js",
|
|
233
|
+
default: "./dist/hono.js"
|
|
234
|
+
},
|
|
235
|
+
"./types": {
|
|
236
|
+
types: "./dist/types-export.d.ts",
|
|
237
|
+
import: "./dist/types-export.js"
|
|
238
|
+
},
|
|
239
|
+
"./session": {
|
|
240
|
+
types: "./dist/session.d.ts",
|
|
241
|
+
import: "./dist/session.js"
|
|
242
|
+
},
|
|
429
243
|
"./mdx/*": "./src/mdx/*"
|
|
430
244
|
},
|
|
431
245
|
files: [
|
|
432
246
|
"dist",
|
|
247
|
+
"bin",
|
|
433
248
|
"src/mdx",
|
|
434
249
|
"README.md",
|
|
435
250
|
"LICENSE"
|
|
@@ -447,16 +262,19 @@ var require_package = __commonJS({
|
|
|
447
262
|
"authentication",
|
|
448
263
|
"auth",
|
|
449
264
|
"login",
|
|
450
|
-
"
|
|
265
|
+
"identity",
|
|
451
266
|
"cli",
|
|
452
267
|
"sdk",
|
|
453
|
-
"
|
|
454
|
-
"workos"
|
|
268
|
+
"org-ai",
|
|
269
|
+
"workos",
|
|
270
|
+
"authkit",
|
|
271
|
+
"react",
|
|
272
|
+
"hono"
|
|
455
273
|
],
|
|
456
274
|
author: {
|
|
457
|
-
name: "
|
|
458
|
-
email: "npm@
|
|
459
|
-
url: "https://
|
|
275
|
+
name: "org.ai",
|
|
276
|
+
email: "npm@org.ai",
|
|
277
|
+
url: "https://org.ai"
|
|
460
278
|
},
|
|
461
279
|
license: "MIT",
|
|
462
280
|
repository: {
|
|
@@ -476,8 +294,57 @@ var require_package = __commonJS({
|
|
|
476
294
|
optionalDependencies: {
|
|
477
295
|
keytar: "^7.9.0"
|
|
478
296
|
},
|
|
297
|
+
peerDependencies: {
|
|
298
|
+
"@radix-ui/themes": ">=3.0.0",
|
|
299
|
+
"@tanstack/react-query": ">=5.0.0",
|
|
300
|
+
"@workos-inc/authkit-react": ">=0.5.0",
|
|
301
|
+
"@workos-inc/widgets": ">=1.0.0",
|
|
302
|
+
hono: ">=4.0.0",
|
|
303
|
+
jose: ">=5.0.0",
|
|
304
|
+
react: ">=18.0.0",
|
|
305
|
+
"react-dom": ">=18.0.0"
|
|
306
|
+
},
|
|
307
|
+
peerDependenciesMeta: {
|
|
308
|
+
"@radix-ui/themes": {
|
|
309
|
+
optional: true
|
|
310
|
+
},
|
|
311
|
+
"@tanstack/react-query": {
|
|
312
|
+
optional: true
|
|
313
|
+
},
|
|
314
|
+
"@workos-inc/authkit-react": {
|
|
315
|
+
optional: true
|
|
316
|
+
},
|
|
317
|
+
"@workos-inc/widgets": {
|
|
318
|
+
optional: true
|
|
319
|
+
},
|
|
320
|
+
hono: {
|
|
321
|
+
optional: true
|
|
322
|
+
},
|
|
323
|
+
jose: {
|
|
324
|
+
optional: true
|
|
325
|
+
},
|
|
326
|
+
react: {
|
|
327
|
+
optional: true
|
|
328
|
+
},
|
|
329
|
+
"react-dom": {
|
|
330
|
+
optional: true
|
|
331
|
+
}
|
|
332
|
+
},
|
|
479
333
|
devDependencies: {
|
|
334
|
+
"@radix-ui/themes": "^3.0.0",
|
|
335
|
+
"@tanstack/react-query": "^5.0.0",
|
|
336
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
337
|
+
"@testing-library/react": "^16.3.2",
|
|
480
338
|
"@types/node": "^24.10.1",
|
|
339
|
+
"@types/react": "^18.2.0",
|
|
340
|
+
"@types/react-dom": "^18.2.0",
|
|
341
|
+
"@workos-inc/authkit-react": "^0.16.0",
|
|
342
|
+
"@workos-inc/widgets": "^1.0.0",
|
|
343
|
+
hono: "^4.0.0",
|
|
344
|
+
jose: "^5.0.0",
|
|
345
|
+
jsdom: "^27.4.0",
|
|
346
|
+
react: "^18.2.0",
|
|
347
|
+
"react-dom": "^18.2.0",
|
|
481
348
|
tsup: "^8.0.0",
|
|
482
349
|
typescript: "^5.5.2",
|
|
483
350
|
vitest: "^2.1.8"
|
|
@@ -487,11 +354,7 @@ var require_package = __commonJS({
|
|
|
487
354
|
});
|
|
488
355
|
|
|
489
356
|
// src/config.ts
|
|
490
|
-
|
|
491
|
-
if (globalThis[key]) return globalThis[key];
|
|
492
|
-
if (typeof process !== "undefined" && process.env?.[key]) return process.env[key];
|
|
493
|
-
return void 0;
|
|
494
|
-
}
|
|
357
|
+
init_utils();
|
|
495
358
|
var globalConfig = {
|
|
496
359
|
apiUrl: getEnv("OAUTH_API_URL") || getEnv("API_URL") || "https://apis.do",
|
|
497
360
|
clientId: getEnv("OAUTH_CLIENT_ID") || "client_01JQYTRXK9ZPD8JPJTKDCRB656",
|
|
@@ -596,22 +459,37 @@ async function pollForTokens(deviceCode, interval = 5, expiresIn = 600) {
|
|
|
596
459
|
}
|
|
597
460
|
|
|
598
461
|
// src/auth.ts
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
462
|
+
init_utils();
|
|
463
|
+
async function getUser(token) {
|
|
464
|
+
const config = getConfig();
|
|
465
|
+
const authToken = token || getEnv("DO_TOKEN") || "";
|
|
466
|
+
if (!authToken) {
|
|
467
|
+
return { user: null };
|
|
468
|
+
}
|
|
469
|
+
try {
|
|
470
|
+
const response = await config.fetch(`${config.apiUrl}/me`, {
|
|
471
|
+
method: "GET",
|
|
472
|
+
headers: {
|
|
473
|
+
"Authorization": `Bearer ${authToken}`,
|
|
474
|
+
"Content-Type": "application/json"
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
if (!response.ok) {
|
|
478
|
+
if (response.status === 401) {
|
|
479
|
+
return { user: null };
|
|
480
|
+
}
|
|
481
|
+
throw new Error(`Authentication failed: ${response.statusText}`);
|
|
482
|
+
}
|
|
483
|
+
const user = await response.json();
|
|
484
|
+
return { user, token: authToken };
|
|
485
|
+
} catch (error) {
|
|
486
|
+
console.error("Auth error:", error);
|
|
487
|
+
return { user: null };
|
|
604
488
|
}
|
|
605
|
-
return null;
|
|
606
|
-
}
|
|
607
|
-
function getEnv3(key) {
|
|
608
|
-
if (globalThis[key]) return globalThis[key];
|
|
609
|
-
if (typeof process !== "undefined" && process.env?.[key]) return process.env[key];
|
|
610
|
-
return void 0;
|
|
611
489
|
}
|
|
612
490
|
async function logout(token) {
|
|
613
491
|
const config = getConfig();
|
|
614
|
-
const authToken = token ||
|
|
492
|
+
const authToken = token || getEnv("DO_TOKEN") || "";
|
|
615
493
|
if (!authToken) {
|
|
616
494
|
return;
|
|
617
495
|
}
|
|
@@ -630,84 +508,6 @@ async function logout(token) {
|
|
|
630
508
|
console.error("Logout error:", error);
|
|
631
509
|
}
|
|
632
510
|
}
|
|
633
|
-
var REFRESH_BUFFER_MS = 5 * 60 * 1e3;
|
|
634
|
-
function isTokenExpired(expiresAt) {
|
|
635
|
-
if (!expiresAt) return false;
|
|
636
|
-
return Date.now() >= expiresAt - REFRESH_BUFFER_MS;
|
|
637
|
-
}
|
|
638
|
-
async function getToken() {
|
|
639
|
-
const adminToken = getEnv3("DO_ADMIN_TOKEN");
|
|
640
|
-
if (adminToken) return adminToken;
|
|
641
|
-
const doToken = getEnv3("DO_TOKEN");
|
|
642
|
-
if (doToken) return doToken;
|
|
643
|
-
try {
|
|
644
|
-
const { env } = await import('cloudflare:workers');
|
|
645
|
-
const cfAdminToken = await resolveSecret(env.DO_ADMIN_TOKEN);
|
|
646
|
-
if (cfAdminToken) return cfAdminToken;
|
|
647
|
-
const cfToken = await resolveSecret(env.DO_TOKEN);
|
|
648
|
-
if (cfToken) return cfToken;
|
|
649
|
-
} catch {
|
|
650
|
-
}
|
|
651
|
-
try {
|
|
652
|
-
const { createSecureStorage: createSecureStorage2 } = await Promise.resolve().then(() => (init_storage(), storage_exports));
|
|
653
|
-
const config = getConfig();
|
|
654
|
-
const storage2 = createSecureStorage2(config.storagePath);
|
|
655
|
-
const tokenData = storage2.getTokenData ? await storage2.getTokenData() : null;
|
|
656
|
-
if (tokenData) {
|
|
657
|
-
if (!isTokenExpired(tokenData.expiresAt)) {
|
|
658
|
-
return tokenData.accessToken;
|
|
659
|
-
}
|
|
660
|
-
if (tokenData.refreshToken) {
|
|
661
|
-
try {
|
|
662
|
-
const newTokens = await refreshAccessToken(tokenData.refreshToken);
|
|
663
|
-
const expiresAt = newTokens.expires_in ? Date.now() + newTokens.expires_in * 1e3 : void 0;
|
|
664
|
-
const newData = {
|
|
665
|
-
accessToken: newTokens.access_token,
|
|
666
|
-
refreshToken: newTokens.refresh_token || tokenData.refreshToken,
|
|
667
|
-
expiresAt
|
|
668
|
-
};
|
|
669
|
-
if (storage2.setTokenData) {
|
|
670
|
-
await storage2.setTokenData(newData);
|
|
671
|
-
} else {
|
|
672
|
-
await storage2.setToken(newTokens.access_token);
|
|
673
|
-
}
|
|
674
|
-
return newTokens.access_token;
|
|
675
|
-
} catch {
|
|
676
|
-
return null;
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
return null;
|
|
680
|
-
}
|
|
681
|
-
return await storage2.getToken();
|
|
682
|
-
} catch {
|
|
683
|
-
return null;
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
function auth() {
|
|
687
|
-
return getToken;
|
|
688
|
-
}
|
|
689
|
-
async function refreshAccessToken(refreshToken) {
|
|
690
|
-
const config = getConfig();
|
|
691
|
-
if (!config.clientId) {
|
|
692
|
-
throw new Error("Client ID is required for token refresh");
|
|
693
|
-
}
|
|
694
|
-
const response = await config.fetch("https://auth.apis.do/user_management/authenticate", {
|
|
695
|
-
method: "POST",
|
|
696
|
-
headers: {
|
|
697
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
698
|
-
},
|
|
699
|
-
body: new URLSearchParams({
|
|
700
|
-
grant_type: "refresh_token",
|
|
701
|
-
refresh_token: refreshToken,
|
|
702
|
-
client_id: config.clientId
|
|
703
|
-
}).toString()
|
|
704
|
-
});
|
|
705
|
-
if (!response.ok) {
|
|
706
|
-
const errorText = await response.text();
|
|
707
|
-
throw new Error(`Token refresh failed: ${response.status} - ${errorText}`);
|
|
708
|
-
}
|
|
709
|
-
return await response.json();
|
|
710
|
-
}
|
|
711
511
|
|
|
712
512
|
// src/cli.ts
|
|
713
513
|
init_storage();
|
|
@@ -830,7 +630,7 @@ ${colors.dim}Waiting for authorization...${colors.reset}
|
|
|
830
630
|
authResponse.expires_in
|
|
831
631
|
);
|
|
832
632
|
await storage.setToken(tokenResponse.access_token);
|
|
833
|
-
const authResult = await
|
|
633
|
+
const authResult = await getUser(tokenResponse.access_token);
|
|
834
634
|
printSuccess("Login successful!");
|
|
835
635
|
if (authResult.user) {
|
|
836
636
|
console.log(`
|
|
@@ -877,7 +677,7 @@ async function whoamiCommand() {
|
|
|
877
677
|
Run ${colors.cyan}oauth.do login${colors.reset} to authenticate`);
|
|
878
678
|
return;
|
|
879
679
|
}
|
|
880
|
-
const authResult = await
|
|
680
|
+
const authResult = await getUser(token);
|
|
881
681
|
if (!authResult.user) {
|
|
882
682
|
console.log(`${colors.dim}Not authenticated${colors.reset}`);
|
|
883
683
|
console.log(`
|
|
@@ -932,7 +732,7 @@ ${colors.cyan}Auth:${colors.reset} ${colors.dim}Not authenticated${colors.reset}
|
|
|
932
732
|
Run ${colors.cyan}oauth.do login${colors.reset} to authenticate`);
|
|
933
733
|
return;
|
|
934
734
|
}
|
|
935
|
-
const authResult = await
|
|
735
|
+
const authResult = await getUser(token);
|
|
936
736
|
if (authResult.user) {
|
|
937
737
|
console.log(`
|
|
938
738
|
${colors.cyan}Auth:${colors.reset} ${colors.green}Authenticated${colors.reset}`);
|
|
@@ -954,7 +754,7 @@ async function autoLoginOrShowUser() {
|
|
|
954
754
|
try {
|
|
955
755
|
const token = await storage.getToken();
|
|
956
756
|
if (token) {
|
|
957
|
-
const authResult = await
|
|
757
|
+
const authResult = await getUser(token);
|
|
958
758
|
if (authResult.user) {
|
|
959
759
|
console.log(`${colors.green}\u2713${colors.reset} Already authenticated
|
|
960
760
|
`);
|