Skip to content

Commit

Permalink
Merge branch 'full-id-service' into client-server-refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
BichraiX authored Jul 22, 2024
2 parents ea687a0 + b0d2ccd commit 88aa4ac
Show file tree
Hide file tree
Showing 29 changed files with 608 additions and 299 deletions.
27 changes: 18 additions & 9 deletions packages/matrix-client-server/src/account/3pid/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ const add = (clientServer: MatrixClientServer): expressAppHandler => {
clientServer.uiauthenticate(req, res, allowedFlows, (obj, userId) => {
validateParameters(res, schema, obj, clientServer.logger, (obj) => {
if (!clientSecretRegex.test((obj as RequestBody).client_secret)) {
send(res, 400, errMsg('invalidParam', 'Invalid client_secret'))
send(
res,
400,
errMsg('invalidParam', 'Invalid client_secret'),
clientServer.logger
)
return
}
if (!sidRegex.test((obj as RequestBody).sid)) {
send(res, 400, errMsg('invalidParam', 'Invalid session ID'))
send(
res,
400,
errMsg('invalidParam', 'Invalid session ID'),
clientServer.logger
)
return
}
const body = obj as RequestBody
Expand All @@ -49,12 +59,12 @@ const add = (clientServer: MatrixClientServer): expressAppHandler => {
)
.then((sessionRows) => {
if (sessionRows.length === 0) {
send(res, 400, errMsg('noValidSession'))
send(res, 400, errMsg('noValidSession'), clientServer.logger)
return
}
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!sessionRows[0].validated_at) {
send(res, 400, errMsg('sessionNotValidated'))
send(res, 400, errMsg('sessionNotValidated'), clientServer.logger)
return
}
clientServer.matrixDb
Expand All @@ -63,7 +73,7 @@ const add = (clientServer: MatrixClientServer): expressAppHandler => {
})
.then((rows) => {
if (rows.length > 0) {
send(res, 400, errMsg('threepidInUse'))
send(res, 400, errMsg('threepidInUse'), clientServer.logger)
} else {
clientServer.matrixDb
.insert('user_threepids', {
Expand All @@ -74,16 +84,15 @@ const add = (clientServer: MatrixClientServer): expressAppHandler => {
added_at: epoch()
})
.then(() => {
send(res, 200, {})
send(res, 200, {}, clientServer.logger)
})
.catch((e) => {
// istanbul ignore next
clientServer.logger.error(
'Error while inserting user_threepids',
e
'Error while inserting user_threepids'
)
// istanbul ignore next
send(res, 400, errMsg('unknown', e))
send(res, 400, errMsg('unknown', e), clientServer.logger)
})
}
})
Expand Down
8 changes: 4 additions & 4 deletions packages/matrix-client-server/src/account/whoami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const whoami = (clientServer: MatrixClientServer): expressAppHandler => {
.then((rows) => {
// istanbul ignore if // might remove the istanbul ignore if an endpoint other than /register modifies the users table
if (rows.length === 0) {
send(res, 403, errMsg('invalidUsername'))
send(res, 403, errMsg('invalidUsername'), clientServer.logger)
return
}
const isGuest = rows[0].is_guest !== 0
Expand All @@ -24,13 +24,13 @@ const whoami = (clientServer: MatrixClientServer): expressAppHandler => {
if (data.device_id) {
body.device_id = data.device_id
}
send(res, 200, body)
send(res, 200, body, clientServer.logger)
})
.catch((e) => {
// istanbul ignore next
clientServer.logger.error('Error while fetching user data', e)
clientServer.logger.error('Error while fetching user data')
// istanbul ignore next
send(res, 500, errMsg('unknown'))
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
}
Expand Down
22 changes: 13 additions & 9 deletions packages/matrix-client-server/src/admin/whois.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,27 @@ const whois = (clientServer: MatrixClientServer): expressAppHandler => {
}
devices[deviceId].sessions.push(sessions[mappings[deviceId]])
})
send(res, 200, {
user_id: prms.userId,
devices
})
send(
res,
200,
{
user_id: prms.userId,
devices
},
clientServer.logger
)
})
.catch((err) => {
.catch((e) => {
// istanbul ignore next
clientServer.logger.error(
'Error retrieving user informations from the MatrixDB',
err
'Error retrieving user informations from the MatrixDB'
)
// istanbul ignore next
send(res, 500, errMsg('unknown'))
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
} else {
send(res, 400, errMsg('missingParams'))
send(res, 400, errMsg('missingParams'), clientServer.logger)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/matrix-client-server/src/devices/changeDevices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ export const changeDeviceName = (
errMsg(
'notFound',
'The current user has no device with the given ID'
)
),
clientServer.logger
)
} else {
clientServer.logger.debug('Device Name updated')
send(res, 200, {})
send(res, 200, {}, clientServer.logger)
}
})
.catch((e) => {
/* istanbul ignore next */
clientServer.logger.error('Error querying profiles:', e)
clientServer.logger.error('Error querying profiles:')
/* istanbul ignore next */
send(res, 500, errMsg('unknown', 'Error querying profiles'))
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
})
Expand Down
30 changes: 19 additions & 11 deletions packages/matrix-client-server/src/devices/getDevices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export const getDevices = (
last_seen_ts: row.last_seen
}
})
send(res, 200, { devices: _devices })
send(res, 200, { devices: _devices }, clientServer.logger)
})
.catch((e) => {
/* istanbul ignore next */
clientServer.logger.error('Error querying devices:', e)
clientServer.logger.error('Error querying devices')
/* istanbul ignore next */
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
}
Expand All @@ -65,22 +67,28 @@ export const getDeviceInfo = (
errMsg(
'notFound',
'The current user has no device with the given ID'
)
),
clientServer.logger
)
} else {
send(res, 200, {
device_id: deviceId,
display_name: rows[0].display_name,
last_seen_ip: rows[0].ip,
last_seen_ts: rows[0].last_seen
})
send(
res,
200,
{
device_id: deviceId,
display_name: rows[0].display_name,
last_seen_ip: rows[0].ip,
last_seen_ts: rows[0].last_seen
},
clientServer.logger
)
}
})
.catch((e) => {
/* istanbul ignore next */
clientServer.logger.error('Error querying devices:', e)
clientServer.logger.error('Error querying devices:')
/* istanbul ignore next */
send(res, 500, errMsg('unknown', 'Error querying devices'))
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/matrix-client-server/src/login/getLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type MatrixClientServer from '..'

const getLogin = (clientServer: MatrixClientServer): expressAppHandler => {
return (req, res) => {
send(res, 200, clientServer.conf.login_flows)
send(res, 200, clientServer.conf.login_flows, clientServer.logger)
}
}

Expand Down
12 changes: 8 additions & 4 deletions packages/matrix-client-server/src/login/postLogin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
errMsg,
expressAppHandler,
type expressAppHandler,
jsonContent,
send,
validateParameters
} from '@twake/utils'
import MatrixClientServer from '..'
import type MatrixClientServer from '..'
import { type UserIdentifier } from '../types'

interface LoginRequestBody {
Expand Down Expand Up @@ -37,8 +37,12 @@ const postLogin = (clientServer: MatrixClientServer): expressAppHandler => {
case 'm.login.password':
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!body.password) {
clientServer.logger.error('Missing password')
send(res, 400, errMsg('missingParam', 'password'))
send(
res,
400,
errMsg('missingParam', 'Missing password'),
clientServer.logger
)
// return
}
}
Expand Down
48 changes: 29 additions & 19 deletions packages/matrix-client-server/src/presence/getStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ const getStatus = (clientServer: MatrixClientServer): expressAppHandler => {
// @ts-expect-error
const userId: string = req.params.userId as string
if (!matrixIdRegex.test(userId)) {
clientServer.logger.warn('Invalid user ID')
send(res, 400, errMsg('invalidParam'))
send(
res,
400,
errMsg('invalidParam', 'Invalid user ID'),
clientServer.logger
)
} else {
clientServer.authenticate(req, res, (data, id) => {
clientServer.matrixDb
Expand All @@ -20,29 +24,35 @@ const getStatus = (clientServer: MatrixClientServer): expressAppHandler => {
})
.then((rows) => {
if (rows.length === 0) {
clientServer.logger.error('No presence state for this user')
send(res, 404, {
errcode: 'M_UNKNOWN',
error:
'There is no presence state for this user. This user may not exist or isn’t exposing presence information to you.'
})
send(
res,
404,
{
errcode: 'M_UNKNOWN',
error:
'There is no presence state for this user. This user may not exist or isn’t exposing presence information to you.'
},
clientServer.logger
)
} else {
send(res, 200, {
currently_active: rows[0].state === 'online',
last_active_ts: epoch() - (rows[0].mtime as number), // TODO : Check if mtime corresponds to last_active_ts, not clear in the spec
state: rows[0].state,
status_msg: rows[0].status_msg
})
send(
res,
200,
{
currently_active: rows[0].state === 'online',
last_active_ts: epoch() - (rows[0].mtime as number), // TODO : Check if mtime corresponds to last_active_ts, not clear in the spec
state: rows[0].state,
status_msg: rows[0].status_msg
},
clientServer.logger
)
}
})
.catch((e) => {
// istanbul ignore next
clientServer.logger.error(
"Error retrieving user's presence state",
e
)
clientServer.logger.error("Error retrieving user's presence state")
// istanbul ignore next
send(res, 500, errMsg('unknown'))
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
}
Expand Down
17 changes: 10 additions & 7 deletions packages/matrix-client-server/src/presence/putStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ const putStatus = (clientServer: MatrixClientServer): expressAppHandler => {
// @ts-expect-error
const userId: string = req.params.userId as string
if (!matrixIdRegex.test(userId)) {
clientServer.logger.warn('Invalid user ID')
send(res, 400, errMsg('invalidParam'))
send(
res,
400,
errMsg('invalidParam', 'Invalid user ID'),
clientServer.logger
)
} else {
clientServer.authenticate(req, res, (data, id) => {
jsonContent(req, res, clientServer.logger, (obj) => {
Expand All @@ -39,7 +43,7 @@ const putStatus = (clientServer: MatrixClientServer): expressAppHandler => {
clientServer.logger.warn(
'You cannot set the presence state of another user'
)
send(res, 403, errMsg('forbidden'))
send(res, 403, errMsg('forbidden'), clientServer.logger)
return
}
if (
Expand Down Expand Up @@ -68,16 +72,15 @@ const putStatus = (clientServer: MatrixClientServer): expressAppHandler => {
[{ field: 'user_id', value: userId }]
)
.then(() => {
send(res, 200, {})
send(res, 200, {}, clientServer.logger)
})
.catch((e) => {
// istanbul ignore next
clientServer.logger.error(
"Error updating user's presence state",
e
"Error updating user's presence state"
)
// istanbul ignore next
send(res, 500, errMsg('unknown'))
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
})
Expand Down
Loading

0 comments on commit 88aa4ac

Please sign in to comment.