Skip to content

Commit

Permalink
Merge pull request #142 from linagora/client-server-fix-delete
Browse files Browse the repository at this point in the history
fix : removed the fetch in /delete and replaced it with auxiliary function
  • Loading branch information
guimard authored Aug 19, 2024
2 parents 4d8b130 + 5ea5692 commit d0c4194
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@
"is_sso_login_enabled": true,
"is_msisdn_login_enabled": true,
"registration_required_3pid": [],
"capabilities": {}
"capabilities": {},
"open_id_token_lifetime": 3600000
}
46 changes: 1 addition & 45 deletions packages/matrix-client-server/src/account/3pid/3pid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,19 +474,6 @@ describe('Use configuration file', () => {
expect(response.body).toHaveProperty('error', 'Invalid phone number')
})
it('should unbind from the id server provided in the request body', async () => {
const mockOpenIDResponse = Promise.resolve({
ok: true,
status: 200,
// eslint-disable-next-line @typescript-eslint/promise-function-async
json: () =>
Promise.resolve({
access_token: 'openIdToken',
expires_in: 3600,
matrix_server_name: 'example.com',
token_type: 'Bearer'
})
})

const mockResolveResponse = Promise.resolve({
ok: true,
status: 200,
Expand Down Expand Up @@ -518,8 +505,6 @@ describe('Use configuration file', () => {
})
})
// @ts-expect-error mock is unknown
fetch.mockImplementationOnce(async () => await mockOpenIDResponse)
// @ts-expect-error mock is unknown
fetch.mockImplementationOnce(async () => await mockResolveResponse)
// @ts-expect-error mock is unknown
fetch.mockImplementationOnce(async () => await mockRegisterResponse)
Expand Down Expand Up @@ -564,19 +549,6 @@ describe('Use configuration file', () => {
added_at: epoch()
})

const mockOpenIDResponse = Promise.resolve({
ok: true,
status: 200,
// eslint-disable-next-line @typescript-eslint/promise-function-async
json: () =>
Promise.resolve({
access_token: 'openIdToken',
expires_in: 3600,
matrix_server_name: 'example.com',
token_type: 'Bearer'
})
})

const mockResolveResponse = Promise.resolve({
ok: true,
status: 200,
Expand Down Expand Up @@ -608,8 +580,6 @@ describe('Use configuration file', () => {
})
})
// @ts-expect-error mock is unknown
fetch.mockImplementationOnce(async () => await mockOpenIDResponse)
// @ts-expect-error mock is unknown
fetch.mockImplementationOnce(async () => await mockResolveResponse)
// @ts-expect-error mock is unknown
fetch.mockImplementationOnce(async () => await mockRegisterResponse)
Expand Down Expand Up @@ -655,19 +625,6 @@ describe('Use configuration file', () => {
)
})
it('should return an error if the unbind was unsuccessful on the id-server', async () => {
const mockOpenIDResponse = Promise.resolve({
ok: true,
status: 200,
// eslint-disable-next-line @typescript-eslint/promise-function-async
json: () =>
Promise.resolve({
access_token: 'openIdToken',
expires_in: 3600,
matrix_server_name: 'example.com',
token_type: 'Bearer'
})
})

const mockResolveResponse = Promise.resolve({
ok: true,
status: 200,
Expand Down Expand Up @@ -698,8 +655,7 @@ describe('Use configuration file', () => {
errcode: 'M_INVALID_PARAM'
})
})
// @ts-expect-error mock is unknown
fetch.mockImplementationOnce(async () => await mockOpenIDResponse)

// @ts-expect-error mock is unknown
fetch.mockImplementationOnce(async () => await mockResolveResponse)
// @ts-expect-error mock is unknown
Expand Down
200 changes: 87 additions & 113 deletions packages/matrix-client-server/src/account/3pid/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type { ServerResponse } from 'http'
import type e from 'express'
import { MatrixResolve } from 'matrix-resolve'
import { isAdmin } from '../../utils/utils'
import { insertOpenIdToken } from '../../user/openid/requestToken'
import { randomString } from '@twake/crypto'

interface RequestBody {
address: string
Expand All @@ -25,13 +27,6 @@ interface RegisterResponseBody {
token: string
}

interface OpenIDResponseBody {
access_token: string
expires_in: number
matrix_server_name: string
token_type: string
}

const schema = {
address: true,
id_server: false,
Expand All @@ -43,101 +38,86 @@ const deleteAndSend = async (
body: RequestBody,
clientServer: MatrixClientServer,
idServer: string,
data: TokenContent,
token: string | null
data: TokenContent
): Promise<void> => {
const openIDResponse = await fetch(
`https://${clientServer.conf.server_name}/_matrix/client/v3/user/${data.sub}/openid/request_token`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${token as string}`
},
body: JSON.stringify({
user_id: data.sub
})
}
)
// istanbul ignore if
if (!openIDResponse.ok) {
// istanbul ignore next
send(
res,
500,
errMsg('unknown', 'Error while requesting OpenID token'),
clientServer.logger
)
// istanbul ignore next
return
}
const openIDResponseBody: OpenIDResponseBody =
(await openIDResponse.json()) as OpenIDResponseBody

const matrixResolve = new MatrixResolve({
cache: 'toad-cache'
})
const baseUrl: string | string[] = await matrixResolve.resolve(idServer)
const registerResponse = await fetch(
`https://${baseUrl as string}/_matrix/identity/v2/account/register`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(openIDResponseBody)
}
)
const validToken = ((await registerResponse.json()) as RegisterResponseBody)
.token
const UnbindResponse = await fetch(
`https://${baseUrl as string}/_matrix/identity/v2/3pid/unbind`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${validToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
address: body.address,
medium: body.medium
})
}
)
if (UnbindResponse.ok) {
const deleteAdd = clientServer.matrixDb.deleteWhere('user_threepids', [
{ field: 'address', value: body.address, operator: '=' },
{ field: 'medium', value: body.medium, operator: '=' },
{ field: 'user_id', value: data.sub, operator: '=' }
])
const deleteBind = clientServer.matrixDb.deleteWhere(
'user_threepid_id_server',
[
{ field: 'address', value: body.address, operator: '=' },
{ field: 'medium', value: body.medium, operator: '=' },
{ field: 'user_id', value: data.sub, operator: '=' },
{ field: 'id_server', value: idServer, operator: '=' }
]
)
Promise.all([deleteAdd, deleteBind])
.then(() => {
send(res, 200, { id_server_unbind_result: 'success' })
insertOpenIdToken(clientServer, data.sub, randomString(64))
.then(async (openIDRows) => {
const matrixResolve = new MatrixResolve({
cache: 'toad-cache'
})
.catch((e) => {
// istanbul ignore next
clientServer.logger.error('Error while deleting user_threepids', e)
const baseUrl: string | string[] = await matrixResolve.resolve(idServer)
const registerResponse = await fetch(
`https://${baseUrl as string}/_matrix/identity/v2/account/register`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
access_token: openIDRows[0].token,
expires_in: clientServer.conf.open_id_token_lifetime,
matrix_server_name: clientServer.conf.server_name,
token_type: 'Bearer'
})
}
)
const validToken = (
(await registerResponse.json()) as RegisterResponseBody
).token
const UnbindResponse = await fetch(
`https://${baseUrl as string}/_matrix/identity/v2/3pid/unbind`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${validToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
address: body.address,
medium: body.medium
})
}
)
if (UnbindResponse.ok) {
const deleteAdd = clientServer.matrixDb.deleteWhere('user_threepids', [
{ field: 'address', value: body.address, operator: '=' },
{ field: 'medium', value: body.medium, operator: '=' },
{ field: 'user_id', value: data.sub, operator: '=' }
])
const deleteBind = clientServer.matrixDb.deleteWhere(
'user_threepid_id_server',
[
{ field: 'address', value: body.address, operator: '=' },
{ field: 'medium', value: body.medium, operator: '=' },
{ field: 'user_id', value: data.sub, operator: '=' },
{ field: 'id_server', value: idServer, operator: '=' }
]
)
Promise.all([deleteAdd, deleteBind])
.then(() => {
send(res, 200, { id_server_unbind_result: 'success' })
})
.catch((e) => {
// istanbul ignore next
clientServer.logger.error('Error while deleting user_threepids', e)
// istanbul ignore next
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
} else {
// istanbul ignore next
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
} else {
// istanbul ignore next
send(res, UnbindResponse.status, {
id_server_unbind_result: 'no-support'
send(res, UnbindResponse.status, {
id_server_unbind_result: 'no-support'
})
}
})
.catch((e) => {
// istanbul ignore next
clientServer.logger.error('Error while inserting openID token', e)
// istanbul ignore next
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
}
}

const delete3pid = (clientServer: MatrixClientServer): expressAppHandler => {
Expand Down Expand Up @@ -201,19 +181,14 @@ const delete3pid = (clientServer: MatrixClientServer): expressAppHandler => {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (typeof body.id_server === 'string' && body.id_server) {
idServer = body.id_server
deleteAndSend(
res,
body,
clientServer,
idServer,
data,
token
).catch((e) => {
// istanbul ignore next
clientServer.logger.error('Error while deleting 3pid', e)
// istanbul ignore next
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
deleteAndSend(res, body, clientServer, idServer, data).catch(
(e) => {
// istanbul ignore next
clientServer.logger.error('Error while deleting 3pid', e)
// istanbul ignore next
send(res, 500, errMsg('unknown', e), clientServer.logger)
}
)
} else {
clientServer.matrixDb
.get('user_threepid_id_server', ['id_server'], {
Expand All @@ -236,8 +211,7 @@ const delete3pid = (clientServer: MatrixClientServer): expressAppHandler => {
body,
clientServer,
rows[0].id_server as string,
data,
token
data
).catch((e) => {
// istanbul ignore next
clientServer.logger.error('Error while deleting 3pid', e)
Expand Down
3 changes: 2 additions & 1 deletion packages/matrix-client-server/src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@
"is_password_login_enabled": true,
"is_sso_login_enabled": true,
"is_msisdn_login_enabled": true,
"registration_required_3pid": []
"registration_required_3pid": [],
"open_id_token_lifetime": 3600000
}
Loading

0 comments on commit d0c4194

Please sign in to comment.