1
1
import * as jose from "jose" ;
2
- import { PublicFederatedToken } from "jwt" ;
2
+ import {
3
+ PublicFederatedToken ,
4
+ TokenExpiredError ,
5
+ TokenInvalidError ,
6
+ } from "./jwt" ;
3
7
import { KeyObject } from "node:crypto" ;
4
8
5
9
type TokenSignerOptions = {
@@ -70,15 +74,28 @@ export class TokenSigner {
70
74
}
71
75
72
76
async verifyJWT ( value : string ) {
73
- return await jose . jwtVerify (
74
- value ,
75
- this . _signKeys . getKeyFunction . bind ( this . _signKeys ) ,
76
- {
77
- algorithms : [ "HS256" ] ,
78
- audience : this . config . audience ,
79
- issuer : this . config . issuer ,
77
+ try {
78
+ return await jose . jwtVerify (
79
+ value ,
80
+ this . _signKeys . getKeyFunction . bind ( this . _signKeys ) ,
81
+ {
82
+ algorithms : [ "HS256" ] ,
83
+ audience : this . config . audience ,
84
+ issuer : this . config . issuer ,
85
+ }
86
+ ) ;
87
+ } catch ( e ) {
88
+ if ( e instanceof jose . errors . JWTClaimValidationFailed ) {
89
+ throw new TokenExpiredError ( e . message ) ;
80
90
}
81
- ) ;
91
+ if ( e instanceof jose . errors . JWTExpired ) {
92
+ throw new TokenExpiredError ( e . message ) ;
93
+ }
94
+ if ( e instanceof Error ) {
95
+ throw new TokenInvalidError ( e . message ) ;
96
+ }
97
+ throw e ;
98
+ }
82
99
}
83
100
84
101
// For refresh token, encrypt the token (JWE)
@@ -96,14 +113,21 @@ export class TokenSigner {
96
113
}
97
114
98
115
async decryptJWT ( jwt : string ) {
99
- return await jose . jwtDecrypt (
100
- jwt ,
101
- this . _encryptKeys . getKeyFunction . bind ( this . _encryptKeys ) ,
102
- {
103
- audience : this . config . audience ,
104
- issuer : this . config . issuer ,
116
+ try {
117
+ return await jose . jwtDecrypt (
118
+ jwt ,
119
+ this . _encryptKeys . getKeyFunction . bind ( this . _encryptKeys ) ,
120
+ {
121
+ audience : this . config . audience ,
122
+ issuer : this . config . issuer ,
123
+ }
124
+ ) ;
125
+ } catch ( e ) {
126
+ if ( e instanceof jose . errors . JWTClaimValidationFailed ) {
127
+ throw new TokenExpiredError ( e . message ) ;
105
128
}
106
- ) ;
129
+ throw new TokenInvalidError ( ) ;
130
+ }
107
131
}
108
132
}
109
133
0 commit comments