-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
produce.ts
104 lines (95 loc) · 2.83 KB
/
produce.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import type { JWTPayload } from '../types.d.ts'
import epoch from '../lib/epoch.ts'
import isObject from '../lib/is_object.ts'
import secs from '../lib/secs.ts'
/** Generic class for JWT producing. */
export class ProduceJWT {
protected _payload!: JWTPayload
/** @param payload The JWT Claims Set object. */
constructor(payload: JWTPayload) {
if (!isObject(payload)) {
throw new TypeError('JWT Claims Set MUST be an object')
}
this._payload = payload
}
/**
* Set "iss" (Issuer) Claim.
*
* @param issuer "Issuer" Claim value to set on the JWT Claims Set.
*/
setIssuer(issuer: string) {
this._payload = { ...this._payload, iss: issuer }
return this
}
/**
* Set "sub" (Subject) Claim.
*
* @param subject "sub" (Subject) Claim value to set on the JWT Claims Set.
*/
setSubject(subject: string) {
this._payload = { ...this._payload, sub: subject }
return this
}
/**
* Set "aud" (Audience) Claim.
*
* @param audience "aud" (Audience) Claim value to set on the JWT Claims Set.
*/
setAudience(audience: string | string[]) {
this._payload = { ...this._payload, aud: audience }
return this
}
/**
* Set "jti" (JWT ID) Claim.
*
* @param jwtId "jti" (JWT ID) Claim value to set on the JWT Claims Set.
*/
setJti(jwtId: string) {
this._payload = { ...this._payload, jti: jwtId }
return this
}
/**
* Set "nbf" (Not Before) Claim.
*
* @param input "nbf" (Not Before) Claim value to set on the JWT Claims Set. When number is passed
* that is used as a value, when string is passed it is resolved to a time span and added to the
* current timestamp.
*/
setNotBefore(input: number | string) {
if (typeof input === 'number') {
this._payload = { ...this._payload, nbf: input }
} else {
this._payload = { ...this._payload, nbf: epoch(new Date()) + secs(input) }
}
return this
}
/**
* Set "exp" (Expiration Time) Claim.
*
* @param input "exp" (Expiration Time) Claim value to set on the JWT Claims Set. When number is
* passed that is used as a value, when string is passed it is resolved to a time span and added
* to the current timestamp.
*/
setExpirationTime(input: number | string) {
if (typeof input === 'number') {
this._payload = { ...this._payload, exp: input }
} else {
this._payload = { ...this._payload, exp: epoch(new Date()) + secs(input) }
}
return this
}
/**
* Set "iat" (Issued At) Claim.
*
* @param input "iat" (Issued At) Claim value to set on the JWT Claims Set. Default is current
* timestamp.
*/
setIssuedAt(input?: number) {
if (typeof input === 'undefined') {
this._payload = { ...this._payload, iat: epoch(new Date()) }
} else {
this._payload = { ...this._payload, iat: input }
}
return this
}
}