Merge pull request 'Save user access token' (#18) from store-access-token into main
Reviewed-on: https://git.verdigado.com/NB-Public/rocketchat2matrix/pulls/18
This commit is contained in:
commit
ac5da7dd01
@ -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)
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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')
|
||||
|
||||
16
src/users.ts
16
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<string> {
|
||||
return (await axios.get('/_synapse/admin/v1/register')).data.nonce
|
||||
}
|
||||
|
||||
async function registerUser(user: MatrixUser): Promise<string> {
|
||||
return (await axios.post('/_synapse/admin/v1/register', user)).data.user_id
|
||||
async function registerUser(user: MatrixUser): Promise<AccessToken> {
|
||||
return (await axios.post('/_synapse/admin/v1/register', user)).data
|
||||
}
|
||||
|
||||
export async function createUser(rcUser: RcUser): Promise<MatrixUser> {
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user