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

Commit

Permalink
add delegatedauthentication to validated server config (#11053)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry authored Jun 13, 2023
1 parent d5d1ec7 commit 41dfec2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/utils/AutoDiscoveryUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ limitations under the License.

import React, { ReactNode } from "react";
import { AutoDiscovery, ClientConfig } from "matrix-js-sdk/src/autodiscovery";
import { IDelegatedAuthConfig, M_AUTHENTICATION } from "matrix-js-sdk/src/client";
import { logger } from "matrix-js-sdk/src/logger";
import { IClientWellKnown } from "matrix-js-sdk/src/matrix";
import { ValidatedIssuerConfig } from "matrix-js-sdk/src/oidc/validate";

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

let delegatedAuthentication = undefined;
if (discoveryResult[M_AUTHENTICATION.stable!]?.state === AutoDiscovery.SUCCESS) {
const { authorizationEndpoint, registrationEndpoint, tokenEndpoint, account, issuer } = discoveryResult[
M_AUTHENTICATION.stable!
] as IDelegatedAuthConfig & ValidatedIssuerConfig;
delegatedAuthentication = {
authorizationEndpoint,
registrationEndpoint,
tokenEndpoint,
account,
issuer,
};
}

return {
hsUrl: preferredHomeserverUrl,
hsName: preferredHomeserverName,
Expand All @@ -268,6 +284,7 @@ export default class AutoDiscoveryUtils {
isDefault: false,
warning: hsResult.error,
isNameResolvable: !isSynthetic,
delegatedAuthentication,
} as ValidatedServerConfig;
}
}
5 changes: 5 additions & 0 deletions src/utils/ValidatedServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { IDelegatedAuthConfig } from "matrix-js-sdk/src/client";
import { ValidatedIssuerConfig } from "matrix-js-sdk/src/oidc/validate";

export interface ValidatedServerConfig {
hsUrl: string;
hsName: string;
Expand All @@ -26,4 +29,6 @@ export interface ValidatedServerConfig {
isNameResolvable: boolean;

warning: string | Error;

delegatedAuthentication?: IDelegatedAuthConfig & ValidatedIssuerConfig;
}
46 changes: 46 additions & 0 deletions test/utils/AutoDiscoveryUtils-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

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

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

Expand Down Expand Up @@ -186,5 +187,50 @@ describe("AutoDiscoveryUtils", () => {
warning: "Homeserver URL does not appear to be a valid Matrix homeserver",
});
});

it("ignores delegated auth config when discovery was not successful", () => {
const discoveryResult = {
...validIsConfig,
...validHsConfig,
[M_AUTHENTICATION.stable!]: {
state: AutoDiscoveryAction.FAIL_ERROR,
error: "",
},
};
const syntaxOnly = true;
expect(
AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult, syntaxOnly),
).toEqual({
...expectedValidatedConfig,
delegatedAuthentication: undefined,
warning: undefined,
});
});

it("sets delegated auth config when discovery was successful", () => {
const authConfig = {
issuer: "https://test.com/",
authorizationEndpoint: "https://test.com/auth",
registrationEndpoint: "https://test.com/registration",
tokenEndpoint: "https://test.com/token",
};
const discoveryResult = {
...validIsConfig,
...validHsConfig,
[M_AUTHENTICATION.stable!]: {
state: AutoDiscoveryAction.SUCCESS,
error: null,
...authConfig,
},
};
const syntaxOnly = true;
expect(
AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult, syntaxOnly),
).toEqual({
...expectedValidatedConfig,
delegatedAuthentication: authConfig,
warning: undefined,
});
});
});
});

0 comments on commit 41dfec2

Please sign in to comment.