Skip to content

Commit 7e3d13f

Browse files
author
Kerry Archibald
committed
add authentication cases to autodiscovery tests
1 parent 2287e0c commit 7e3d13f

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

spec/unit/autodiscovery.spec.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
import MockHttpBackend from "matrix-mock-request";
1919

2020
import { AutoDiscovery } from "../../src/autodiscovery";
21+
import { OidcDiscoveryError } from "../../src/oidc/validate";
2122

2223
describe("AutoDiscovery", function () {
2324
const getHttpBackend = (): MockHttpBackend => {
@@ -368,7 +369,7 @@ describe("AutoDiscovery", function () {
368369
},
369370
);
370371

371-
it("should return SUCCESS when .well-known has a verifiably accurate base_url for " + "m.homeserver", function () {
372+
it("should return SUCCESS when .well-known has a verifiably accurate base_url for m.homeserver", function () {
372373
const httpBackend = getHttpBackend();
373374
httpBackend
374375
.when("GET", "/_matrix/client/versions")
@@ -397,6 +398,10 @@ describe("AutoDiscovery", function () {
397398
error: null,
398399
base_url: null,
399400
},
401+
"m.authentication": {
402+
state: "IGNORE",
403+
error: OidcDiscoveryError.NotSupported,
404+
},
400405
};
401406

402407
expect(conf).toEqual(expected);
@@ -434,6 +439,10 @@ describe("AutoDiscovery", function () {
434439
error: null,
435440
base_url: null,
436441
},
442+
"m.authentication": {
443+
state: "IGNORE",
444+
error: OidcDiscoveryError.NotSupported,
445+
},
437446
};
438447

439448
expect(conf).toEqual(expected);
@@ -625,7 +634,7 @@ describe("AutoDiscovery", function () {
625634
},
626635
);
627636

628-
it("should return SUCCESS when the identity server configuration is " + "verifiably accurate", function () {
637+
it("should return SUCCESS when the identity server configuration is verifiably accurate", function () {
629638
const httpBackend = getHttpBackend();
630639
httpBackend
631640
.when("GET", "/_matrix/client/versions")
@@ -664,14 +673,18 @@ describe("AutoDiscovery", function () {
664673
error: null,
665674
base_url: "https://identity.example.org",
666675
},
676+
"m.authentication": {
677+
state: "IGNORE",
678+
error: OidcDiscoveryError.NotSupported,
679+
},
667680
};
668681

669682
expect(conf).toEqual(expected);
670683
}),
671684
]);
672685
});
673686

674-
it("should return SUCCESS and preserve non-standard keys from the " + ".well-known response", function () {
687+
it("should return SUCCESS and preserve non-standard keys from the .well-known response", function () {
675688
const httpBackend = getHttpBackend();
676689
httpBackend
677690
.when("GET", "/_matrix/client/versions")
@@ -716,6 +729,10 @@ describe("AutoDiscovery", function () {
716729
"org.example.custom.property": {
717730
cupcakes: "yes",
718731
},
732+
"m.authentication": {
733+
state: "IGNORE",
734+
error: OidcDiscoveryError.NotSupported,
735+
},
719736
};
720737

721738
expect(conf).toEqual(expected);

src/autodiscovery.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18-
import { IClientWellKnown, IWellKnownConfig, IDelegatedAuthConfig, IServerVersions } from "./client";
18+
import { IClientWellKnown, IWellKnownConfig, IDelegatedAuthConfig, IServerVersions, M_AUTHENTICATION } from "./client";
1919
import { logger } from "./logger";
2020
import { MatrixError, Method, timeoutSignal } from "./http-api";
2121
import {
@@ -268,7 +268,7 @@ export class AutoDiscovery {
268268
});
269269

270270
const authConfig = await this.validateDiscoveryAuthenticationConfig(wellknown);
271-
clientConfig.m_authentication = authConfig;
271+
clientConfig[M_AUTHENTICATION.stable!] = authConfig;
272272

273273
// Step 8: Give the config to the caller (finally)
274274
return Promise.resolve(clientConfig);
@@ -300,16 +300,19 @@ export class AutoDiscovery {
300300
};
301301
return delegatedAuthConfig;
302302
} catch (error) {
303-
console.log("hhh", error);
304-
305303
const errorMessage = (error as Error).message as unknown as OidcDiscoveryError;
306304
const errorType = Object.values(OidcDiscoveryError).includes(errorMessage)
307305
? errorMessage
308306
: OidcDiscoveryError.General;
309307

308+
const state =
309+
errorType === OidcDiscoveryError.NotSupported
310+
? AutoDiscoveryAction.IGNORE
311+
: AutoDiscoveryAction.FAIL_ERROR;
312+
310313
// @TODO(kerrya) better way to handle this fail type
311314
return {
312-
state: AutoDiscoveryAction.FAIL_ERROR,
315+
state,
313316
error: errorType,
314317
} as unknown as DelegatedAuthConfig;
315318
}

src/oidc/validate.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
import { IClientWellKnown, IDelegatedAuthConfig, M_AUTHENTICATION } from "../client";
218
import { logger } from "../logger";
319

@@ -29,8 +45,8 @@ export const validateWellKnownAuthentication = (wellKnown: IClientWellKnown): ID
2945
}
3046

3147
if (
32-
(typeof authentication.issuer === "string" && !authentication.account) ||
33-
typeof authentication.account === "string"
48+
typeof authentication.issuer === "string" &&
49+
(!authentication.account || typeof authentication.account === "string")
3450
) {
3551
return {
3652
issuer: authentication.issuer,
@@ -42,7 +58,8 @@ export const validateWellKnownAuthentication = (wellKnown: IClientWellKnown): ID
4258
};
4359

4460
// force into a record to make accessing properties easier
45-
const isRecord = (value: unknown): value is Record<string, unknown> => !!value && typeof value === "object";
61+
const isRecord = (value: unknown): value is Record<string, unknown> =>
62+
!!value && typeof value === "object" && !Array.isArray(value);
4663
const requiredStringProperty = (wellKnown: Record<string, unknown>, key: string): boolean => {
4764
if (!wellKnown[key] || typeof wellKnown[key] !== "string") {
4865
logger.error(`OIDC issuer configuration: ${key} is invalid`);
@@ -52,7 +69,7 @@ const requiredStringProperty = (wellKnown: Record<string, unknown>, key: string)
5269
};
5370
const requiredArrayValue = (wellKnown: Record<string, unknown>, key: string, value: any): boolean => {
5471
const array = wellKnown[key];
55-
if (!array || !Array.isArray(array) || !array.find(value)) {
72+
if (!array || !Array.isArray(array) || !array.includes(value)) {
5673
logger.error(`OIDC issuer configuration: ${key} is invalid. ${value} is required.`);
5774
return false;
5875
}
@@ -69,7 +86,7 @@ const requiredArrayValue = (wellKnown: Record<string, unknown>, key: string, val
6986
*/
7087
export const validateOIDCIssuerWellKnown = (wellKnown: unknown): ValidatedIssuerConfig => {
7188
if (!isRecord(wellKnown)) {
72-
logger.error("Issuer configuration not found");
89+
logger.error("Issuer configuration not found or malformed");
7390
throw new Error(OidcDiscoveryError.OpSupport);
7491
}
7592

0 commit comments

Comments
 (0)