File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed
Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,9 @@ import { authAdapter } from '@palmares/auth';
33import { sign } from './funcs/sign' ;
44import { verify } from './funcs/verify' ;
55
6+ /**
7+ * Defines the structure of the payload used in JWT operations.
8+ */
69export interface JWTPayload {
710 /**
811 * JWT Issuer
@@ -57,6 +60,9 @@ export interface JWTPayload {
5760 [ propName : string ] : unknown ;
5861}
5962
63+ /**
64+ * Enumerates the supported JWT signing algorithms.
65+ */
6066export type JWTAlgorithm =
6167 | 'HS256'
6268 | 'HS384'
@@ -71,12 +77,22 @@ export type JWTAlgorithm =
7177 | 'PS384'
7278 | 'PS512' ;
7379
80+ /**
81+ * Defines the options for JWT operations.
82+ */
7483export interface JWTOptions {
7584 alg : JWTAlgorithm ;
7685 typ ?: 'JWT' ;
7786 exp : number ; // in seconds
7887}
7988
89+ /**
90+ * Provides JWT authentication methods such as signing and verifying tokens.
91+ * We recommend using the 'jose' library for JWT operations due to its comprehensive support for modern JWT standards.
92+ * Please refer to the documentation in `@palmares/auth` for more details on adapter implementations.
93+ *
94+ * Warning: Ensure that the `secret` provided is securely stored and managed to prevent security vulnerabilities.
95+ */
8096export const jwtAdapter = authAdapter (
8197 ( { secret, library = 'jose' } : { secret : string ; library ?: 'jsonwebtoken' | 'jose' } ) => ( {
8298 name : 'jwt' ,
Original file line number Diff line number Diff line change @@ -3,6 +3,16 @@ import * as jwt from 'jsonwebtoken';
33
44import type { JWTOptions , JWTPayload } from '../adapter' ;
55
6+ /**
7+ * Signs a JWT using the specified library and options.
8+ *
9+ * @param {string } secret - The secret key used for signing the JWT.
10+ * @param {'jsonwebtoken' | 'jose' } library - The library to use for signing ('jsonwebtoken' or 'jose').
11+ * @param {JWTPayload } payload - The payload to be included in the JWT.
12+ * @param {JWTOptions } options - The options to configure the JWT signing process.
13+ * @returns {Promise<string> } A promise that resolves with the signed JWT.
14+ * @throws {Error } If the specified library is not supported or if signing fails.
15+ */
616export async function sign ( {
717 secret,
818 library,
@@ -45,6 +55,6 @@ export async function sign({
4555 return jwtJose ;
4656 }
4757 default :
48- throw new Error ( `unsupported jwt library: ${ library } ` ) ;
58+ throw new Error ( `Unsupported JWT library: ${ library } ` ) ;
4959 }
5060}
Original file line number Diff line number Diff line change 11import * as jose from 'jose' ;
22import * as jwt from 'jsonwebtoken' ;
33
4+ /**
5+ * Verifies a JWT token using the specified library and options.
6+ *
7+ * @param {string } secret - The secret key used to verify the JWT.
8+ * @param {'jsonwebtoken' | 'jose' } library - The library to use for verification ('jsonwebtoken' or 'jose').
9+ * @param {string } token - The JWT token to verify.
10+ * @param {object } options - The options to pass to the JWT verification function.
11+ * @returns {Promise<object> } A promise that resolves with the decoded token payload if verification is successful.
12+ * @throws {Error } If the specified library is not supported or if verification fails.
13+ */
414export async function verify ( {
515 secret,
616 library,
You can’t perform that action at this time.
0 commit comments