Skip to content

Commit 49b83b3

Browse files
authored
fix: qianfan detection (#180)
1 parent 7019d44 commit 49b83b3

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
1+
import { convertAvatarToBase64 } from '../utils.js'
2+
13
/**
24
* Baidu Qianfan platform detection logic
35
* Strategy:
4-
* 1. Call current user API directly
6+
* 1. Read csrftoken from bce-user-info-ct-id cookie
7+
* 2. Call current user API with csrftoken header
58
*/
69
export async function detectQianfanUser() {
710
try {
11+
console.log('[COSE] Qianfan Detection: Starting')
12+
13+
// Read csrftoken from cookie
14+
const csrfCookie = await chrome.cookies.get({ url: 'https://qianfan.cloud.baidu.com', name: 'bce-user-info-ct-id' })
15+
const csrfToken = csrfCookie?.value ? csrfCookie.value.replace(/"/g, '') : ''
16+
817
const response = await fetch('https://qianfan.cloud.baidu.com/api/community/user/current', {
918
method: 'GET',
1019
credentials: 'include',
11-
headers: { 'Accept': 'application/json' }
20+
headers: {
21+
'Accept': 'application/json',
22+
...(csrfToken ? { 'csrftoken': csrfToken } : {}),
23+
}
1224
})
1325
if (!response.ok) return { loggedIn: false }
1426

1527
const data = await response.json()
1628
if (data.success && data.result) {
1729
const username = data.result.displayName || data.result.nickname || ''
18-
const avatar = data.result.avatar || ''
30+
let avatar = data.result.avatar || ''
31+
32+
if (avatar && avatar.includes('bdimg.com')) {
33+
avatar = await convertAvatarToBase64(avatar, 'https://qianfan.cloud.baidu.com/')
34+
}
35+
1936
return { loggedIn: true, username, avatar }
2037
} else {
2138
return { loggedIn: false }
2239
}
23-
} catch (e) { return { loggedIn: false } }
40+
} catch (e) {
41+
console.error('[COSE] Qianfan Detection Error:', e)
42+
return { loggedIn: false, error: e.message }
43+
}
2444
}

0 commit comments

Comments
 (0)