import { Config , Provide , Scope , ScopeEnum } from '@midwayjs/decorator' ; import type { DecodeOptions , GetPublicKeyOrSecret , Jwt , Secret , SignOptions , VerifyOptions , } from 'jsonwebtoken' ; import * as jwt from 'jsonwebtoken' ; type JwtPayload = string | Buffer | Record < string , any > ; see https @ Provide ( ) @ Scope ( ScopeEnum . Singleton ) export class JwtService { @ Config ( 'jwt' ) private jwtConfig : any ; /**
     * Synchronously sign the given payload into a JSON Web Token string
     * payload - Payload to sign, could be an literal, buffer or string
     * secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.
     * [options] - Options for the signature
     * returns - The JSON Web Token string
     */ public signSync ( payload , options ? ) ; public signSync ( payload , secretOrPrivateKey , options ? ) ; public signSync ( payload , secretOrPrivateKey , options ? ) { if ( ! options ) { options = secretOrPrivateKey ; secretOrPrivateKey = this . jwtConfig ?. secret ; } if ( ! secretOrPrivateKey ) { throw new Error ( '[midway-jwt]: jwt secret should be set' ) ; } options = options ?? { } ; options . expiresIn = options . expiresIn ?? this . jwtConfig . expiresIn ; return jwt . sign ( payload , secretOrPrivateKey , options ) ; } /**
     * Asynchronous sign the given payload into a JSON Web Token string
     * payload - Payload to sign, could be an literal, buffer or string
     * secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.
     * [options] - Options for the signature
     * returns - The JSON Web Token string
     */ public async sign ( payload , options ? ) ; public async sign ( payload , secretOrPrivateKey , options ? ) ; public async sign ( payload , secretOrPrivateKey , options ? ) { if ( ! options ) { options = secretOrPrivateKey ; secretOrPrivateKey = this . jwtConfig ?. secret ; } if ( ! secretOrPrivateKey ) { throw new Error ( '[midway-jwt]: provide the jwt secret please' ) ; } options = options ?? { } ; options . expiresIn = options . expiresIn ?? this . jwtConfig . expiresIn ; return new Promise ( ( resolve , reject ) => { jwt . sign ( payload , secretOrPrivateKey , options , ( err , encoded ) => { if ( err ) { reject ( err ) ; } else { resolve ( encoded ) ; } } ) ; } ) ; } /**
     * Synchronously verify given token using a secret or a public key to get a decoded token
     * token - JWT string to verify
     * secretOrPublicKey - Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.
     * [options] - Options for the verification
     * returns - The decoded token.
     */ public verifySync ( token , options ) ; public verifySync ( token , options ) ; public verifySync ( token , secretOrPublicKey , options ? ) ; public verifySync ( token , secretOrPublicKey , options ? ) ; public verifySync ( token , secretOrPublicKey , options ? ) { if ( ! options ) { options = secretOrPublicKey ; secretOrPublicKey = this . jwtConfig ?. secret ; } if ( ! secretOrPublicKey ) { throw new Error ( '[midway-jwt]: provide the jwt secret please' ) ; } return jwt . verify ( token , secretOrPublicKey , options ) ; } /**
     * Asynchronous verify given token using a secret or a public key to get a decoded token
     * token - JWT string to verify
     * secretOrPublicKey - Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.
     * [options] - Options for the verification
     * returns - The decoded token.
     */ public async verify ( token , options ? ) ; public async verify ( token , options ? ) ; public async verify ( token , secretOrPublicKey , options ? ) ; public async verify ( token , secretOrPublicKey , options ? ) ; public async verify ( token , secretOrPublicKey , options ? ) { if ( ! options ) { options = secretOrPublicKey ; secretOrPublicKey = this . jwtConfig ?. secret ; } if ( ! secretOrPublicKey ) { throw new Error ( '[midway-jwt]: provide the jwt secret please' ) ; } return new Promise ( ( resolve , reject ) => { jwt . verify ( token , secretOrPublicKey , options , ( err , encoded ) => { if ( err ) { reject ( err ) ; } else { resolve ( encoded ) ; } } ) ; } ) ; } /**
     * Returns the decoded payload without verifying if the signature is valid.
     * token - JWT string to decode
     * [options] - Options for decoding
     * returns - The decoded Token
     */ public decode ( token , options ) ; public decode ( token , options ) ; public decode ( token , options ? ) ; public decode ( token , options ? ) { return jwt . decode ( token , options ) ; } param token param options public decodeSync ( token , options ) ; public decodeSync ( token , options ) ; public decodeSync ( token , options ? ) ; public decodeSync ( token , options ? ) { return this . decode ( token , options ) ; } }