@@ -13,53 +13,6 @@ import Logger from './Logger';
1313 * algorithm — Encryption algorithm to be used to protect the token.
1414 */
1515
16- export default class JWT {
17- private static readPublicKey ( ) : Promise < string > {
18- return promisify ( readFile ) ( path . join ( __dirname , '../../keys/public.pem' ) , 'utf8' ) ;
19- }
20-
21- private static readPrivateKey ( ) : Promise < string > {
22- return promisify ( readFile ) ( path . join ( __dirname , '../../keys/private.pem' ) , 'utf8' ) ;
23- }
24-
25- public static async encode ( payload : JwtPayload ) : Promise < string > {
26- const cert = await this . readPrivateKey ( ) ;
27- if ( ! cert ) throw new InternalError ( 'Token generation failure' ) ;
28- // @ts -ignore
29- return promisify ( sign ) ( { ...payload } , cert , { algorithm : 'RS256' } ) ;
30- }
31-
32- /**
33- * This method checks the token and returns the decoded data when token is valid in all respect
34- */
35- public static async validate ( token : string ) : Promise < JwtPayload > {
36- const cert = await this . readPublicKey ( ) ;
37- try {
38- // @ts -ignore
39- return ( await promisify ( verify ) ( token , cert ) ) as JwtPayload ;
40- } catch ( e : any ) {
41- Logger . debug ( e ) ;
42- if ( e && e . name === 'TokenExpiredError' ) throw new TokenExpiredError ( ) ;
43- // throws error if the token has not been encrypted by the private key
44- throw new BadTokenError ( ) ;
45- }
46- }
47-
48- /**
49- * Returns the decoded payload if the signature is valid even if it is expired
50- */
51- public static async decode ( token : string ) : Promise < JwtPayload > {
52- const cert = await this . readPublicKey ( ) ;
53- try {
54- // @ts -ignore
55- return ( await promisify ( verify ) ( token , cert , { ignoreExpiration : true } ) ) as JwtPayload ;
56- } catch ( e ) {
57- Logger . debug ( e ) ;
58- throw new BadTokenError ( ) ;
59- }
60- }
61- }
62-
6316export class JwtPayload {
6417 aud : string ;
6518 sub : string ;
@@ -77,3 +30,54 @@ export class JwtPayload {
7730 this . prm = param ;
7831 }
7932}
33+
34+ async function readPublicKey ( ) : Promise < string > {
35+ return promisify ( readFile ) ( path . join ( __dirname , '../../keys/public.pem' ) , 'utf8' ) ;
36+ }
37+
38+ async function readPrivateKey ( ) : Promise < string > {
39+ return promisify ( readFile ) ( path . join ( __dirname , '../../keys/private.pem' ) , 'utf8' ) ;
40+ }
41+
42+ async function encode ( payload : JwtPayload ) : Promise < string > {
43+ const cert = await readPrivateKey ( ) ;
44+ if ( ! cert ) throw new InternalError ( 'Token generation failure' ) ;
45+ // @ts -ignore
46+ return promisify ( sign ) ( { ...payload } , cert , { algorithm : 'RS256' } ) ;
47+ }
48+
49+ /**
50+ * This method checks the token and returns the decoded data when token is valid in all respect
51+ */
52+ async function validate ( token : string ) : Promise < JwtPayload > {
53+ const cert = await readPublicKey ( ) ;
54+ try {
55+ // @ts -ignore
56+ return ( await promisify ( verify ) ( token , cert ) ) as JwtPayload ;
57+ } catch ( e : any ) {
58+ Logger . debug ( e ) ;
59+ if ( e && e . name === 'TokenExpiredError' ) throw new TokenExpiredError ( ) ;
60+ // throws error if the token has not been encrypted by the private key
61+ throw new BadTokenError ( ) ;
62+ }
63+ }
64+
65+ /**
66+ * Returns the decoded payload if the signature is valid even if it is expired
67+ */
68+ async function decode ( token : string ) : Promise < JwtPayload > {
69+ const cert = await readPublicKey ( ) ;
70+ try {
71+ // @ts -ignore
72+ return ( await promisify ( verify ) ( token , cert , { ignoreExpiration : true } ) ) as JwtPayload ;
73+ } catch ( e ) {
74+ Logger . debug ( e ) ;
75+ throw new BadTokenError ( ) ;
76+ }
77+ }
78+
79+ export default {
80+ encode,
81+ validate,
82+ decode,
83+ } ;
0 commit comments