From 97093546cad8524abd1d567a8e223a9eee1dc285 Mon Sep 17 00:00:00 2001 From: BichraiX <160038319+BichraiX@users.noreply.github.com> Date: Mon, 22 Jul 2024 08:41:22 +0100 Subject: [PATCH] feat : added loggers to send calls and renamed fillTables to fillTablesAndSend --- .../account/password/email/requestToken.ts | 55 +++++++++---- .../account/password/msisdn/requestToken.ts | 67 ++++++++++++---- .../src/register/email/requestToken.ts | 66 +++++++++++----- .../src/register/msisdn/requestToken.ts | 78 ++++++++++++++----- .../src/validate/email/requestToken.ts | 6 +- 5 files changed, 201 insertions(+), 71 deletions(-) diff --git a/packages/matrix-client-server/src/account/password/email/requestToken.ts b/packages/matrix-client-server/src/account/password/email/requestToken.ts index 808b1118..e93e51d8 100644 --- a/packages/matrix-client-server/src/account/password/email/requestToken.ts +++ b/packages/matrix-client-server/src/account/password/email/requestToken.ts @@ -10,7 +10,7 @@ import { import type MatrixClientServer from '../../../index' import Mailer from '../../../utils/mailer' import { - fillTable, + fillTableAndSend, getSubmitUrl, preConfigureTemplate } from '../../../register/email/requestToken' @@ -55,23 +55,38 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { const dst = (obj as RequestTokenArgs).email const nextLink = (obj as RequestTokenArgs).next_link if (!clientSecretRe.test(clientSecret)) { - send(res, 400, errMsg('invalidParam', 'invalid client_secret')) + send( + res, + 400, + errMsg('invalidParam', 'invalid client_secret'), + clientServer.logger + ) } else if (!validEmailRe.test(dst)) { - send(res, 400, errMsg('invalidEmail')) + send(res, 400, errMsg('invalidEmail'), clientServer.logger) // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions } else if (nextLink && !isValidUrl(nextLink)) { - send(res, 400, errMsg('invalidParam', 'invalid next_link')) + send( + res, + 400, + errMsg('invalidParam', 'invalid next_link'), + clientServer.logger + ) } else if ( typeof sendAttempt !== 'number' || sendAttempt > maxAttemps ) { - send(res, 400, errMsg('invalidParam', 'Invalid send attempt')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid send attempt'), + clientServer.logger + ) } else { clientServer.matrixDb .get('user_threepids', ['user_id'], { address: dst }) .then((rows) => { if (rows.length === 0) { - send(res, 400, errMsg('threepidNotFound')) + send(res, 400, errMsg('threepidNotFound'), clientServer.logger) } else { clientServer.matrixDb .get( @@ -85,10 +100,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { .then((rows) => { if (rows.length > 0) { if (sendAttempt === rows[0].last_send_attempt) { - send(res, 200, { - sid: rows[0].session_id, - submit_url: getSubmitUrl(clientServer.conf) - }) + send( + res, + 200, + { + sid: rows[0].session_id, + submit_url: getSubmitUrl(clientServer.conf) + }, + clientServer.logger + ) } else { clientServer.matrixDb .deleteWhere('threepid_validation_session', [ @@ -104,7 +124,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { } ]) .then(() => { - fillTable( + fillTableAndSend( // The calls to send are made in this function clientServer, dst, @@ -121,11 +141,16 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { // istanbul ignore next clientServer.logger.error('Deletion error', err) // istanbul ignore next - send(res, 500, errMsg('unknown', err)) + send( + res, + 500, + errMsg('unknown', err), + clientServer.logger + ) }) } } else { - fillTable( + fillTableAndSend( // The calls to send are made in this function clientServer, dst, @@ -143,7 +168,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { /* istanbul ignore next */ clientServer.logger.error('Send_attempt error', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } }) @@ -151,7 +176,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { /* istanbul ignore next */ clientServer.logger.error('Error getting userID :', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } }) diff --git a/packages/matrix-client-server/src/account/password/msisdn/requestToken.ts b/packages/matrix-client-server/src/account/password/msisdn/requestToken.ts index 34daae29..3c1d287b 100644 --- a/packages/matrix-client-server/src/account/password/msisdn/requestToken.ts +++ b/packages/matrix-client-server/src/account/password/msisdn/requestToken.ts @@ -12,7 +12,7 @@ import type MatrixClientServer from '../../../index' import SmsSender from '../../../utils/smsSender' import { getSubmitUrl } from '../../../register/email/requestToken' import { - fillTable, + fillTableAndSend, formatPhoneNumber, preConfigureTemplate } from '../../../register/msisdn/requestToken' @@ -61,25 +61,50 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { const dst = formatPhoneNumber(phoneNumber, country) const nextLink = (obj as RequestTokenArgs).next_link if (!clientSecretRegex.test(clientSecret)) { - send(res, 400, errMsg('invalidParam', 'Invalid client_secret')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid client_secret'), + clientServer.logger + ) } else if (!validCountryRegex.test(country)) { - send(res, 400, errMsg('invalidParam', 'Invalid country')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid country'), + clientServer.logger + ) // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions } else if (nextLink && !isValidUrl(nextLink)) { - send(res, 400, errMsg('invalidParam', 'Invalid next_link')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid next_link'), + clientServer.logger + ) } else if (!validPhoneNumberRegex.test(dst)) { - send(res, 400, errMsg('invalidParam', 'Invalid phone number')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid phone number'), + clientServer.logger + ) } else if ( typeof sendAttempt !== 'number' || sendAttempt > maxAttemps ) { - send(res, 400, errMsg('invalidParam', 'Invalid send attempt')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid send attempt'), + clientServer.logger + ) } else { clientServer.matrixDb .get('user_threepids', ['user_id'], { address: dst }) .then((rows) => { if (rows.length === 0) { - send(res, 400, errMsg('threepidNotFound')) + send(res, 400, errMsg('threepidNotFound'), clientServer.logger) } else { clientServer.matrixDb .get( @@ -93,10 +118,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { .then((rows) => { if (rows.length > 0) { if (sendAttempt === rows[0].last_send_attempt) { - send(res, 200, { - sid: rows[0].session_id, - submit_url: getSubmitUrl(clientServer.conf) - }) + send( + res, + 200, + { + sid: rows[0].session_id, + submit_url: getSubmitUrl(clientServer.conf) + }, + clientServer.logger + ) } else { clientServer.matrixDb .deleteWhere('threepid_validation_session', [ @@ -112,7 +142,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { } ]) .then(() => { - fillTable( + fillTableAndSend( // The calls to send are made in this function clientServer, dst, @@ -129,11 +159,16 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { // istanbul ignore next clientServer.logger.error('Deletion error:', err) // istanbul ignore next - send(res, 500, errMsg('unknown', err)) + send( + res, + 500, + errMsg('unknown', err), + clientServer.logger + ) }) } } else { - fillTable( + fillTableAndSend( // The calls to send are made in this function clientServer, dst, @@ -151,7 +186,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { /* istanbul ignore next */ clientServer.logger.error('Send_attempt error:', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } }) @@ -159,7 +194,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { /* istanbul ignore next */ clientServer.logger.error('Error getting userID :', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } }) diff --git a/packages/matrix-client-server/src/register/email/requestToken.ts b/packages/matrix-client-server/src/register/email/requestToken.ts index dd21eb04..6ac1adc7 100644 --- a/packages/matrix-client-server/src/register/email/requestToken.ts +++ b/packages/matrix-client-server/src/register/email/requestToken.ts @@ -91,7 +91,7 @@ export const mailBody = ( ) } -export const fillTable = ( +export const fillTableAndSend = ( clientServer: MatrixClientServer, dst: string, clientSecret: string, @@ -118,20 +118,25 @@ export const fillTable = ( last_send_attempt: sendAttempt }) .then(() => { - send(res, 200, { sid, submit_url: getSubmitUrl(clientServer.conf) }) + send( + res, + 200, + { sid, submit_url: getSubmitUrl(clientServer.conf) }, + clientServer.logger + ) }) .catch((err) => { // istanbul ignore next clientServer.logger.error('Insertion error', err) // istanbul ignore next - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) }) .catch((err) => { /* istanbul ignore next */ clientServer.logger.error('Token error', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } @@ -152,23 +157,38 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { const dst = (obj as RequestTokenArgs).email const nextLink = (obj as RequestTokenArgs).next_link if (!clientSecretRe.test(clientSecret)) { - send(res, 400, errMsg('invalidParam', 'invalid client_secret')) + send( + res, + 400, + errMsg('invalidParam', 'invalid client_secret'), + clientServer.logger + ) } else if (!validEmailRe.test(dst)) { - send(res, 400, errMsg('invalidEmail')) + send(res, 400, errMsg('invalidEmail'), clientServer.logger) // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions } else if (nextLink && !isValidUrl(nextLink)) { - send(res, 400, errMsg('invalidParam', 'invalid next_link')) + send( + res, + 400, + errMsg('invalidParam', 'invalid next_link'), + clientServer.logger + ) } else if ( typeof sendAttempt !== 'number' || sendAttempt > maxAttemps ) { - send(res, 400, errMsg('invalidParam', 'Invalid send attempt')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid send attempt'), + clientServer.logger + ) } else { clientServer.matrixDb .get('user_threepids', ['user_id'], { address: dst }) .then((rows) => { if (rows.length > 0) { - send(res, 400, errMsg('threepidInUse')) + send(res, 400, errMsg('threepidInUse'), clientServer.logger) } else { clientServer.matrixDb .get( @@ -182,10 +202,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { .then((rows) => { if (rows.length > 0) { if (sendAttempt === rows[0].last_send_attempt) { - send(res, 200, { - sid: rows[0].session_id, - submit_url: getSubmitUrl(clientServer.conf) - }) + send( + res, + 200, + { + sid: rows[0].session_id, + submit_url: getSubmitUrl(clientServer.conf) + }, + clientServer.logger + ) } else { clientServer.matrixDb .deleteWhere('threepid_validation_session', [ @@ -201,7 +226,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { } ]) .then(() => { - fillTable( + fillTableAndSend( // The calls to send are made in this function clientServer, dst, @@ -218,11 +243,16 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { // istanbul ignore next clientServer.logger.error('Deletion error', err) // istanbul ignore next - send(res, 500, errMsg('unknown', err)) + send( + res, + 500, + errMsg('unknown', err), + clientServer.logger + ) }) } } else { - fillTable( + fillTableAndSend( // The calls to send are made in this function clientServer, dst, @@ -240,7 +270,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { /* istanbul ignore next */ clientServer.logger.error('Send_attempt error', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } }) @@ -248,7 +278,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { /* istanbul ignore next */ clientServer.logger.error('Error getting userID :', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } }) diff --git a/packages/matrix-client-server/src/register/msisdn/requestToken.ts b/packages/matrix-client-server/src/register/msisdn/requestToken.ts index 37cc9e16..850f01e0 100644 --- a/packages/matrix-client-server/src/register/msisdn/requestToken.ts +++ b/packages/matrix-client-server/src/register/msisdn/requestToken.ts @@ -93,7 +93,7 @@ export const smsBody = ( ) } -export const fillTable = ( +export const fillTableAndSend = ( clientServer: MatrixClientServer, dst: string, clientSecret: string, @@ -120,20 +120,25 @@ export const fillTable = ( last_send_attempt: sendAttempt }) .then(() => { - send(res, 200, { sid, submit_url: getSubmitUrl(clientServer.conf) }) + send( + res, + 200, + { sid, submit_url: getSubmitUrl(clientServer.conf) }, + clientServer.logger + ) }) .catch((err) => { // istanbul ignore next clientServer.logger.error('Insertion error:', err) // istanbul ignore next - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) }) .catch((err) => { /* istanbul ignore next */ clientServer.logger.error('Token error:', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } @@ -156,25 +161,50 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { const dst = formatPhoneNumber(phoneNumber, country) const nextLink = (obj as RequestTokenArgs).next_link if (!clientSecretRegex.test(clientSecret)) { - send(res, 400, errMsg('invalidParam', 'Invalid client_secret')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid client_secret'), + clientServer.logger + ) } else if (!validCountryRegex.test(country)) { - send(res, 400, errMsg('invalidParam', 'Invalid country')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid country'), + clientServer.logger + ) // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions } else if (nextLink && !isValidUrl(nextLink)) { - send(res, 400, errMsg('invalidParam', 'Invalid next_link')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid next_link'), + clientServer.logger + ) } else if (!validPhoneNumberRegex.test(dst)) { - send(res, 400, errMsg('invalidParam', 'Invalid phone number')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid phone number'), + clientServer.logger + ) } else if ( typeof sendAttempt !== 'number' || sendAttempt > maxAttemps ) { - send(res, 400, errMsg('invalidParam', 'Invalid send attempt')) + send( + res, + 400, + errMsg('invalidParam', 'Invalid send attempt'), + clientServer.logger + ) } else { clientServer.matrixDb .get('user_threepids', ['user_id'], { address: dst }) .then((rows) => { if (rows.length > 0) { - send(res, 400, errMsg('threepidInUse')) + send(res, 400, errMsg('threepidInUse'), clientServer.logger) } else { clientServer.matrixDb .get( @@ -188,10 +218,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { .then((rows) => { if (rows.length > 0) { if (sendAttempt === rows[0].last_send_attempt) { - send(res, 200, { - sid: rows[0].session_id, - submit_url: getSubmitUrl(clientServer.conf) - }) + send( + res, + 200, + { + sid: rows[0].session_id, + submit_url: getSubmitUrl(clientServer.conf) + }, + clientServer.logger + ) } else { clientServer.matrixDb .deleteWhere('threepid_validation_session', [ @@ -207,7 +242,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { } ]) .then(() => { - fillTable( + fillTableAndSend( // The calls to send are made in this function clientServer, dst, @@ -224,11 +259,16 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { // istanbul ignore next clientServer.logger.error('Deletion error:', err) // istanbul ignore next - send(res, 500, errMsg('unknown', err)) + send( + res, + 500, + errMsg('unknown', err), + clientServer.logger + ) }) } } else { - fillTable( + fillTableAndSend( // The calls to send are made in this function clientServer, dst, @@ -246,7 +286,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { /* istanbul ignore next */ clientServer.logger.error('Send_attempt error:', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } }) @@ -254,7 +294,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => { /* istanbul ignore next */ clientServer.logger.error('Error getting userID :', err) /* istanbul ignore next */ - send(res, 500, errMsg('unknown', err)) + send(res, 500, errMsg('unknown', err), clientServer.logger) }) } }) diff --git a/packages/matrix-identity-server/src/validate/email/requestToken.ts b/packages/matrix-identity-server/src/validate/email/requestToken.ts index 863e0bd2..5b0b0cc8 100644 --- a/packages/matrix-identity-server/src/validate/email/requestToken.ts +++ b/packages/matrix-identity-server/src/validate/email/requestToken.ts @@ -81,7 +81,7 @@ const mailBody = ( ) } -const fillTable = ( +const fillTableAndSend = ( idServer: MatrixIdentityServer, dst: string, clientSecret: string, @@ -185,7 +185,7 @@ const RequestToken = ( { field: 'session_id', value: rows[0].session_id } ) .then(() => { - fillTable( + fillTableAndSend( // The calls to send are made in this function idServer, dst, @@ -205,7 +205,7 @@ const RequestToken = ( }) } } else { - fillTable( + fillTableAndSend( // The calls to send are made in this function idServer, dst,