2025-06-12 00:13:57 +08:00
|
|
|
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'
|
2025-06-12 13:01:25 +08:00
|
|
|
import * as local from "hono/cookie";
|
2025-06-12 00:13:57 +08:00
|
|
|
|
|
|
|
export type Bindings = {
|
2025-06-12 13:01:25 +08:00
|
|
|
MAIN_URLS: string
|
2025-06-12 00:13:57 +08:00
|
|
|
}
|
|
|
|
const app = new Hono<{ Bindings: Bindings }>()
|
|
|
|
app.use("*", serveStatic({manifest: manifest, root: "./"}));
|
2025-06-12 13:01:25 +08:00
|
|
|
|
|
|
|
// 登录申请 ##############################################################################
|
2025-06-12 00:13:57 +08:00
|
|
|
app.get('/onedrive/requests', async (c) => {
|
|
|
|
const client_uid = <string>c.req.query('client_uid');
|
|
|
|
const client_key = <string>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';
|
2025-06-12 13:01:25 +08:00
|
|
|
// 请求参数 ==========================================================================
|
2025-06-12 00:13:57 +08:00
|
|
|
const params_all: Record<string, any> = {
|
|
|
|
client_id: client_uid,
|
|
|
|
scope: scopes_all,
|
|
|
|
response_type: 'code',
|
|
|
|
redirect_uri: 'https://' + c.env.MAIN_URLS + '/onedrive/callback'
|
|
|
|
};
|
|
|
|
const urlWithParams = new URL(client_url);
|
|
|
|
Object.keys(params_all).forEach(key => {
|
|
|
|
urlWithParams.searchParams.append(key, params_all[key]);
|
|
|
|
});
|
2025-06-12 13:01:25 +08:00
|
|
|
// 执行请求 ===========================================================================
|
2025-06-12 00:13:57 +08:00
|
|
|
try {
|
|
|
|
const response = await fetch(urlWithParams.href, {
|
|
|
|
method: 'GET',
|
|
|
|
});
|
2025-06-12 13:01:25 +08:00
|
|
|
local.setCookie(c, 'client_uid', client_uid);
|
|
|
|
local.setCookie(c, 'client_key', client_key);
|
2025-06-12 00:13:57 +08:00
|
|
|
return c.json({text: response.url}, 200);
|
|
|
|
} catch (error) {
|
|
|
|
return c.json({text: error}, 500);
|
|
|
|
}
|
|
|
|
})
|
2025-06-12 13:01:25 +08:00
|
|
|
// 令牌申请 ##############################################################################
|
|
|
|
app.get('/onedrive/callback', async (c) => {
|
2025-06-12 00:13:57 +08:00
|
|
|
const login_data = <string>c.req.query('code');
|
2025-06-12 13:01:25 +08:00
|
|
|
const client_uid: string | undefined = local.getCookie(c, 'client_uid')
|
|
|
|
const client_key: string | undefined = local.getCookie(c, 'client_key')
|
2025-06-12 00:13:57 +08:00
|
|
|
const client_url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
|
2025-06-12 13:01:25 +08:00
|
|
|
console.log(login_data);
|
|
|
|
// 请求参数 ==========================================================================
|
2025-06-12 00:13:57 +08:00
|
|
|
const params_all = {
|
|
|
|
client_id: client_uid,
|
|
|
|
client_secret: client_key,
|
|
|
|
redirect_uri: 'https://' + c.env.MAIN_URLS + '/onedrive/callback',
|
|
|
|
code: login_data,
|
|
|
|
grant_type: 'authorization_code'
|
|
|
|
};
|
2025-06-12 13:01:25 +08:00
|
|
|
// 执行请求 ===========================================================================
|
2025-06-12 00:13:57 +08:00
|
|
|
try {
|
2025-06-12 13:01:25 +08:00
|
|
|
const paramsString = new URLSearchParams(params_all).toString();
|
|
|
|
const response: Response = await fetch(client_url, {
|
2025-06-12 00:13:57 +08:00
|
|
|
method: 'POST',
|
2025-06-12 13:01:25 +08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
|
|
|
body: paramsString,
|
2025-06-12 00:13:57 +08:00
|
|
|
});
|
2025-06-12 13:01:25 +08:00
|
|
|
console.log(response);
|
|
|
|
if (!response.ok)
|
|
|
|
return c.json({text: response.text()}, 403);
|
|
|
|
const json: Record<string, any> = await response.json();
|
2025-06-12 00:13:57 +08:00
|
|
|
if (json.token_type === 'Bearer') {
|
2025-06-12 13:01:25 +08:00
|
|
|
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,
|
|
|
|
// });
|
2025-06-12 00:13:57 +08:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2025-06-12 13:01:25 +08:00
|
|
|
console.error(error);
|
|
|
|
return c.json({text: error}, 500);
|
2025-06-12 00:13:57 +08:00
|
|
|
}
|
2025-06-12 13:01:25 +08:00
|
|
|
})
|
|
|
|
|
2025-06-12 00:13:57 +08:00
|
|
|
|
|
|
|
export default app
|