import { Config , Inject , Provide , Scope , ScopeEnum } from '@midwayjs/decorator' ; import * as jwt from 'jsonwebtoken' ; import { Crypto } from './crypto' ; import { Lock } from './lock' ; @ Scope ( ScopeEnum . Singleton ) @ Provide ( ) export class Jwt { @ Config ( 'jwt.secret' ) private readonly secret ; @ Config ( 'jwt.signOptions' ) private readonly signOptions ; @ Config ( 'jwt.verifyOptions' ) private readonly verifyOptions ; @ Inject ( ) private readonly crypto : Crypto ; @ Inject ( ) private readonly lock : Lock ; public async checkRevoked ( token ) { const key = this . crypto . md5 ( token ) ; const result = await this . lock . checkLock ( key ) ; return result ; } public async revoke ( token ) { const key = this . crypto . md5 ( token ) ; await this . lock . lock ( key , this . signOptions . expiresIn ) ; } public sign ( payload , secretOrPrivateKey ? , options ? ) { const secret = secretOrPrivateKey ? secretOrPrivateKey : this . secret ; const opts = Object . assign ( this . signOptions , options ) ; return jwt . sign ( payload , secret , opts ) ; } public verify ( token , secretOrPublicKey ? , options ? ) { const secret = secretOrPublicKey ? secretOrPublicKey : this . secret ; const opts = Object . assign ( this . verifyOptions , options ) ; return jwt . verify ( token , secret , opts ) ; } }