Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 41dfec2

Browse files
author
Kerry
authored
add delegatedauthentication to validated server config (#11053)
1 parent d5d1ec7 commit 41dfec2

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/utils/AutoDiscoveryUtils.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ limitations under the License.
1616

1717
import React, { ReactNode } from "react";
1818
import { AutoDiscovery, ClientConfig } from "matrix-js-sdk/src/autodiscovery";
19+
import { IDelegatedAuthConfig, M_AUTHENTICATION } from "matrix-js-sdk/src/client";
1920
import { logger } from "matrix-js-sdk/src/logger";
2021
import { IClientWellKnown } from "matrix-js-sdk/src/matrix";
22+
import { ValidatedIssuerConfig } from "matrix-js-sdk/src/oidc/validate";
2123

2224
import { _t, UserFriendlyError } from "../languageHandler";
2325
import SdkConfig from "../SdkConfig";
@@ -260,6 +262,20 @@ export default class AutoDiscoveryUtils {
260262
throw new UserFriendlyError("Unexpected error resolving homeserver configuration");
261263
}
262264

265+
let delegatedAuthentication = undefined;
266+
if (discoveryResult[M_AUTHENTICATION.stable!]?.state === AutoDiscovery.SUCCESS) {
267+
const { authorizationEndpoint, registrationEndpoint, tokenEndpoint, account, issuer } = discoveryResult[
268+
M_AUTHENTICATION.stable!
269+
] as IDelegatedAuthConfig & ValidatedIssuerConfig;
270+
delegatedAuthentication = {
271+
authorizationEndpoint,
272+
registrationEndpoint,
273+
tokenEndpoint,
274+
account,
275+
issuer,
276+
};
277+
}
278+
263279
return {
264280
hsUrl: preferredHomeserverUrl,
265281
hsName: preferredHomeserverName,
@@ -268,6 +284,7 @@ export default class AutoDiscoveryUtils {
268284
isDefault: false,
269285
warning: hsResult.error,
270286
isNameResolvable: !isSynthetic,
287+
delegatedAuthentication,
271288
} as ValidatedServerConfig;
272289
}
273290
}

src/utils/ValidatedServerConfig.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
import { IDelegatedAuthConfig } from "matrix-js-sdk/src/client";
18+
import { ValidatedIssuerConfig } from "matrix-js-sdk/src/oidc/validate";
19+
1720
export interface ValidatedServerConfig {
1821
hsUrl: string;
1922
hsName: string;
@@ -26,4 +29,6 @@ export interface ValidatedServerConfig {
2629
isNameResolvable: boolean;
2730

2831
warning: string | Error;
32+
33+
delegatedAuthentication?: IDelegatedAuthConfig & ValidatedIssuerConfig;
2934
}

test/utils/AutoDiscoveryUtils-test.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
import { AutoDiscovery, AutoDiscoveryAction, ClientConfig } from "matrix-js-sdk/src/autodiscovery";
1818
import { logger } from "matrix-js-sdk/src/logger";
19+
import { M_AUTHENTICATION } from "matrix-js-sdk/src/client";
1920

2021
import AutoDiscoveryUtils from "../../src/utils/AutoDiscoveryUtils";
2122

@@ -186,5 +187,50 @@ describe("AutoDiscoveryUtils", () => {
186187
warning: "Homeserver URL does not appear to be a valid Matrix homeserver",
187188
});
188189
});
190+
191+
it("ignores delegated auth config when discovery was not successful", () => {
192+
const discoveryResult = {
193+
...validIsConfig,
194+
...validHsConfig,
195+
[M_AUTHENTICATION.stable!]: {
196+
state: AutoDiscoveryAction.FAIL_ERROR,
197+
error: "",
198+
},
199+
};
200+
const syntaxOnly = true;
201+
expect(
202+
AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult, syntaxOnly),
203+
).toEqual({
204+
...expectedValidatedConfig,
205+
delegatedAuthentication: undefined,
206+
warning: undefined,
207+
});
208+
});
209+
210+
it("sets delegated auth config when discovery was successful", () => {
211+
const authConfig = {
212+
issuer: "https://test.com/",
213+
authorizationEndpoint: "https://test.com/auth",
214+
registrationEndpoint: "https://test.com/registration",
215+
tokenEndpoint: "https://test.com/token",
216+
};
217+
const discoveryResult = {
218+
...validIsConfig,
219+
...validHsConfig,
220+
[M_AUTHENTICATION.stable!]: {
221+
state: AutoDiscoveryAction.SUCCESS,
222+
error: null,
223+
...authConfig,
224+
},
225+
};
226+
const syntaxOnly = true;
227+
expect(
228+
AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult, syntaxOnly),
229+
).toEqual({
230+
...expectedValidatedConfig,
231+
delegatedAuthentication: authConfig,
232+
warning: undefined,
233+
});
234+
});
189235
});
190236
});

0 commit comments

Comments
 (0)