From b1df0931277fb20adc7620881f1dfe3c216ddf9a Mon Sep 17 00:00:00 2001 From: 7086cmd <1371077054@qq.com> Date: Wed, 17 Jan 2024 17:09:52 +0800 Subject: [PATCH] feat: add the fake token and _id to fake --- api/user/auth.post.yaml | 8 ++++---- fake/activity.fake.ts | 12 ++++++++++-- fake/auth.fake.ts | 8 ++++---- package.json | 1 + pnpm-lock.yaml | 7 +++++++ src/api/user/auth.ts | 29 +++++++++++++++++++---------- 6 files changed, 45 insertions(+), 20 deletions(-) diff --git a/api/user/auth.post.yaml b/api/user/auth.post.yaml index 2780e6f..b8bfd13 100644 --- a/api/user/auth.post.yaml +++ b/api/user/auth.post.yaml @@ -7,16 +7,16 @@ params: type: string required: true description: The user id (ObjectId) - - name: password + - name: credential in: body type: string required: true - description: The user password (after rsa-128) - - name: role + description: The user credential (after json dump with password and timestamp) + - name: mode in: body type: string required: true - description: The user auth role ('short' or 'long') + description: The user auth mode ('short' or 'long') response: - name: token type: string diff --git a/fake/activity.fake.ts b/fake/activity.fake.ts index 21837f9..fc9dc31 100644 --- a/fake/activity.fake.ts +++ b/fake/activity.fake.ts @@ -1,6 +1,8 @@ import { faker } from '@faker-js/faker/locale/zh_CN' import { generateNumber } from './utils' import ObjectID from 'bson-objectid' +import JSEncrypt from 'jsencrypt' +import * as jose from 'jose' function generateActivity(_id?: string, serie: boolean = true) { return { @@ -96,7 +98,11 @@ function createPerson(id?: string) { position: faker.number.int({ min: 0, max: 3 }) === 0 ? ['student', 'secretary', 'department', 'auditor', 'admin'] - : [['teacher', 'secretary', 'department', 'auditor', 'admin'][Math.floor(Math.random() * 5)]], + : [ + ['teacher', 'secretary', 'department', 'auditor', 'admin'][ + Math.floor(Math.random() * 5) + ] + ], code: generateCode() } } @@ -294,7 +300,9 @@ export default [ code: 200, status: 'success', data: { - token: 'test-token' + token: + 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY1NTc3Zjk0MDIzODY5MGExNjdiZWI1ZSIsIm5hbWUiOiJhZG1pbiIsInNleCI6IjIwMjEtMDQtMjZUMjA6MjA6MjYuMjY2WiIsInN1YiI6ImFkbWluIiwiaWF0IjoxNjE4NjY0NjI2LCJleHAiOjE2M', + _id: '65577f940238690a167beb5e' } } } diff --git a/fake/auth.fake.ts b/fake/auth.fake.ts index 63fa494..bc684e7 100644 --- a/fake/auth.fake.ts +++ b/fake/auth.fake.ts @@ -1,8 +1,8 @@ const pubkeyeg = `-----BEGIN PUBLIC KEY----- -MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHcxQEyZbfrWTzOBvahQsg404jJY -d24wexXHj3VZgWyw+m2yzRjclroNLyV1uck10bVJoibn9CFseVtHyN6eOGHGuC50 -QTsbcy4gtxzxNm7YRJpe56X28EgR9vX5gawEq8GAPVOyodBBSqC7YRzOoC7t9r9Q -Pkt0nCrMJON9b+xDAgMBAAE= +MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHjW3q+L5hJ8Vw5cIJJG2G801GPw +D38oKME7Cb3PG8SlL+WfaWVotHC8j3i9AG6m9L47DQ9m6cxsG3LRoFExVCIsEWEc +E7VteO34n5BqFjujOb+thHUh+6yZETUTqbIX68g55Tt2A+qpQ9CprqvwTO6oZKux +f3lGEiRtBn7H37ShAgMBAAE= -----END PUBLIC KEY-----` export default [ { diff --git a/package.json b/package.json index 073d3b4..dece10a 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "eslint-plugin-vue": "^9.17.0", "git-format-staged": "^3.0.0", "husky": "^8.0.3", + "jose": "^5.2.0", "js-md5": "^0.8.3", "js-yaml": "^4.1.0", "jsdom": "^22.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5fbd841..2cd4b32 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -142,6 +142,9 @@ devDependencies: husky: specifier: ^8.0.3 version: 8.0.3 + jose: + specifier: ^5.2.0 + version: 5.2.0 js-md5: specifier: ^0.8.3 version: 0.8.3 @@ -6834,6 +6837,10 @@ packages: '@sideway/pinpoint': 2.0.0 dev: true + /jose@5.2.0: + resolution: {integrity: sha512-oW3PCnvyrcm1HMvGTzqjxxfnEs9EoFOFWi2HsEGhlFVOXxTE3K9GKWVMFoFw06yPUqwpvEWic1BmtUZBI/tIjw==} + dev: true + /js-beautify@1.14.9: resolution: {integrity: sha512-coM7xq1syLcMyuVGyToxcj2AlzhkDjmfklL8r0JgJ7A76wyGMpJ1oA35mr4APdYNO/o/4YY8H54NQIJzhMbhBg==} engines: {node: '>=12'} diff --git a/src/api/user/auth.ts b/src/api/user/auth.ts index 89236f7..5b14bbf 100644 --- a/src/api/user/auth.ts +++ b/src/api/user/auth.ts @@ -3,15 +3,18 @@ import type { Response } from '@/../@types/response' import type { LoginResult } from '@/../@types/login' import { ElNotification } from 'element-plus' import JSEncrypt from 'jsencrypt' +import dayjs from 'dayjs' export async function getRSAPublicCert(): Promise { - const result = (await axios('/cert', { - method: 'GET', - params: { - type: 'public', - method: 'RSA' - } - })) as Response + const result = ( + await axios('/cert', { + method: 'GET', + params: { + type: 'public', + method: 'RSA' + } + }) + ).data as Response if (result.status === 'error') { ElNotification({ title: '获取公钥错误(' + result.code + ')', @@ -28,13 +31,19 @@ export async function getRSAPublicCert(): Promise { async function UserLogin(user: string, password: string, term: 'long' | 'short' = 'long') { const encrypt = await getRSAPublicCert() if (!encrypt) return - const password_encrypted = encrypt?.encrypt(password) + const credentials = { + password: password, + timestamp: dayjs().unix() + } + console.log(encrypt) + const credential = encrypt.encrypt(JSON.stringify(credentials)) + console.log(`User ${user} login with ${term} term, with the credential ${credential}`) const result = (await axios('/user/auth', { method: 'POST', data: { userident: user.toString(), - password: password_encrypted, - role: term + password: credential, + mode: term } })) as Response if (result.status === 'error') {