|
|
@@ -0,0 +1,27 @@
|
|
|
+import JSEncrypt from 'jsencrypt'
|
|
|
+import { get } from './request.js'
|
|
|
+
|
|
|
+/**
|
|
|
+ * 对齐 web-admin `buildWebEncryptedLoginBody`:
|
|
|
+ * 拉公钥后加密 { username, password, ts }。
|
|
|
+ * @returns {Promise<{ encryptedLogin: string }>}
|
|
|
+ */
|
|
|
+export async function buildEncryptedLoginBody(username, password) {
|
|
|
+ const data = await get('/web/auth/login-rsa-public-key')
|
|
|
+ const publicKeyPem = data && data.publicKeyPem
|
|
|
+ if (!publicKeyPem) {
|
|
|
+ throw new Error('获取登录公钥失败')
|
|
|
+ }
|
|
|
+ const crypt = new JSEncrypt()
|
|
|
+ crypt.setPublicKey(publicKeyPem)
|
|
|
+ const payload = JSON.stringify({
|
|
|
+ username,
|
|
|
+ password,
|
|
|
+ ts: Date.now()
|
|
|
+ })
|
|
|
+ const encryptedLogin = crypt.encrypt(payload)
|
|
|
+ if (!encryptedLogin) {
|
|
|
+ throw new Error('登录加密失败,请重试')
|
|
|
+ }
|
|
|
+ return { encryptedLogin }
|
|
|
+}
|