-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPayloadDto.ts
More file actions
50 lines (39 loc) · 1.42 KB
/
PayloadDto.ts
File metadata and controls
50 lines (39 loc) · 1.42 KB
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
import { AESEncryption, Utils } from "../QueueITHelpers";
import { Base64 } from "../helpers/Base64";
import { TokenOrigin } from "./TokenOrigin";
export class PayloadDto {
public RelativeQuality?: number
public Key: string
public CustomData?: object
public Origin?: TokenOrigin
Serialize(): Uint8Array {
const obj = {
r: this.RelativeQuality,
k: this.Key
};
if (this.CustomData && Object.keys(this.CustomData).length > 0) {
obj['cd'] = this.CustomData
}
if (this.Origin) {
obj['o'] = this.Origin
}
let jsonData = JSON.stringify(obj);
return Utils.stringToUint8Array(jsonData);
}
static DeserializePayload(input: string, secretKey: string, tokenIdentifier: string): PayloadDto {
const headerEncrypted = Base64.decode(input);
const decryptedBytes: Uint8Array = AESEncryption.DecryptPayload(secretKey, tokenIdentifier, headerEncrypted);
const jsonData = JSON.parse(Utils.uint8ArrayToString(decryptedBytes));
if (jsonData == null) return null;
const payload = new PayloadDto();
payload.RelativeQuality = jsonData['r'];
payload.Key = jsonData['k'];
if (jsonData['cd']) {
payload.CustomData = jsonData['cd'];
}
if (jsonData['o']) {
payload.Origin = jsonData['o'];
}
return payload;
}
}