更新导出结构; 增加index.d.ts声明

master
yutent 2025-01-06 10:52:59 +08:00
parent f0058ae87b
commit 7e29408bdf
3 changed files with 86 additions and 91 deletions

28
index.d.ts vendored
View File

@ -1,27 +1,21 @@
import Request from '@gm5/request'
import Response from '@gm5/response'
declare module '@gm5/jwt' { declare module '@gm5/jwt' {
//
interface JwtConfig { interface JwtConfig {
secret: string
ttl?: number ttl?: number
level?: number level?: number
secret: string }
interface JwtInstance {
ttl: number
sign(data: object, secret: string): string
verify(token: string, secret: string): object | false
} }
interface JwtModule { interface JwtModule {
name: string name: 'jwt'
install(conf?: JwtConfig): { install(conf?: JwtConfig): JwtInstance
ttl: number
sign(data: object, secret: string): string
verify(token: string, secret: string): object | false
}
} }
export const JwtModule: JwtModule export function createJwt(): JwtModule
export function createJwt(): (
req: Request,
res: Response,
next: () => void
) => void
} }

147
index.js
View File

@ -18,89 +18,90 @@ function hmac_base64(str, secret) {
return base64encode(buf, true) return base64encode(buf, true)
} }
export const JwtModule = { function JwtMiddleware(req, res, next) {
name: 'jwt', let { secret, level } = this.get('jwt')
install(conf = {}) { let deviceID = ''
if (!conf.secret) { let ssid
console.warn(
new Error( // options请求不处理jwt
'You must set a `secret` key for jwt, or it will use the default key.' 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) let jwt = Object.assign({}, DEFAULT_CONFIG, conf)
this.set({ jwt }) this.set({ jwt })
this.use(JwtMiddleware)
return { return {
ttl: jwt.ttl, ttl: jwt.ttl,
// 签名, 返回token // 签名, 返回token
// header: base64("{"typ":"JWT","alg":"HS256"}") // header: base64("{"typ":"JWT","alg":"HS256"}")
// 这里固定使用sha256 // 这里固定使用sha256
sign(data, secret) { sign(data, secret) {
// 加入过期时间, // 加入过期时间,
let payload = { data, expires: Date.now() + this.ttl * 1000 } let payload = { data, expires: Date.now() + this.ttl * 1000 }
let token = '' let token = ''
payload = base64encode(JSON.stringify(payload), true) payload = base64encode(JSON.stringify(payload), true)
token = hmac_base64(`${HS256_HEADER}.${payload}`, secret) token = hmac_base64(`${HS256_HEADER}.${payload}`, secret)
return `${HS256_HEADER}.${payload}.${token}` return `${HS256_HEADER}.${payload}.${token}`
}, },
// 校验token // 校验token
verify(token = '', secret) { verify(token = '', secret) {
let jwt = token.split('.') let jwt = token.split('.')
let auth, payload 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 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()
}
}

View File

@ -1,6 +1,6 @@
{ {
"name": "@gm5/jwt", "name": "@gm5/jwt",
"version": "2.0.3", "version": "3.0.0",
"type": "module", "type": "module",
"description": "json web token", "description": "json web token",
"main": "index.js", "main": "index.js",