Add creation of admin user mapping

This commit is contained in:
Henrik Hüttemann 2023-09-14 16:14:22 +02:00
parent 806d41cc09
commit b141488f16
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
3 changed files with 30 additions and 17 deletions

View File

@ -1,3 +1,4 @@
REGISTRATION_SHARED_SECRET='look in your synapses homeserver.yaml' REGISTRATION_SHARED_SECRET='look in your synapses homeserver.yaml'
AS_TOKEN='look in the app-service.yaml' AS_TOKEN='look in the app-service.yaml'
EXCLUDED_USERS='rocket.cat' # Comma-separated list of usernames or IDs EXCLUDED_USERS='rocket.cat' # Comma-separated list of usernames or IDs
ADMIN_USERNAME='admin' # The login name of the Rocket.Chat admin

View File

@ -1,8 +1,10 @@
process.env.REGISTRATION_SHARED_SECRET = 'ThisIsSoSecretWow' process.env.REGISTRATION_SHARED_SECRET = 'ThisIsSoSecretWow'
process.env.EXCLUDED_USERS = 'excludedUser1,excludedUser2' process.env.EXCLUDED_USERS = 'excludedUser1,excludedUser2'
process.env.ADMIN_USERNAME = 'testAdmin'
import { expect, jest, test } from '@jest/globals' import { expect, jest, test } from '@jest/globals'
import axios from 'axios' import axios from 'axios'
import * as storage from '../helpers/storage' import { Entity, entities } from '../Entities'
import { IdMapping } from '../entity/IdMapping'
import { import {
MatrixUser, MatrixUser,
RcUser, RcUser,
@ -12,8 +14,7 @@ import {
mapUser, mapUser,
userIsExcluded, userIsExcluded,
} from '../handlers/users' } from '../handlers/users'
import { IdMapping } from '../entity/IdMapping' import * as storage from '../helpers/storage'
import { Entity, entities } from '../Entities'
jest.mock('axios') jest.mock('axios')
const mockedAxios = axios as jest.Mocked<typeof axios> const mockedAxios = axios as jest.Mocked<typeof axios>

View File

@ -1,9 +1,10 @@
import { createHmac } from 'node:crypto' import { createHmac } from 'node:crypto'
import log from '../helpers/logger'
import { axios } from '../helpers/synapse'
import { createMembership, getUserId, save } from '../helpers/storage'
import { IdMapping } from '../entity/IdMapping'
import { Entity, entities } from '../Entities' import { Entity, entities } from '../Entities'
import adminAccessToken from '../config/synapse_access_token.json'
import { IdMapping } from '../entity/IdMapping'
import log from '../helpers/logger'
import { createMembership, getUserId, save } from '../helpers/storage'
import { axios } from '../helpers/synapse'
export type RcUser = { export type RcUser = {
_id: string _id: string
@ -41,15 +42,22 @@ export function mapUser(rcUser: RcUser): MatrixUser {
} }
} }
const registration_shared_secret = process.env.REGISTRATION_SHARED_SECRET || '' const registrationSharedSecret = process.env.REGISTRATION_SHARED_SECRET || ''
if (!registration_shared_secret) { if (!registrationSharedSecret) {
const message = 'No REGISTRATION_SHARED_SECRET found in .env.' const message = 'No REGISTRATION_SHARED_SECRET found in .env.'
log.error(message) log.error(message)
throw new Error(message) throw new Error(message)
} }
const adminUsername = process.env.ADMIN_USERNAME || ''
if (!adminUsername) {
const message = 'No ADMIN_USERNAME found in .env.'
log.error(message)
throw new Error(message)
}
export function generateHmac(user: MatrixUser): string { export function generateHmac(user: MatrixUser): string {
const hmac = createHmac('sha1', registration_shared_secret) const hmac = createHmac('sha1', registrationSharedSecret)
hmac.write( hmac.write(
`${user.nonce}\0${user.username}\0${user.password}\0${ `${user.nonce}\0${user.username}\0${user.password}\0${
user.admin ? 'admin' : 'notadmin' user.admin ? 'admin' : 'notadmin'
@ -87,7 +95,7 @@ export function userIsExcluded(rcUser: RcUser): boolean {
reasons.push(`username "${rcUser.username}" is on exclusion list`) reasons.push(`username "${rcUser.username}" is on exclusion list`)
if (reasons.length > 0) { if (reasons.length > 0) {
log.debug(`User ${rcUser.name} is excluded: ${reasons.join(', ')}`) log.warn(`User ${rcUser.name} is excluded: ${reasons.join(', ')}`)
return true return true
} }
return false return false
@ -124,15 +132,18 @@ export async function createUser(rcUser: RcUser): Promise<MatrixUser> {
export async function handle(rcUser: RcUser): Promise<void> { export async function handle(rcUser: RcUser): Promise<void> {
log.info(`Parsing user: ${rcUser.name}: ${rcUser._id}`) log.info(`Parsing user: ${rcUser.name}: ${rcUser._id}`)
if (userIsExcluded(rcUser)) {
return undefined
}
const matrixId = await getUserId(rcUser._id) const matrixId = await getUserId(rcUser._id)
if (matrixId) { if (matrixId) {
log.debug(`Mapping exists: ${rcUser._id} -> ${matrixId}`) log.debug(`Mapping exists: ${rcUser._id} -> ${matrixId}`)
} else { } else {
const matrixUser = await createUser(rcUser) if (rcUser.username === adminUsername) {
await createMapping(rcUser._id, matrixUser) log.info(
`User ${rcUser.username} is defined as admin in ENV, mapping as such`
)
await createMapping(rcUser._id, adminAccessToken as unknown as MatrixUser)
} else if (!userIsExcluded(rcUser)) {
const matrixUser = await createUser(rcUser)
await createMapping(rcUser._id, matrixUser)
}
} }
} }