From 9e6b42032672a0ce5f94ca8a7ff2a0f687319f88 Mon Sep 17 00:00:00 2001 From: pikachuim Date: Thu, 12 Jun 2025 13:01:25 +0800 Subject: [PATCH] fin onedrive official token --- .gitignore | 11 +++ .idea/cf-worker-api.iml | 12 +++ .idea/modules.xml | 2 +- .idea/vcs.xml | 6 ++ .idea/workspace.xml | 83 +++++++++++++++++++++ public/index.html | 95 ++++++++++++++++-------- public/static/style-all.css | 2 +- src/index.ts | 72 +++++++++--------- wrangler.jsonc.example => wrangler.jsonc | 8 +- 9 files changed, 215 insertions(+), 76 deletions(-) create mode 100644 .gitignore create mode 100644 .idea/cf-worker-api.iml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml rename wrangler.jsonc.example => wrangler.jsonc (64%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..32b877b --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +/tmp +/out-tsc + +/node_modules +npm-debug.log* +yarn-debug.log* +yarn-error.log* +/.pnp +.pnp.js + +.vscode/* \ No newline at end of file diff --git a/.idea/cf-worker-api.iml b/.idea/cf-worker-api.iml new file mode 100644 index 0000000..24643cc --- /dev/null +++ b/.idea/cf-worker-api.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index eeb95fb..526ae56 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..f9f2cce --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + { + "associatedIndex": 4 +} + + + + + + + + + + + + + + + + + + + 1749696640494 + + + + + + + + + \ No newline at end of file diff --git a/public/index.html b/public/index.html index 0cc5381..e007b31 100644 --- a/public/index.html +++ b/public/index.html @@ -3,7 +3,7 @@ - AList Token获取工具 + OpenList Token获取工具 @@ -14,10 +14,10 @@
-

AList Token获取工具

+

OpenList Token获取工具

- +
- +
- - + +
@@ -45,13 +45,13 @@
- - + +
- +
@@ -60,11 +60,22 @@ \ No newline at end of file diff --git a/public/static/style-all.css b/public/static/style-all.css index 2c33b9b..94bcd3e 100644 --- a/public/static/style-all.css +++ b/public/static/style-all.css @@ -49,7 +49,7 @@ h1 { color: #555; } -.input-group input, .input-group select { +.input-group input, .input-group select,.input-group textarea{ width: 100%; padding: 10px; border: 1px solid #ddd; diff --git a/src/index.ts b/src/index.ts index 1ceffe0..073c099 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,18 +2,21 @@ import {Context, Hono} from 'hono' import {KVNamespace} from '@cloudflare/workers-types'; import {serveStatic} from 'hono/cloudflare-workers' // @ts-ignore import manifest from '__STATIC_CONTENT_MANIFEST' +import * as local from "hono/cookie"; export type Bindings = { - MAIN_URLS: string, DATABASE: KVNamespace, + MAIN_URLS: string } const app = new Hono<{ Bindings: Bindings }>() app.use("*", serveStatic({manifest: manifest, root: "./"})); + +// 登录申请 ############################################################################## app.get('/onedrive/requests', async (c) => { const client_uid = c.req.query('client_uid'); const client_key = c.req.query('client_key'); const scopes_all = 'offline_access Files.ReadWrite.All'; const client_url = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'; - + // 请求参数 ========================================================================== const params_all: Record = { client_id: client_uid, scope: scopes_all, @@ -24,37 +27,26 @@ app.get('/onedrive/requests', async (c) => { Object.keys(params_all).forEach(key => { urlWithParams.searchParams.append(key, params_all[key]); }); + // 执行请求 =========================================================================== try { const response = await fetch(urlWithParams.href, { method: 'GET', }); - console.log(response); - await c.env.DATABASE.put(client_uid, JSON.stringify({ - "keys": client_key, - "data": "", - })) + local.setCookie(c, 'client_uid', client_uid); + local.setCookie(c, 'client_key', client_key); return c.json({text: response.url}, 200); } catch (error) { return c.json({text: error}, 500); } }) - -app.get('/onedrive/callback/', async (c) => { +// 令牌申请 ############################################################################## +app.get('/onedrive/callback', async (c) => { const login_data = c.req.query('code'); - const client_uid = c.req.query('uuid'); - const client_key = c.req.query('keys'); - console.log(login_data); - let data: string = await c.env.DATABASE.get(client_uid) - // getToken(c,) -}) - -app.get('/onedrive/gettoken/', (c) => { - const client_uid = c.req.query('uuid'); - -}) - -async function getToken(c: Context, client_uid: string, client_key: string, login_data: string) { + const client_uid: string | undefined = local.getCookie(c, 'client_uid') + const client_key: string | undefined = local.getCookie(c, 'client_key') const client_url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'; + console.log(login_data); + // 请求参数 ========================================================================== const params_all = { client_id: client_uid, client_secret: client_key, @@ -62,24 +54,36 @@ async function getToken(c: Context, client_uid: string, client_key: string, logi code: login_data, grant_type: 'authorization_code' }; - + // 执行请求 =========================================================================== try { - const response = await fetch(client_url, { + const paramsString = new URLSearchParams(params_all).toString(); + const response: Response = await fetch(client_url, { method: 'POST', - body: params_all, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: paramsString, }); - const json = JSON.parse(response.body); + console.log(response); + if (!response.ok) + return c.json({text: response.text()}, 403); + const json: Record = await response.json(); if (json.token_type === 'Bearer') { - return c.json({ - access_token: json.access_token, - refresh_token: json.refresh_token ? json.refresh_token : 'Error', - }); - } else { - return c.json({error: 'Invalid token type'}); + return c.redirect( + `/?access_token=${json.access_token}` + + `&refresh_token=${json.refresh_token}` + + `&client_uid=${client_uid}` + + `&client_key=${client_key}`); + // return c.json({ + // access_token: json.access_token, + // refresh_token: json.refresh_token, + // }); } } catch (error) { - return c.json({error: error}); + console.error(error); + return c.json({text: error}, 500); } -} +}) + export default app diff --git a/wrangler.jsonc.example b/wrangler.jsonc similarity index 64% rename from wrangler.jsonc.example rename to wrangler.jsonc index ef86850..243ac9a 100644 --- a/wrangler.jsonc.example +++ b/wrangler.jsonc @@ -7,15 +7,9 @@ "nodejs_compat" ], "vars": { - "MAIN_URLS": "example.com" + "MAIN_URLS": "api.oplist.org" }, "site": { "bucket": "./public" }, - "kv_namespaces": [ - { - "binding": "DATABASE", - "id": "*******************************" - } - ] }