diff --git a/src/app.ts b/src/app.ts index a2211b1..aed672d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -57,6 +57,7 @@ async function loadRcExport(entity: Entities) { mapping.rcId = rcUser._id mapping.matrixId = matrixUser.user_id mapping.type = 0 + mapping.accessToken = matrixUser.access_token AppDataSource.manager.save(mapping) log.debug('Mapping added:', mapping) diff --git a/src/entity/IdMapping.ts b/src/entity/IdMapping.ts index 20fef3d..f2c00d4 100644 --- a/src/entity/IdMapping.ts +++ b/src/entity/IdMapping.ts @@ -3,11 +3,14 @@ import { Entity, Column, PrimaryColumn } from 'typeorm' @Entity() export class IdMapping { @PrimaryColumn() - rcId!: string + rcId!: string // Rocket.Chat ID of the entity @Column() - matrixId?: string + matrixId?: string // Matrix ID of the entity @Column('integer') - type!: number + type!: number // Type of the entity; 0 = user, 1 = room, 2 = message + + @Column() + accessToken?: string // Access token for matrix users } diff --git a/src/users.test.ts b/src/users.test.ts index 82549d1..2849020 100644 --- a/src/users.test.ts +++ b/src/users.test.ts @@ -34,16 +34,18 @@ test('generating correct hmac', () => { test('creating users', async () => { const matrixId = 'TestRandomId' + const accessToken = 'secretaccesstoken' mockedAxios.get.mockResolvedValue({ data: { nonce: nonce } }) mockedAxios.post.mockResolvedValue({ - data: { user_id: matrixId }, + data: { user_id: matrixId, access_token: accessToken }, }) const createdUser = await createUser(rcUser) expect(createdUser).toStrictEqual({ ...matrixUser, user_id: matrixId, + access_token: accessToken, }) expect(mockedAxios.get).toHaveBeenCalledWith('/_synapse/admin/v1/register') diff --git a/src/users.ts b/src/users.ts index 7ca2ae6..0c4e02d 100644 --- a/src/users.ts +++ b/src/users.ts @@ -18,6 +18,14 @@ export type MatrixUser = { admin: boolean nonce?: string mac?: string + access_token?: string +} + +export type AccessToken = { + access_token: string + device_id: string + home_server: string + user_id: string } export function mapUser(rcUser: RcUser): MatrixUser { @@ -52,15 +60,17 @@ async function getUserRegistrationNonce(): Promise { return (await axios.get('/_synapse/admin/v1/register')).data.nonce } -async function registerUser(user: MatrixUser): Promise { - return (await axios.post('/_synapse/admin/v1/register', user)).data.user_id +async function registerUser(user: MatrixUser): Promise { + return (await axios.post('/_synapse/admin/v1/register', user)).data } export async function createUser(rcUser: RcUser): Promise { const user = mapUser(rcUser) user.nonce = await getUserRegistrationNonce() user.mac = generateHmac(user) - user.user_id = await registerUser(user) + const accessToken = await registerUser(user) + user.user_id = accessToken.user_id + user.access_token = accessToken.access_token log.info(`User ${rcUser.username} created:`, user) delete user.nonce