Skip to content

Commit

Permalink
Use matrix-resolve in Matrix token validation of matrix-identity-server
Browse files Browse the repository at this point in the history
  • Loading branch information
guimard committed Nov 26, 2023
1 parent 07ec83b commit f31fbc3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
7 changes: 4 additions & 3 deletions packages/matrix-identity-server/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
testTimeout: 10000,
testTimeout: 30000,
testEnvironment: 'node',
preset: 'ts-jest',
collectCoverage: true,
Expand All @@ -13,7 +13,8 @@ export default {
}
},
moduleNameMapper: {
"@twake/(.*)$": "<rootDir>/../$1/src",
'@twake/(.*)$': '<rootDir>/../$1/src',
'matrix-resolve': '<rootDir>/../matrix-resolve/src',
},
globalTeardown: '<rootDir>/jest.global-teardown.ts'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ describe('/_matrix/identity/v2/account/register', () => {
status: 200,
json: () => {
return {
sub: '@dwho:example.com'
sub: '@dwho:example.com',
'm.server': 'matrix.example.com:8448'
}
}
})
Expand Down
9 changes: 6 additions & 3 deletions packages/matrix-identity-server/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ describe('/_matrix/identity/v2/account/register', () => {
status: 200,
json: () => {
return {
sub: '@dwho:example.com'
sub: '@dwho:example.com',
'm.server': 'matrix.example.com:8448'
}
}
})
Expand Down Expand Up @@ -179,7 +180,8 @@ describe('/_matrix/identity/v2/account/register', () => {
status: 200,
json: () => {
return {
email: '[email protected]'
email: '[email protected]',
'm.server': 'matrix.example.com:8448'
}
}
})
Expand All @@ -203,7 +205,8 @@ describe('/_matrix/identity/v2/account/register', () => {
status: 200,
json: () => {
return {
sub: '[email protected]'
sub: '[email protected]',
'm.server': 'matrix.example.com:8448'
}
}
})
Expand Down
3 changes: 2 additions & 1 deletion packages/matrix-identity-server/src/terms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ test('Get authentication token', async () => {
status: 200,
json: () => {
return {
sub: '@dwho:example.com'
sub: '@dwho:example.com',
'm.server': 'matrix.example.com:8448'
}
}
})
Expand Down
45 changes: 27 additions & 18 deletions packages/matrix-identity-server/src/utils/validateMatrixToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable prefer-promise-reject-errors */
import fetch from 'node-fetch'
import { matrixResolve } from 'matrix-resolve'

interface userInfoResponse {
sub: string
Expand All @@ -17,24 +18,32 @@ const validateMatrixToken = (
if (!hostnameRe.test(matrixServer))
return Promise.reject('Bad matrix_server_name')
return new Promise((resolve, reject) => {
fetch(
encodeURI(
`https://${matrixServer}/_matrix/federation/v1/openid/userinfo?access_token=${accessToken}`
)
)
// eslint-disable-next-line @typescript-eslint/promise-function-async
.then((res) => res.json())
.then((userInfo) => {
if ((userInfo as userInfoResponse).sub != null) {
const cmp = (userInfo as userInfoResponse).sub.match(/^@(.+?):[^:]+$/)
if (cmp != null) {
resolve((userInfo as userInfoResponse).sub)
} else {
reject('Invalid response from Matrix Server')
}
} else {
reject("The Matrix homeserver did not include 'sub' in its response")
}
matrixResolve(matrixServer)
.then((baseUrl) => {
fetch(
encodeURI(
`https://${matrixServer}/_matrix/federation/v1/openid/userinfo?access_token=${accessToken}`
)
)
// eslint-disable-next-line @typescript-eslint/promise-function-async
.then((res) => res.json())
.then((userInfo) => {
if ((userInfo as userInfoResponse).sub != null) {
const cmp = (userInfo as userInfoResponse).sub.match(
/^@(.+?):[^:]+$/
)
if (cmp != null) {
resolve((userInfo as userInfoResponse).sub)
} else {
reject('Invalid response from Matrix Server')
}
} else {
reject(
"The Matrix homeserver did not include 'sub' in its response"
)
}
})
.catch(reject)
})
.catch(reject)
})
Expand Down

0 comments on commit f31fbc3

Please sign in to comment.