Implement user registration functions
This commit is contained in:
parent
a1a50d44e9
commit
62d6b5b30b
@ -6,7 +6,7 @@ services:
|
||||
image: docker.io/matrixdotorg/synapse:latest
|
||||
# Since synapse does not retry to connect to the database, restart upon
|
||||
# failure
|
||||
restart: unless-stopped
|
||||
restart: "no"
|
||||
# See the readme for a full documentation of the environment settings
|
||||
# NOTE: You must edit homeserver.yaml to use postgres, it defaults to sqlite
|
||||
environment:
|
||||
@ -46,4 +46,4 @@ services:
|
||||
image: awesometechnologies/synapse-admin:latest
|
||||
ports:
|
||||
- "8080:80"
|
||||
restart: unless-stopped
|
||||
restart: "no"
|
||||
|
||||
@ -35,17 +35,17 @@ function loadRcExport(entity: Entities): Promise<void> {
|
||||
}
|
||||
|
||||
let userMapping = storage.users.find((e) => e.rcId === rcUser._id) // Lookup mapping
|
||||
if (userMapping) {
|
||||
if (userMapping && userMapping.matrixId) {
|
||||
log.debug('Mapping exists:', userMapping)
|
||||
} else {
|
||||
userMapping = {
|
||||
rcId: rcUser._id,
|
||||
matrixId: `@${rcUser.username}:localhost`,
|
||||
matrixId: `@${rcUser.username}:localhost`, // Create user on synapse
|
||||
}
|
||||
storage.users.push(userMapping) // Save new mapping
|
||||
log.debug('Mapping added:', userMapping)
|
||||
|
||||
// Add user to room mapping
|
||||
// Add user to room mapping (specific to users)
|
||||
rcUser.__rooms.forEach((rcRoomId: string) => {
|
||||
const roomIndex = storage.rooms.findIndex(
|
||||
(e) => e.rcId === rcRoomId
|
||||
@ -91,7 +91,7 @@ async function main() {
|
||||
await loadRcExport(Entities.Users)
|
||||
log.info('Done.')
|
||||
} catch (error) {
|
||||
log.error(`Encountered an error booting up`)
|
||||
log.error(`Encountered an error while booting up`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
54
src/users.ts
54
src/users.ts
@ -1,3 +1,6 @@
|
||||
import { axios } from './synapse'
|
||||
import { createHmac } from 'node:crypto'
|
||||
|
||||
export type RcUser = {
|
||||
username: string
|
||||
name: string
|
||||
@ -5,3 +8,54 @@ export type RcUser = {
|
||||
_id: 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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user