File size: 2,529 Bytes
10852fa
 
9592df2
 
10852fa
 
 
 
 
 
 
9592df2
 
10852fa
 
 
9592df2
 
 
 
 
 
 
 
 
 
10852fa
9592df2
 
10852fa
9592df2
 
10852fa
9592df2
 
10852fa
9592df2
 
10852fa
9592df2
 
 
 
 
 
 
 
 
 
 
 
10852fa
9592df2
 
10852fa
 
9592df2
10852fa
 
9592df2
 
 
 
 
 
10852fa
9592df2
 
10852fa
 
9592df2
10852fa
 
9592df2
 
 
 
 
 
10852fa
9592df2
 
10852fa
9592df2
 
10852fa
 
9592df2
10852fa
 
 
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
import epoch from '../lib/epoch.js';
import secs from '../lib/secs.js';
import isObject from '../lib/is_object.js';
import { encoder } from '../lib/buffer_utils.js';
function validateInput(label, input) {
    if (!Number.isFinite(input)) {
        throw new TypeError(`Invalid ${label} input`);
    }
    return input;
}
export class ProduceJWT {
    #payload;
    constructor(payload) {
        if (!isObject(payload)) {
            throw new TypeError('JWT Claims Set MUST be an object');
        }
        this.#payload = structuredClone(payload);
    }
    data() {
        return encoder.encode(JSON.stringify(this.#payload));
    }
    get iss() {
        return this.#payload.iss;
    }
    set iss(value) {
        this.#payload.iss = value;
    }
    get sub() {
        return this.#payload.sub;
    }
    set sub(value) {
        this.#payload.sub = value;
    }
    get aud() {
        return this.#payload.aud;
    }
    set aud(value) {
        this.#payload.aud = value;
    }
    get jti() {
        return this.#payload.jti;
    }
    set jti(value) {
        this.#payload.jti = value;
    }
    get nbf() {
        return this.#payload.nbf;
    }
    set nbf(value) {
        if (typeof value === 'number') {
            this.#payload.nbf = validateInput('setNotBefore', value);
        }
        else if (value instanceof Date) {
            this.#payload.nbf = validateInput('setNotBefore', epoch(value));
        }
        else {
            this.#payload.nbf = epoch(new Date()) + secs(value);
        }
    }
    get exp() {
        return this.#payload.exp;
    }
    set exp(value) {
        if (typeof value === 'number') {
            this.#payload.exp = validateInput('setExpirationTime', value);
        }
        else if (value instanceof Date) {
            this.#payload.exp = validateInput('setExpirationTime', epoch(value));
        }
        else {
            this.#payload.exp = epoch(new Date()) + secs(value);
        }
    }
    get iat() {
        return this.#payload.iat;
    }
    set iat(value) {
        if (typeof value === 'undefined') {
            this.#payload.iat = epoch(new Date());
        }
        else if (value instanceof Date) {
            this.#payload.iat = validateInput('setIssuedAt', epoch(value));
        }
        else if (typeof value === 'string') {
            this.#payload.iat = validateInput('setIssuedAt', epoch(new Date()) + secs(value));
        }
        else {
            this.#payload.iat = validateInput('setIssuedAt', value);
        }
    }
}