Skip to content

Commit b816b7c

Browse files
authored
fix(123cloud_oa): return expires_in for token and renew (#81)
* fix(refresh): support optional expires_in in renew response * fix(123cloud_oa): return expires_in for token and renew * fix(123cloud_oa): scope expires_in handling to 123pan driver
1 parent 4822784 commit b816b7c

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/driver/123cloud_oa.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {pubLogin} from "../shares/oauthv2";
55
import * as configs from "../shares/configs";
66
import {pubParse} from "../shares/urlback";
77
import {encodeCallbackData} from "../shares/secrets";
8-
import {pubRenew} from "../shares/refresh";
98

109

1110
const driver_map: string[] = [
@@ -14,6 +13,14 @@ const driver_map: string[] = [
1413
"https://www.123pan.com/auth"
1514
]
1615

16+
function toQueryString(params: Record<string, any>): string {
17+
return new URLSearchParams(
18+
Object.fromEntries(Object.entries(params)
19+
.filter(([_, v]) => v !== undefined)
20+
.map(([k, v]) => [k, String(v)]))
21+
).toString();
22+
}
23+
1724
// 登录申请 ##############################################################################
1825
export async function oneLogin(c: Context) {
1926
const clients_info: configs.Clients | undefined = configs.getInfo(c);
@@ -55,11 +62,7 @@ export async function oneToken(c: Context) {
5562
refresh_token: refresh_text || undefined
5663
}
5764
console.log(params);
58-
const query_str = new URLSearchParams(
59-
Object.fromEntries(Object.entries(params)
60-
.filter(([_, v]) => v !== undefined)
61-
.map(([k, v]) => [k, String(v)]))
62-
).toString();
65+
const query_str = toQueryString(params);
6366
const result = await pubLogin(
6467
c, "", `${driver_map[1]}?${query_str}`,
6568
false, "POST", "json");
@@ -69,6 +72,7 @@ export async function oneToken(c: Context) {
6972
return c.redirect("/#" + encodeCallbackData({
7073
access_token: result.access_token,
7174
refresh_token: result.refresh_token,
75+
expires_in: result.expires_in,
7276
driver_txt: "123cloud_oa",
7377
server_use: true
7478
}));
@@ -78,14 +82,23 @@ export async function oneToken(c: Context) {
7882
export async function genToken(c: Context) {
7983
const refresh_text: string | undefined = c.req.query('refresh_ui');
8084
if (!refresh_text) return c.json({text: "缺少刷新令牌"}, 500);
81-
const params: Record<string, string> = {
85+
const params: Record<string, any> = {
8286
client_id: c.env.cloud123_uid,
8387
client_secret: c.env.cloud123_key,
8488
grant_type: "refresh_token",
8589
redirect_uri: c.env.cloud123_url,
8690
refresh_token: refresh_text
8791
};
88-
return await pubRenew(
89-
c, driver_map[1], params,
90-
"POST", "access_token", "refresh_token");
92+
const result = await pubLogin(
93+
c, "", `${driver_map[1]}?${toQueryString(params)}`,
94+
false, "POST", "json");
95+
if (!result || !result.access_token)
96+
return c.json({text: result?.error_description || "无法获取AccessToken"}, 500);
97+
const result_data: Record<string, any> = {
98+
refresh_token: result.refresh_token || refresh_text,
99+
access_token: result.access_token,
100+
};
101+
if (result.expires_in !== undefined && result.expires_in !== null && result.expires_in !== "")
102+
result_data.expires_in = result.expires_in;
103+
return c.json(result_data, 200);
91104
}

src/shares/secrets.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export function encodeCallbackData(data: Secrets) {
1010
export interface Secrets {
1111
access_token?: string;
1212
refresh_token?: string;
13+
expires_in?: number;
1314
server_use?: string | boolean;
1415
client_uid?: string;
1516
client_key?: string;

0 commit comments

Comments
 (0)