@@ -8,6 +8,7 @@ import type {
8
8
JWEHeaderParameters ,
9
9
FlattenedJWE ,
10
10
JWTDecryptResult ,
11
+ ResolvedKey ,
11
12
} from '../types.d'
12
13
import jwtPayload from '../lib/jwt_claims_set.js'
13
14
import { JWTClaimValidationFailed } from '../util/errors.js'
@@ -27,7 +28,7 @@ export interface JWTDecryptGetKey extends GetKeyFunction<JWEHeaderParameters, Fl
27
28
* Verifies the JWT format (to be a JWE Compact format), decrypts the ciphertext, validates the JWT Claims Set.
28
29
*
29
30
* @param jwt JSON Web Token value (encoded as JWE).
30
- * @param key Private Key or Secret, or a function resolving one, to decrypt and verify the JWT with.
31
+ * @param key Private Key or Secret to decrypt and verify the JWT with.
31
32
* @param options JWT Decryption and JWT Claims Set validation options.
32
33
*
33
34
* @example ESM import
@@ -58,12 +59,27 @@ export interface JWTDecryptGetKey extends GetKeyFunction<JWEHeaderParameters, Fl
58
59
* console.log(payload)
59
60
* ```
60
61
*/
62
+ async function jwtDecrypt (
63
+ jwt : string | Uint8Array ,
64
+ key : KeyLike ,
65
+ options ?: JWTDecryptOptions ,
66
+ ) : Promise < JWTDecryptResult >
67
+ /**
68
+ * @param jwt JSON Web Token value (encoded as JWE).
69
+ * @param getKey Function resolving Private Key or Secret to decrypt and verify the JWT with.
70
+ * @param options JWT Decryption and JWT Claims Set validation options.
71
+ */
72
+ async function jwtDecrypt (
73
+ jwt : string | Uint8Array ,
74
+ getKey : JWTDecryptGetKey ,
75
+ options ?: JWTDecryptOptions ,
76
+ ) : Promise < JWTDecryptResult & ResolvedKey >
61
77
async function jwtDecrypt (
62
78
jwt : string | Uint8Array ,
63
79
key : KeyLike | JWTDecryptGetKey ,
64
80
options ?: JWTDecryptOptions ,
65
- ) : Promise < JWTDecryptResult > {
66
- const decrypted = await decrypt ( jwt , key , options )
81
+ ) {
82
+ const decrypted = await decrypt ( jwt , < Parameters < typeof decrypt > [ 1 ] > key , options )
67
83
const payload = jwtPayload ( decrypted . protectedHeader , decrypted . plaintext , options )
68
84
69
85
const { protectedHeader } = decrypted
@@ -95,7 +111,13 @@ async function jwtDecrypt(
95
111
)
96
112
}
97
113
98
- return { payload, protectedHeader }
114
+ const result = { payload, protectedHeader }
115
+
116
+ if ( typeof key === 'function' ) {
117
+ return { ...result , key : decrypted . key }
118
+ }
119
+
120
+ return result
99
121
}
100
122
101
123
export { jwtDecrypt }
0 commit comments