Implement user registration functions

This commit is contained in:
Henrik Hüttemann 2023-05-24 18:01:08 +02:00
parent a1a50d44e9
commit 62d6b5b30b
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
3 changed files with 60 additions and 6 deletions

View File

@ -6,7 +6,7 @@ services:
image: docker.io/matrixdotorg/synapse:latest image: docker.io/matrixdotorg/synapse:latest
# Since synapse does not retry to connect to the database, restart upon # Since synapse does not retry to connect to the database, restart upon
# failure # failure
restart: unless-stopped restart: "no"
# See the readme for a full documentation of the environment settings # See the readme for a full documentation of the environment settings
# NOTE: You must edit homeserver.yaml to use postgres, it defaults to sqlite # NOTE: You must edit homeserver.yaml to use postgres, it defaults to sqlite
environment: environment:
@ -46,4 +46,4 @@ services:
image: awesometechnologies/synapse-admin:latest image: awesometechnologies/synapse-admin:latest
ports: ports:
- "8080:80" - "8080:80"
restart: unless-stopped restart: "no"

View File

@ -35,17 +35,17 @@ function loadRcExport(entity: Entities): Promise<void> {
} }
let userMapping = storage.users.find((e) => e.rcId === rcUser._id) // Lookup mapping let userMapping = storage.users.find((e) => e.rcId === rcUser._id) // Lookup mapping
if (userMapping) { if (userMapping && userMapping.matrixId) {
log.debug('Mapping exists:', userMapping) log.debug('Mapping exists:', userMapping)
} else { } else {
userMapping = { userMapping = {
rcId: rcUser._id, rcId: rcUser._id,
matrixId: `@${rcUser.username}:localhost`, matrixId: `@${rcUser.username}:localhost`, // Create user on synapse
} }
storage.users.push(userMapping) // Save new mapping storage.users.push(userMapping) // Save new mapping
log.debug('Mapping added:', userMapping) log.debug('Mapping added:', userMapping)
// Add user to room mapping // Add user to room mapping (specific to users)
rcUser.__rooms.forEach((rcRoomId: string) => { rcUser.__rooms.forEach((rcRoomId: string) => {
const roomIndex = storage.rooms.findIndex( const roomIndex = storage.rooms.findIndex(
(e) => e.rcId === rcRoomId (e) => e.rcId === rcRoomId
@ -91,7 +91,7 @@ async function main() {
await loadRcExport(Entities.Users) await loadRcExport(Entities.Users)
log.info('Done.') log.info('Done.')
} catch (error) { } catch (error) {
log.error(`Encountered an error booting up`) log.error(`Encountered an error while booting up`)
} }
} }

View File

@ -1,3 +1,6 @@
import { axios } from './synapse'
import { createHmac } from 'node:crypto'
export type RcUser = { export type RcUser = {
username: string username: string
name: string name: string
@ -5,3 +8,54 @@ export type RcUser = {
_id: string _id: string
__rooms: string[] __rooms: string[]
} }
export type MatrixUser = {
user_id?: string
nonce?: string
username: string
displayname: string
password?: string
admin: boolean
mac?: string
}
export function mapUser(rcUser: RcUser): MatrixUser {
return {
user_id: '',
username: rcUser.username,
displayname: rcUser.name,
password: '',
admin: rcUser.roles.includes('admin'),
}
}
const registration_shared_secret =
'vkq7zfBDt4A1NmMN6jJ*g+,G~.R:QuC_xI:~7~jQ_6kJ6O~JrG'
function generateHmac(user: MatrixUser): string {
const hmac = createHmac('sha1', registration_shared_secret)
hmac.write(
`${user.nonce}\0${user.username}\0${user.password}\0${
user.admin ? 'admin' : 'notadmin'
}`
)
hmac.end()
return hmac.read().toString('hex')
}
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
}
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)
return user
}