deno.land / x / jose@v5.2.4 / jwt / 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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'
function validateInput(label: string, input: number) { if (!Number.isFinite(input)) { throw new TypeError(`Invalid ${label} input`) }
return input}
/** Generic class for JWT producing. */export class ProduceJWT { protected _payload!: JWTPayload
/** @param payload The JWT Claims Set object. Defaults to an empty object. */ constructor(payload: JWTPayload = {}) { if (!isObject(payload)) { throw new TypeError('JWT Claims Set MUST be an object') } this._payload = payload }
/** * Set the "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 the "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 the "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 the "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 the "nbf" (Not Before) Claim. * * - If a `number` is passed as an argument it is used as the claim directly. * - If a `Date` instance is passed as an argument it is converted to unix timestamp and used as the * claim. * - If a `string` is passed as an argument it is resolved to a time span, and then added to the * current unix timestamp and used as the claim. * * Format used for time span should be a number followed by a unit, such as "5 minutes" or "1 * day". * * Valid units are: "sec", "secs", "second", "seconds", "s", "minute", "minutes", "min", "mins", * "m", "hour", "hours", "hr", "hrs", "h", "day", "days", "d", "week", "weeks", "w", "year", * "years", "yr", "yrs", and "y". It is not possible to specify months. 365.25 days is used as an * alias for a year. * * If the string is suffixed with "ago", or prefixed with a "-", the resulting time span gets * subtracted from the current unix timestamp. A "from now" suffix can also be used for * readability when adding to the current unix timestamp. * * @param input "nbf" (Not Before) Claim value to set on the JWT Claims Set. */ setNotBefore(input: number | string | Date) { if (typeof input === 'number') { this._payload = { ...this._payload, nbf: validateInput('setNotBefore', input) } } else if (input instanceof Date) { this._payload = { ...this._payload, nbf: validateInput('setNotBefore', epoch(input)) } } else { this._payload = { ...this._payload, nbf: epoch(new Date()) + secs(input) } } return this }
/** * Set the "exp" (Expiration Time) Claim. * * - If a `number` is passed as an argument it is used as the claim directly. * - If a `Date` instance is passed as an argument it is converted to unix timestamp and used as the * claim. * - If a `string` is passed as an argument it is resolved to a time span, and then added to the * current unix timestamp and used as the claim. * * Format used for time span should be a number followed by a unit, such as "5 minutes" or "1 * day". * * Valid units are: "sec", "secs", "second", "seconds", "s", "minute", "minutes", "min", "mins", * "m", "hour", "hours", "hr", "hrs", "h", "day", "days", "d", "week", "weeks", "w", "year", * "years", "yr", "yrs", and "y". It is not possible to specify months. 365.25 days is used as an * alias for a year. * * If the string is suffixed with "ago", or prefixed with a "-", the resulting time span gets * subtracted from the current unix timestamp. A "from now" suffix can also be used for * readability when adding to the current unix timestamp. * * @param input "exp" (Expiration Time) Claim value to set on the JWT Claims Set. */ setExpirationTime(input: number | string | Date) { if (typeof input === 'number') { this._payload = { ...this._payload, exp: validateInput('setExpirationTime', input) } } else if (input instanceof Date) { this._payload = { ...this._payload, exp: validateInput('setExpirationTime', epoch(input)) } } else { this._payload = { ...this._payload, exp: epoch(new Date()) + secs(input) } } return this }
/** * Set the "iat" (Issued At) Claim. * * - If no argument is used the current unix timestamp is used as the claim. * - If a `number` is passed as an argument it is used as the claim directly. * - If a `Date` instance is passed as an argument it is converted to unix timestamp and used as the * claim. * - If a `string` is passed as an argument it is resolved to a time span, and then added to the * current unix timestamp and used as the claim. * * Format used for time span should be a number followed by a unit, such as "5 minutes" or "1 * day". * * Valid units are: "sec", "secs", "second", "seconds", "s", "minute", "minutes", "min", "mins", * "m", "hour", "hours", "hr", "hrs", "h", "day", "days", "d", "week", "weeks", "w", "year", * "years", "yr", "yrs", and "y". It is not possible to specify months. 365.25 days is used as an * alias for a year. * * If the string is suffixed with "ago", or prefixed with a "-", the resulting time span gets * subtracted from the current unix timestamp. A "from now" suffix can also be used for * readability when adding to the current unix timestamp. * * @param input "iat" (Expiration Time) Claim value to set on the JWT Claims Set. */ setIssuedAt(input?: number | string | Date) { if (typeof input === 'undefined') { this._payload = { ...this._payload, iat: epoch(new Date()) } } else if (input instanceof Date) { this._payload = { ...this._payload, iat: validateInput('setIssuedAt', epoch(input)) } } else if (typeof input === 'string') { this._payload = { ...this._payload, iat: validateInput('setIssuedAt', epoch(new Date()) + secs(input)), } } else { this._payload = { ...this._payload, iat: validateInput('setIssuedAt', input) } } return this }}
jose

Version Info

Tagged at
a month ago