Skip to content

Commit

Permalink
feat: add the fake token and _id to fake
Browse files Browse the repository at this point in the history
  • Loading branch information
7086cmd committed Jan 17, 2024
1 parent f874727 commit b1df093
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
8 changes: 4 additions & 4 deletions api/user/auth.post.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions fake/activity.fake.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -294,7 +300,9 @@ export default [
code: 200,
status: 'success',
data: {
token: 'test-token'
token:
'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY1NTc3Zjk0MDIzODY5MGExNjdiZWI1ZSIsIm5hbWUiOiJhZG1pbiIsInNleCI6IjIwMjEtMDQtMjZUMjA6MjA6MjYuMjY2WiIsInN1YiI6ImFkbWluIiwiaWF0IjoxNjE4NjY0NjI2LCJleHAiOjE2M',
_id: '65577f940238690a167beb5e'
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions fake/auth.fake.ts
Original file line number Diff line number Diff line change
@@ -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 [
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 19 additions & 10 deletions src/api/user/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSEncrypt | undefined> {
const result = (await axios('/cert', {
method: 'GET',
params: {
type: 'public',
method: 'RSA'
}
})) as Response<string>
const result = (
await axios('/cert', {
method: 'GET',
params: {
type: 'public',
method: 'RSA'
}
})
).data as Response<string>
if (result.status === 'error') {
ElNotification({
title: '获取公钥错误(' + result.code + ')',
Expand All @@ -28,13 +31,19 @@ export async function getRSAPublicCert(): Promise<JSEncrypt | undefined> {
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<LoginResult>
if (result.status === 'error') {
Expand Down

0 comments on commit b1df093

Please sign in to comment.