From 7e29408bdf79e47e7970e6d40e7e2e2417327027 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 6 Jan 2025 10:52:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AF=BC=E5=87=BA=E7=BB=93?= =?UTF-8?q?=E6=9E=84;=20=E5=A2=9E=E5=8A=A0index.d.ts=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.d.ts | 28 ++++------ index.js | 147 ++++++++++++++++++++++++++------------------------- package.json | 2 +- 3 files changed, 86 insertions(+), 91 deletions(-) diff --git a/index.d.ts b/index.d.ts index f1eefe4..2532d13 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,27 +1,21 @@ -import Request from '@gm5/request' -import Response from '@gm5/response' - declare module '@gm5/jwt' { + // interface JwtConfig { + secret: string ttl?: number level?: number - secret: string + } + + interface JwtInstance { + ttl: number + sign(data: object, secret: string): string + verify(token: string, secret: string): object | false } interface JwtModule { - name: string - install(conf?: JwtConfig): { - ttl: number - sign(data: object, secret: string): string - verify(token: string, secret: string): object | false - } + name: 'jwt' + install(conf?: JwtConfig): JwtInstance } - export const JwtModule: JwtModule - - export function createJwt(): ( - req: Request, - res: Response, - next: () => void - ) => void + export function createJwt(): JwtModule } diff --git a/index.js b/index.js index d204795..74de3ae 100644 --- a/index.js +++ b/index.js @@ -18,89 +18,90 @@ function hmac_base64(str, secret) { return base64encode(buf, true) } -export const JwtModule = { - name: 'jwt', - install(conf = {}) { - if (!conf.secret) { - console.warn( - new Error( - 'You must set a `secret` key for jwt, or it will use the default key.' +function JwtMiddleware(req, res, next) { + let { secret, level } = this.get('jwt') + let deviceID = '' + let ssid + + // options请求不处理jwt + if (req.method === 'OPTIONS') { + return next() + } + + // 校验UA + if (level & 2) { + deviceID += req.header('user-agent') + } + + // 校验IP + if (level & 4) { + deviceID += req.ip() + } + + if (deviceID) { + deviceID = sha1(deviceID) + } + + req.__mix_key__ = secret + deviceID + + next() +} + +export function createJwt() { + return { + name: 'jwt', + install(conf = {}) { + if (!conf.secret) { + console.warn( + new Error( + 'You must set a `secret` key for jwt, or it will use the default key.' + ) ) - ) - } + } - let jwt = Object.assign({}, DEFAULT_CONFIG, conf) - this.set({ jwt }) + let jwt = Object.assign({}, DEFAULT_CONFIG, conf) + this.set({ jwt }) + this.use(JwtMiddleware) - return { - ttl: jwt.ttl, - // 签名, 返回token - // header: base64("{"typ":"JWT","alg":"HS256"}") - // 这里固定使用sha256 - sign(data, secret) { - // 加入过期时间, - let payload = { data, expires: Date.now() + this.ttl * 1000 } - let token = '' + return { + ttl: jwt.ttl, + // 签名, 返回token + // header: base64("{"typ":"JWT","alg":"HS256"}") + // 这里固定使用sha256 + sign(data, secret) { + // 加入过期时间, + let payload = { data, expires: Date.now() + this.ttl * 1000 } + let token = '' - payload = base64encode(JSON.stringify(payload), true) - token = hmac_base64(`${HS256_HEADER}.${payload}`, secret) + payload = base64encode(JSON.stringify(payload), true) + token = hmac_base64(`${HS256_HEADER}.${payload}`, secret) - return `${HS256_HEADER}.${payload}.${token}` - }, + return `${HS256_HEADER}.${payload}.${token}` + }, - // 校验token - verify(token = '', secret) { - let jwt = token.split('.') - let auth, payload + // 校验token + verify(token = '', secret) { + let jwt = token.split('.') + let [_, payload, auth] = jwt + + if (jwt.length !== 3) { + return false + } + + payload = JSON.parse(base64decode(payload, true)) + + // 如果已经过期, 则不再校验hash + if (payload.expires < Date.now()) { + return false + } + + if (hmac_base64(jwt.join('.'), secret) === auth) { + return payload.data + } - if (jwt.length !== 3) { return false } - auth = jwt.pop() - payload = JSON.parse(base64decode(jwt[1], true)) - - // 如果已经过期, 则不再校验hash - if (payload.expires < Date.now()) { - return false - } - - if (hmac_base64(jwt.join('.'), secret) === auth) { - return payload.data - } - - return false } } } } - -export function createJwt() { - return function (req, res, next) { - let { secret, level } = this.get('jwt') - let deviceID = '' - let ssid - - // options请求不处理jwt - if (req.method === 'OPTIONS') { - return next() - } - - // 校验UA - if (level & 2) { - deviceID += req.header('user-agent') - } - - // 校验IP - if (level & 4) { - deviceID += req.ip() - } - - if (deviceID) { - deviceID = sha1(deviceID) - } - - req.__mix_key__ = secret + deviceID - - next() - } -} diff --git a/package.json b/package.json index 1b5d6fe..760c0ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gm5/jwt", - "version": "2.0.3", + "version": "3.0.0", "type": "module", "description": "json web token", "main": "index.js",