|
|
@@ -2,9 +2,16 @@ import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'
|
|
2
|
2
|
import { getScreenPublicKey, screenAutoLogin } from '@/api/login'
|
|
3
|
3
|
import { getToken, setToken } from '@/utils/auth'
|
|
4
|
4
|
|
|
|
5
|
+/** URL 密文登录失败时展示给用户的提示 */
|
|
|
6
|
+export const SCREEN_LOGIN_CIPHER_ERROR = '公钥不对'
|
|
|
7
|
+
|
|
|
8
|
+/** URL uuid 免密登录失败时展示给用户的提示 */
|
|
|
9
|
+export const SCREEN_LOGIN_UUID_ERROR = '免密失败,请联系管理员获取免登ID'
|
|
|
10
|
+
|
|
5
|
11
|
/** 同一会话内仅尝试一次免密登录,避免反复请求 */
|
|
6
|
12
|
let autoLoginState = null
|
|
7
|
13
|
let autoLoginPromise = null
|
|
|
14
|
+let autoLoginUuid = null
|
|
8
|
15
|
|
|
9
|
16
|
export function isScreenAutoLoginEnabled() {
|
|
10
|
17
|
return import.meta.env.VITE_SCREEN_AUTO_LOGIN === 'true'
|
|
|
@@ -13,58 +20,136 @@ export function isScreenAutoLoginEnabled() {
|
|
13
|
20
|
export function resetScreenAutoLoginState() {
|
|
14
|
21
|
autoLoginState = null
|
|
15
|
22
|
autoLoginPromise = null
|
|
|
23
|
+ autoLoginUuid = null
|
|
|
24
|
+}
|
|
|
25
|
+
|
|
|
26
|
+/** 从路由 query 读取 RSA 密文(?token=xxxx) */
|
|
|
27
|
+export function getUrlScreenLoginCipher(query) {
|
|
|
28
|
+ const raw = query?.token
|
|
|
29
|
+ if (typeof raw !== 'string' || !raw.trim()) {
|
|
|
30
|
+ return ''
|
|
|
31
|
+ }
|
|
|
32
|
+ try {
|
|
|
33
|
+ return decodeURIComponent(raw.trim())
|
|
|
34
|
+ } catch {
|
|
|
35
|
+ return raw.trim()
|
|
|
36
|
+ }
|
|
|
37
|
+}
|
|
|
38
|
+
|
|
|
39
|
+/** 从路由 query 读取免登 UUID(?uuid=xxxx) */
|
|
|
40
|
+export function getUrlScreenLoginUuid(query) {
|
|
|
41
|
+ const raw = query?.uuid
|
|
|
42
|
+ if (typeof raw !== 'string' || !raw.trim()) {
|
|
|
43
|
+ return ''
|
|
|
44
|
+ }
|
|
|
45
|
+ try {
|
|
|
46
|
+ return decodeURIComponent(raw.trim())
|
|
|
47
|
+ } catch {
|
|
|
48
|
+ return raw.trim()
|
|
|
49
|
+ }
|
|
|
50
|
+}
|
|
|
51
|
+
|
|
|
52
|
+/** 去掉 URL 中的 token,避免密文残留在地址栏 */
|
|
|
53
|
+export function stripScreenLoginTokenQuery(query = {}) {
|
|
|
54
|
+ const next = { ...query }
|
|
|
55
|
+ delete next.token
|
|
|
56
|
+ return next
|
|
|
57
|
+}
|
|
|
58
|
+
|
|
|
59
|
+/** 去掉 URL 中的 uuid */
|
|
|
60
|
+export function stripScreenLoginUuidQuery(query = {}) {
|
|
|
61
|
+ const next = { ...query }
|
|
|
62
|
+ delete next.uuid
|
|
|
63
|
+ return next
|
|
16
|
64
|
}
|
|
17
|
65
|
|
|
18
|
66
|
/**
|
|
19
|
|
- * RSA 加密 UUID 换取 JWT(见 doc/大屏/免密登录/大屏免密登录技术方案.md)
|
|
|
67
|
+ * 使用 URL 传入的 RSA 密文换取 JWT
|
|
|
68
|
+ * @param {string} cipher
|
|
20
|
69
|
* @returns {Promise<string>} JWT
|
|
21
|
70
|
*/
|
|
22
|
|
-export async function tryScreenAutoLogin() {
|
|
23
|
|
- const keyRes = await getScreenPublicKey()
|
|
24
|
|
- const publicKey = keyRes.data?.publicKey
|
|
25
|
|
- if (!publicKey) {
|
|
26
|
|
- throw new Error('未获取到 RSA 公钥')
|
|
|
71
|
+export async function tryScreenLoginWithCipher(cipher) {
|
|
|
72
|
+ if (!cipher) {
|
|
|
73
|
+ throw new Error(SCREEN_LOGIN_CIPHER_ERROR)
|
|
|
74
|
+ }
|
|
|
75
|
+ try {
|
|
|
76
|
+ const loginRes = await screenAutoLogin(cipher)
|
|
|
77
|
+ if (!loginRes?.token) {
|
|
|
78
|
+ throw new Error(SCREEN_LOGIN_CIPHER_ERROR)
|
|
|
79
|
+ }
|
|
|
80
|
+ setToken(loginRes.token)
|
|
|
81
|
+ autoLoginState = 'ok'
|
|
|
82
|
+ return loginRes.token
|
|
|
83
|
+ } catch {
|
|
|
84
|
+ throw new Error(SCREEN_LOGIN_CIPHER_ERROR)
|
|
27
|
85
|
}
|
|
|
86
|
+}
|
|
28
|
87
|
|
|
29
|
|
- const uuid = crypto.randomUUID()
|
|
30
|
|
- const encryptor = new JSEncrypt()
|
|
31
|
|
- encryptor.setPublicKey(publicKey)
|
|
32
|
|
- const cipher = encryptor.encrypt(uuid)
|
|
33
|
|
- if (!cipher) {
|
|
34
|
|
- throw new Error('RSA 加密失败')
|
|
|
88
|
+/**
|
|
|
89
|
+ * 获取公钥 → RSA 加密 URL 中的 UUID → 换取 JWT
|
|
|
90
|
+ * @param {string} uuid
|
|
|
91
|
+ * @returns {Promise<string>} JWT
|
|
|
92
|
+ */
|
|
|
93
|
+export async function tryScreenAutoLogin(uuid) {
|
|
|
94
|
+ if (!uuid) {
|
|
|
95
|
+ throw new Error(SCREEN_LOGIN_UUID_ERROR)
|
|
35
|
96
|
}
|
|
|
97
|
+ try {
|
|
|
98
|
+ const keyRes = await getScreenPublicKey()
|
|
|
99
|
+ const publicKey = keyRes.data?.publicKey
|
|
|
100
|
+ if (!publicKey) {
|
|
|
101
|
+ throw new Error(SCREEN_LOGIN_UUID_ERROR)
|
|
|
102
|
+ }
|
|
|
103
|
+
|
|
|
104
|
+ const encryptor = new JSEncrypt()
|
|
|
105
|
+ encryptor.setPublicKey(publicKey)
|
|
|
106
|
+ const cipher = encryptor.encrypt(uuid)
|
|
|
107
|
+ if (!cipher) {
|
|
|
108
|
+ throw new Error(SCREEN_LOGIN_UUID_ERROR)
|
|
|
109
|
+ }
|
|
36
|
110
|
|
|
37
|
|
- const loginRes = await screenAutoLogin(cipher)
|
|
38
|
|
- if (!loginRes.token) {
|
|
39
|
|
- throw new Error('免密登录未返回 token')
|
|
|
111
|
+ const loginRes = await screenAutoLogin(cipher)
|
|
|
112
|
+ if (!loginRes?.token) {
|
|
|
113
|
+ throw new Error(SCREEN_LOGIN_UUID_ERROR)
|
|
|
114
|
+ }
|
|
|
115
|
+ setToken(loginRes.token)
|
|
|
116
|
+ return loginRes.token
|
|
|
117
|
+ } catch {
|
|
|
118
|
+ throw new Error(SCREEN_LOGIN_UUID_ERROR)
|
|
40
|
119
|
}
|
|
41
|
|
- setToken(loginRes.token)
|
|
42
|
|
- return loginRes.token
|
|
43
|
120
|
}
|
|
44
|
121
|
|
|
45
|
122
|
/**
|
|
46
|
|
- * 路由守卫用:已登录直接通过;未启用免密则 false;否则尝试一次免密登录
|
|
47
|
|
- * @returns {Promise<boolean>}
|
|
|
123
|
+ * 路由守卫用:URL 含 uuid 时尝试免密登录
|
|
|
124
|
+ * @returns {Promise<{ attempted: boolean, ok: boolean }>}
|
|
48
|
125
|
*/
|
|
49
|
|
-export async function ensureScreenAutoLogin() {
|
|
|
126
|
+export async function ensureScreenAutoLogin(query) {
|
|
50
|
127
|
if (getToken()) {
|
|
51
|
|
- return true
|
|
|
128
|
+ return { attempted: false, ok: true }
|
|
52
|
129
|
}
|
|
53
|
130
|
if (!isScreenAutoLoginEnabled()) {
|
|
54
|
|
- return false
|
|
|
131
|
+ return { attempted: false, ok: false }
|
|
55
|
132
|
}
|
|
56
|
|
- if (autoLoginState === 'fail') {
|
|
57
|
|
- return false
|
|
|
133
|
+
|
|
|
134
|
+ const uuid = getUrlScreenLoginUuid(query)
|
|
|
135
|
+ if (!uuid) {
|
|
|
136
|
+ return { attempted: false, ok: false }
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ if (autoLoginState === 'fail' && autoLoginUuid === uuid) {
|
|
|
140
|
+ return { attempted: true, ok: false }
|
|
58
|
141
|
}
|
|
59
|
|
- if (autoLoginState === 'ok') {
|
|
60
|
|
- return !!getToken()
|
|
|
142
|
+ if (autoLoginState === 'ok' && getToken()) {
|
|
|
143
|
+ return { attempted: false, ok: true }
|
|
61
|
144
|
}
|
|
62
|
|
- if (autoLoginState === 'pending' && autoLoginPromise) {
|
|
63
|
|
- return autoLoginPromise
|
|
|
145
|
+ if (autoLoginState === 'pending' && autoLoginPromise && autoLoginUuid === uuid) {
|
|
|
146
|
+ const ok = await autoLoginPromise
|
|
|
147
|
+ return { attempted: true, ok }
|
|
64
|
148
|
}
|
|
65
|
149
|
|
|
|
150
|
+ autoLoginUuid = uuid
|
|
66
|
151
|
autoLoginState = 'pending'
|
|
67
|
|
- autoLoginPromise = tryScreenAutoLogin()
|
|
|
152
|
+ autoLoginPromise = tryScreenAutoLogin(uuid)
|
|
68
|
153
|
.then(() => {
|
|
69
|
154
|
autoLoginState = 'ok'
|
|
70
|
155
|
return true
|
|
|
@@ -74,5 +159,6 @@ export async function ensureScreenAutoLogin() {
|
|
74
|
159
|
return false
|
|
75
|
160
|
})
|
|
76
|
161
|
|
|
77
|
|
- return autoLoginPromise
|
|
|
162
|
+ const ok = await autoLoginPromise
|
|
|
163
|
+ return { attempted: true, ok }
|
|
78
|
164
|
}
|