Skip to content

Commit 8e7b56e

Browse files
authored
chore: added documentation to most important methods (#16)
1 parent decc25d commit 8e7b56e

File tree

10 files changed

+296
-6
lines changed

10 files changed

+296
-6
lines changed

jsdoc.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@
55
"plugins": ["node_modules/better-docs/typescript"],
66
"source": {
77
"include": ["readme.md", "src"],
8-
"includePattern": "\\.(jsx|js|ts|tsx)$",
9-
"excludePattern": "(node_modules/|docs)"
8+
"includePattern": "\\.(ts)$",
9+
"excludePattern": "(node_modules/|docs|lib/)"
1010
},
1111
"opts": {
12-
"destination": "./docs/"
12+
"destination": "./docs/",
13+
"recurse": true
14+
},
15+
"templates": {
16+
"default": {
17+
"outputSourceFiles": false
18+
},
19+
"cleverLinks": false,
20+
"monospaceLinks": false,
21+
"search": true,
22+
"better-docs": {
23+
"hideGenerator": true
24+
}
1325
}
1426
}

src/cat.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ const claimTypeValidators: { [key: string]: (value: string) => boolean } = {
8484

8585
const CWT_TAG = 61;
8686

87+
/**
88+
* Common Access Token Claims
89+
*/
8790
export type CommonAccessTokenClaims = {
8891
[key: string]: string | number | Map<number, any>;
8992
};
@@ -96,14 +99,34 @@ export type CommonAccessTokenValue =
9699
| Buffer
97100
| Map<number, any>;
98101

102+
/**
103+
* CWT Encryption Key
104+
*/
99105
export interface CWTEncryptionKey {
106+
/**
107+
* Key
108+
*/
100109
k: Buffer;
110+
/**
111+
* Key ID
112+
*/
101113
kid: string;
102114
}
115+
116+
/**
117+
* CWT Decryption Key
118+
*/
103119
export interface CWTDecryptionKey {
120+
/**
121+
* Key
122+
*/
104123
k: Buffer;
124+
/**
125+
* Key ID
126+
*/
105127
kid: string;
106128
}
129+
107130
export interface CWTSigningKey {
108131
d: Buffer;
109132
kid: string;
@@ -157,6 +180,9 @@ function updateMapFromDict(
157180
return claims;
158181
}
159182

183+
/**
184+
* Common Access Token
185+
*/
160186
export class CommonAccessToken {
161187
private payload: Map<number, CommonAccessTokenValue>;
162188
private data?: Buffer;
@@ -166,9 +192,21 @@ export class CommonAccessToken {
166192
this.payload = updateMapFromClaims(claims);
167193
}
168194

195+
/**
196+
* Create a CWT CAT token
197+
*/
169198
public async mac(
199+
/**
200+
* Encryption key
201+
*/
170202
key: CWTEncryptionKey,
203+
/**
204+
* Algorithm to use
205+
*/
171206
alg: string,
207+
/**
208+
* Options
209+
*/
172210
opts?: {
173211
addCwtTag: boolean;
174212
}
@@ -198,6 +236,9 @@ export class CommonAccessToken {
198236
this.kid = key.kid;
199237
}
200238

239+
/**
240+
* Parse a CWT CAT token
241+
*/
201242
public async parse(
202243
token: Buffer,
203244
key: CWTDecryptionKey,
@@ -253,6 +294,9 @@ export class CommonAccessToken {
253294
}
254295
}
255296

297+
/**
298+
* Validate all claims in the token
299+
*/
256300
public async isValid(opts: CatValidationOptions): Promise<boolean> {
257301
this.validateTypes();
258302

@@ -372,6 +416,9 @@ export class CommonAccessToken {
372416
}
373417
}
374418

419+
/**
420+
* Common Access Token Factory
421+
*/
375422
export class CommonAccessTokenFactory {
376423
public static async fromSignedToken(
377424
base64encoded: string,

src/errors.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,80 @@
11
import { CommonAccessTokenValue } from './cat';
22

3+
/**
4+
* Error thrown when a key is not found for validating a token
5+
*/
36
export class KeyNotFoundError extends Error {
47
constructor() {
58
super(`Failed to validate token signature with any of the available keys`);
69
}
710
}
811

12+
/**
13+
* Error thrown when an invalid claim type is found
14+
*/
915
export class InvalidClaimTypeError extends Error {
1016
constructor(claim: string, value: CommonAccessTokenValue) {
1117
super(`Invalid claim type for ${claim}: ${typeof value}`);
1218
}
1319
}
1420

21+
/**
22+
* Error thrown when an invalid issuer is found
23+
*/
1524
export class InvalidIssuerError extends Error {
1625
constructor(issuer: CommonAccessTokenValue | undefined) {
1726
super(`Invalid issuer: ${issuer || 'undefined'}`);
1827
}
1928
}
2029

30+
/**
31+
* Error thrown when token has expired
32+
*/
2133
export class TokenExpiredError extends Error {
2234
constructor() {
2335
super('Token has expired');
2436
}
2537
}
2638

39+
/**
40+
* Error thrown when audience is not valid
41+
*/
2742
export class InvalidAudienceError extends Error {
2843
constructor(audience: string[]) {
2944
super(`Invalid audience: ${audience.join(', ')}`);
3045
}
3146
}
3247

48+
/**
49+
* Error thrown when token is not yet active
50+
*/
3351
export class TokenNotActiveError extends Error {
3452
constructor() {
3553
super('Token is not yet active');
3654
}
3755
}
3856

57+
/**
58+
* Error thrown when CATU claim is invalid
59+
*/
3960
export class InvalidCatuError extends Error {
4061
constructor(reason: string) {
4162
super(reason);
4263
}
4364
}
4465

66+
/**
67+
* Error thrown when trying to access a URI that does not match the allowed URIs
68+
*/
4569
export class UriNotAllowedError extends Error {
4670
constructor(reason: string) {
4771
super(reason);
4872
}
4973
}
5074

75+
/**
76+
* Error thrown when trying to renew a claim that is not renewable
77+
*/
5178
export class RenewalClaimError extends Error {
5279
constructor(reason: string) {
5380
super(reason);

0 commit comments

Comments
 (0)