3 const assert = require('assert');
4 const util = require('util');
5 const crypto = require('crypto');
6 const request = util.promisify(require('request'));
7 const urlJoin = require('url-join');
9 const env = require('gitter-web-env');
10 const config = env.config;
12 const homeserverUrl = config.get('matrix:bridge:homeserverUrl');
13 assert(homeserverUrl, 'matrix:bridge:homeserverUrl must be defined');
14 const testSynapseRegistrationSharedSecret = config.get(
15 'matrix:bridge:testSynapseRegistrationSharedSecret'
18 testSynapseRegistrationSharedSecret,
19 'matrix:bridge:testSynapseRegistrationSharedSecret must be defined'
22 // This is based on https://github.com/matrix-org/synapse/blob/4d624f467a6c252b9295e46b8b83f36b1f0d3d45/synapse/_scripts/register_new_matrix_user.py#L27-L91
23 // Also see https://github.com/matrix-org/synapse/blob/develop/docs/admin_api/user_admin_api.rst#create-or-modify-account
24 async function registerTestSynapseUser(localPart) {
25 const registerUrl = urlJoin(homeserverUrl, `/_synapse/admin/v1/register`);
27 const registerGetRes = await request({
32 'Content-Type': 'application/json'
37 if (registerGetRes.statusCode !== 200) {
39 `registerTestSynapseUser failed to fetch nonce, localPart=${localPart} statusCode=${
40 registerGetRes.statusCode
41 }, body=${JSON.stringify(registerGetRes.body)}`
45 const nonce = registerGetRes.body.nonce;
49 const password = crypto.randomBytes(20).toString('hex');
52 .createHmac('sha1', testSynapseRegistrationSharedSecret)
59 .update(admin ? 'admin' : 'notadmin')
61 //.update(toUtf8(user_type))
64 const userCreateRes = await request({
69 'Content-Type': 'application/json'
77 //user_type: user_type
81 if (userCreateRes.statusCode < 200 || userCreateRes.statusCode >= 300) {
83 `registerTestSynapseUser failed to create user, localPart=${localPart} statusCode=${
84 userCreateRes.statusCode
85 }, body=${JSON.stringify(userCreateRes.body)}`
89 return userCreateRes.body;
92 module.exports = registerTestSynapseUser;