@@ -15,6 +15,7 @@ limitations under the License.
1515*/
1616
1717import { Mocked , mocked } from "jest-mock" ;
18+ import fetchMock from "fetch-mock-jest" ;
1819
1920import { logger } from "../../src/logger" ;
2021import { ClientEvent , IMatrixClientCreateOpts , ITurnServerResponse , MatrixClient , Store } from "../../src/client" ;
@@ -76,6 +77,7 @@ import { SecretStorageKeyDescriptionAesV1, ServerSideSecretStorageImpl } from ".
7677import { CryptoBackend } from "../../src/common-crypto/CryptoBackend" ;
7778import { KnownMembership } from "../../src/@types/membership" ;
7879import { RoomMessageEventContent } from "../../src/@types/events" ;
80+ import { mockOpenIdConfiguration } from "../test-utils/oidc.ts" ;
7981
8082jest . useFakeTimers ( ) ;
8183
@@ -265,13 +267,17 @@ describe("MatrixClient", function () {
265267
266268 if ( next . error ) {
267269 // eslint-disable-next-line
268- return Promise . reject ( {
269- errcode : ( < MatrixError > next . error ) . errcode ,
270- httpStatus : ( < MatrixError > next . error ) . httpStatus ,
271- name : ( < MatrixError > next . error ) . errcode ,
272- message : "Expected testing error" ,
273- data : next . error ,
274- } ) ;
270+ return Promise . reject (
271+ new MatrixError (
272+ {
273+ errcode : ( < MatrixError > next . error ) . errcode ,
274+ name : ( < MatrixError > next . error ) . errcode ,
275+ message : "Expected testing error" ,
276+ data : next . error ,
277+ } ,
278+ ( < MatrixError > next . error ) . httpStatus ,
279+ ) ,
280+ ) ;
275281 }
276282 return Promise . resolve ( next . data ) ;
277283 }
@@ -3489,6 +3495,63 @@ describe("MatrixClient", function () {
34893495 } ) ;
34903496 } ) ;
34913497
3498+ describe ( "getAuthMetadata" , ( ) => {
3499+ beforeEach ( ( ) => {
3500+ fetchMock . mockReset ( ) ;
3501+ // This request is made by oidc-client-ts so is not intercepted by httpLookups
3502+ fetchMock . get ( "https://auth.org/jwks" , {
3503+ status : 200 ,
3504+ headers : {
3505+ "Content-Type" : "application/json" ,
3506+ } ,
3507+ keys : [ ] ,
3508+ } ) ;
3509+ } ) ;
3510+
3511+ it ( "should use unstable prefix" , async ( ) => {
3512+ const metadata = mockOpenIdConfiguration ( ) ;
3513+ httpLookups = [
3514+ {
3515+ method : "GET" ,
3516+ path : `/auth_metadata` ,
3517+ data : metadata ,
3518+ prefix : "/_matrix/client/unstable/org.matrix.msc2965" ,
3519+ } ,
3520+ ] ;
3521+
3522+ await expect ( client . getAuthMetadata ( ) ) . resolves . toEqual ( {
3523+ ...metadata ,
3524+ signingKeys : [ ] ,
3525+ } ) ;
3526+ expect ( httpLookups . length ) . toEqual ( 0 ) ;
3527+ } ) ;
3528+
3529+ it ( "should fall back to auth_issuer + openid-configuration" , async ( ) => {
3530+ const metadata = mockOpenIdConfiguration ( ) ;
3531+ httpLookups = [
3532+ {
3533+ method : "GET" ,
3534+ path : `/auth_metadata` ,
3535+ error : new MatrixError ( { errcode : "M_UNRECOGNIZED" } , 404 ) ,
3536+ prefix : "/_matrix/client/unstable/org.matrix.msc2965" ,
3537+ } ,
3538+ {
3539+ method : "GET" ,
3540+ path : `/auth_issuer` ,
3541+ data : { issuer : metadata . issuer } ,
3542+ prefix : "/_matrix/client/unstable/org.matrix.msc2965" ,
3543+ } ,
3544+ ] ;
3545+ fetchMock . get ( "https://auth.org/.well-known/openid-configuration" , metadata ) ;
3546+
3547+ await expect ( client . getAuthMetadata ( ) ) . resolves . toEqual ( {
3548+ ...metadata ,
3549+ signingKeys : [ ] ,
3550+ } ) ;
3551+ expect ( httpLookups . length ) . toEqual ( 0 ) ;
3552+ } ) ;
3553+ } ) ;
3554+
34923555 describe ( "identityHashedLookup" , ( ) => {
34933556 it ( "should return hashed lookup results" , async ( ) => {
34943557 const ID_ACCESS_TOKEN = "hello_id_server_please_let_me_make_a_request" ;
0 commit comments