This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
SynapseUtils.ts
61 lines (47 loc) · 1.57 KB
/
SynapseUtils.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
// Copyright (c) 2021. Sendanor <info@sendanor.fi>. All rights reserved.
import { createHmac } from 'crypto';
import { SynapseRegisterRequestDTO } from "../types/synapse/SynapseRegisterRequestDTO";
import { MatrixRegisterKind } from "../types/request/register/types/MatrixRegisterKind";
export class SynapseUtils {
public static createRegisterDTO (
sharedSecret : string,
nonce : string,
username : string,
displayName : string,
password : string,
admin : boolean,
userType ?: MatrixRegisterKind
) : SynapseRegisterRequestDTO {
const mac = this.generateRegisterMAC(sharedSecret, nonce, username, password, admin, userType);
return {
nonce,
username,
'displayname': displayName,
password,
admin,
mac
};
}
public static generateRegisterMAC (
sharedSecret : string,
nonce : string,
username : string,
password : string,
admin : boolean,
userType ?: MatrixRegisterKind
) : string {
let mac = createHmac('sha1', sharedSecret)
.update(nonce, 'utf8')
.update('\x00')
.update(username, 'utf8')
.update('\x00')
.update(password, 'utf8')
.update('\x00')
.update( admin ? 'admin' : 'notadmin')
;
if (userType) {
mac = mac.update('\x00').update(userType, 'utf8');
}
return mac.digest('hex');
}
}