Skip to content

Commit b54b8c1

Browse files
committed
chore: update jwt-adapter docs
1 parent dfc5b07 commit b54b8c1

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

libs/jwt-adapter/src/adapter.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { authAdapter } from '@palmares/auth';
33
import { sign } from './funcs/sign';
44
import { verify } from './funcs/verify';
55

6+
/**
7+
* Defines the structure of the payload used in JWT operations.
8+
*/
69
export 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+
*/
6066
export 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+
*/
7483
export 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+
*/
8096
export const jwtAdapter = authAdapter(
8197
({ secret, library = 'jose' }: { secret: string; library?: 'jsonwebtoken' | 'jose' }) => ({
8298
name: 'jwt',

libs/jwt-adapter/src/funcs/sign.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import * as jwt from 'jsonwebtoken';
33

44
import 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+
*/
616
export 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
}

libs/jwt-adapter/src/funcs/verify.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import * as jose from 'jose';
22
import * 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+
*/
414
export async function verify({
515
secret,
616
library,

0 commit comments

Comments
 (0)