Save user access token
This commit is contained in:
parent
5e59481e35
commit
ad034d0343
@ -57,6 +57,7 @@ async function loadRcExport(entity: Entities) {
|
|||||||
mapping.rcId = rcUser._id
|
mapping.rcId = rcUser._id
|
||||||
mapping.matrixId = matrixUser.user_id
|
mapping.matrixId = matrixUser.user_id
|
||||||
mapping.type = 0
|
mapping.type = 0
|
||||||
|
mapping.accessToken = matrixUser.access_token
|
||||||
|
|
||||||
AppDataSource.manager.save(mapping)
|
AppDataSource.manager.save(mapping)
|
||||||
log.debug('Mapping added:', mapping)
|
log.debug('Mapping added:', mapping)
|
||||||
|
|||||||
@ -3,11 +3,14 @@ import { Entity, Column, PrimaryColumn } from 'typeorm'
|
|||||||
@Entity()
|
@Entity()
|
||||||
export class IdMapping {
|
export class IdMapping {
|
||||||
@PrimaryColumn()
|
@PrimaryColumn()
|
||||||
rcId!: string
|
rcId!: string // Rocket.Chat ID of the entity
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
matrixId?: string
|
matrixId?: string // Matrix ID of the entity
|
||||||
|
|
||||||
@Column('integer')
|
@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 () => {
|
test('creating users', async () => {
|
||||||
const matrixId = 'TestRandomId'
|
const matrixId = 'TestRandomId'
|
||||||
|
const accessToken = 'secretaccesstoken'
|
||||||
|
|
||||||
mockedAxios.get.mockResolvedValue({ data: { nonce: nonce } })
|
mockedAxios.get.mockResolvedValue({ data: { nonce: nonce } })
|
||||||
mockedAxios.post.mockResolvedValue({
|
mockedAxios.post.mockResolvedValue({
|
||||||
data: { user_id: matrixId },
|
data: { user_id: matrixId, access_token: accessToken },
|
||||||
})
|
})
|
||||||
|
|
||||||
const createdUser = await createUser(rcUser)
|
const createdUser = await createUser(rcUser)
|
||||||
expect(createdUser).toStrictEqual({
|
expect(createdUser).toStrictEqual({
|
||||||
...matrixUser,
|
...matrixUser,
|
||||||
user_id: matrixId,
|
user_id: matrixId,
|
||||||
|
access_token: accessToken,
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(mockedAxios.get).toHaveBeenCalledWith('/_synapse/admin/v1/register')
|
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
|
admin: boolean
|
||||||
nonce?: string
|
nonce?: string
|
||||||
mac?: 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 {
|
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
|
return (await axios.get('/_synapse/admin/v1/register')).data.nonce
|
||||||
}
|
}
|
||||||
|
|
||||||
async function registerUser(user: MatrixUser): Promise<string> {
|
async function registerUser(user: MatrixUser): Promise<AccessToken> {
|
||||||
return (await axios.post('/_synapse/admin/v1/register', user)).data.user_id
|
return (await axios.post('/_synapse/admin/v1/register', user)).data
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createUser(rcUser: RcUser): Promise<MatrixUser> {
|
export async function createUser(rcUser: RcUser): Promise<MatrixUser> {
|
||||||
const user = mapUser(rcUser)
|
const user = mapUser(rcUser)
|
||||||
user.nonce = await getUserRegistrationNonce()
|
user.nonce = await getUserRegistrationNonce()
|
||||||
user.mac = generateHmac(user)
|
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)
|
log.info(`User ${rcUser.username} created:`, user)
|
||||||
|
|
||||||
delete user.nonce
|
delete user.nonce
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user