-
Notifications
You must be signed in to change notification settings - Fork 60
/
jwt.ts
103 lines (91 loc) · 3.16 KB
/
jwt.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
// File generated from our OpenAPI spec by Stainless.
import { APIResource } from '@mux/mux-node/resource';
import * as jwt from '@mux/mux-node/_shims/auto/jwt';
import {
type SignOptions,
type MuxJWTSignOptions,
TypeClaim,
DataTypeClaim,
} from '@mux/mux-node/util/jwt-types';
export class Jwt extends APIResource {
/**
* Creates a new token to be used with a signed Playback ID
*/
async signPlaybackId(
playbackId: string,
config: MuxJWTSignOptions<keyof typeof TypeClaim> = {},
): Promise<string> {
const claim = TypeClaim[config.type ?? 'video'];
if (!claim) {
throw new Error(`Invalid signature type: ${config.type}; Expected one of ${Object.keys(TypeClaim)}`);
}
const tokenOptions: SignOptions = {
keyid: jwt.getSigningKey(this._client, config),
subject: playbackId,
audience: claim,
expiresIn: config.expiration ?? '7d',
noTimestamp: true,
algorithm: 'RS256',
};
return jwt.sign(config.params ?? {}, await jwt.getPrivateKey(this._client, config), tokenOptions);
}
/**
* Creates a new token for a license for playing back DRM'd video content
*/
async signDrmLicense(
playbackId: string,
config: MuxJWTSignOptions<keyof typeof TypeClaim> = {},
): Promise<string> {
const claim = TypeClaim[config.type ?? 'drm_license'];
if (!claim) {
throw new Error(`Invalid signature type: ${config.type}; Expected one of ${Object.keys(TypeClaim)}`);
}
const tokenOptions: SignOptions = {
keyid: jwt.getSigningKey(this._client, config),
subject: playbackId,
audience: claim,
expiresIn: config.expiration ?? '7d',
noTimestamp: true,
algorithm: 'RS256',
};
return jwt.sign(config.params ?? {}, await jwt.getPrivateKey(this._client, config), tokenOptions);
}
/**
* Creates a new token to be used with a space
* @deprecated Mux Real-Time Video (spaces) has been shut down. This function will be removed in the next major version.
*/
async signSpaceId(spaceId: string, config: MuxJWTSignOptions<never> = {}): Promise<string> {
const tokenOptions: SignOptions = {
keyid: jwt.getSigningKey(this._client, config),
subject: spaceId,
audience: 'rt',
expiresIn: config.expiration ?? '7d',
noTimestamp: true,
algorithm: 'RS256',
};
return jwt.sign(config.params ?? {}, await jwt.getPrivateKey(this._client, config), tokenOptions);
}
/**
* Creates a new token to be used with a signed statistics request
*/
async signViewerCounts(
id: string,
config: MuxJWTSignOptions<keyof typeof DataTypeClaim> = {},
): Promise<string> {
const claim = DataTypeClaim[config.type ?? 'video'];
if (!claim) {
throw new Error(
`Invalid signature type: ${config.type}; Expected one of ${Object.keys(DataTypeClaim)}`,
);
}
const tokenOptions: SignOptions = {
keyid: jwt.getSigningKey(this._client, config),
subject: id,
audience: claim,
expiresIn: config.expiration ?? '7d',
noTimestamp: true,
algorithm: 'RS256',
};
return jwt.sign(config.params ?? {}, await jwt.getPrivateKey(this._client, config), tokenOptions);
}
}