-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathjwt.ts
167 lines (145 loc) · 4.76 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
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
// 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,
TypeClaim,
DataTypeClaim,
TypeToken,
type MuxJWTSignOptions,
type MuxJWTSignOptionsMultiple,
type Tokens,
isMuxJWTSignOptionsMultiple,
} from '@mux/mux-node/util/jwt-types';
export class Jwt extends APIResource {
async signPlaybackId(
playbackId: string,
config?: MuxJWTSignOptions<keyof typeof TypeClaim>,
): Promise<string>;
async signPlaybackId(
playbackId: string,
config?: MuxJWTSignOptionsMultiple<keyof typeof TypeClaim>,
): Promise<Tokens>;
/**
* Creates a new token or tokens to be used with a signed Playback ID
*/
async signPlaybackId(
playbackId: string,
config:
| MuxJWTSignOptions<keyof typeof TypeClaim>
| MuxJWTSignOptionsMultiple<keyof typeof TypeClaim> = {},
): Promise<string | Tokens> {
if (isMuxJWTSignOptionsMultiple(config)) {
return this.signPlaybackIdMultipleTypes(playbackId, config);
} else {
return this.signPlaybackIdSingleType(playbackId, config);
}
}
private async signPlaybackIdSingleType(
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);
}
private async signPlaybackIdMultipleTypes(
playbackId: string,
config: MuxJWTSignOptionsMultiple<keyof typeof TypeClaim>,
): Promise<Tokens> {
const tokens: Tokens = {};
for (const typeOption of config.type) {
let type: keyof typeof TypeClaim;
let params: Record<string, string> | undefined;
if (Array.isArray(typeOption)) {
[type, params] = typeOption;
} else {
type = typeOption;
params = undefined;
}
const singleConfig = {
...config,
type,
params: {
...config.params,
...params,
},
};
const token = await this.signPlaybackIdSingleType(playbackId, singleConfig);
const tokenKey = TypeToken[type];
if (tokenKey) {
tokens[tokenKey] = token;
}
}
return tokens;
}
/**
* 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);
}
}