2019-07-29 12:58:47 +02:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-09-27 09:54:55 +02:00
|
|
|
import React from "react";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { SERVICE_TYPES } from "matrix-js-sdk/src/service-types";
|
2023-05-16 15:25:43 +02:00
|
|
|
import { createClient, MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix";
|
2021-10-23 00:23:32 +02:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2019-08-01 13:46:59 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
|
|
|
import Modal from "./Modal";
|
|
|
|
import { _t } from "./languageHandler";
|
|
|
|
import { Service, startTermsFlow, TermsNotSignedError } from "./Terms";
|
2019-10-31 12:58:31 +01:00
|
|
|
import {
|
|
|
|
doesAccountDataHaveIdentityServer,
|
|
|
|
doesIdentityServerHaveTerms,
|
2022-07-29 13:01:15 +02:00
|
|
|
setToDefaultIdentityServer,
|
2022-12-12 12:24:14 +01:00
|
|
|
} from "./utils/IdentityServerUtils";
|
2021-09-27 09:54:55 +02:00
|
|
|
import QuestionDialog from "./components/views/dialogs/QuestionDialog";
|
|
|
|
import { abbreviateUrl } from "./utils/UrlUtils";
|
2021-09-21 17:48:09 +02:00
|
|
|
|
2019-10-31 12:58:31 +01:00
|
|
|
export class AbortedIdentityActionError extends Error {}
|
2019-07-29 12:58:47 +02:00
|
|
|
|
|
|
|
export default class IdentityAuthClient {
|
2023-05-05 10:11:14 +02:00
|
|
|
private accessToken: string | null = null;
|
|
|
|
private tempClient?: MatrixClient;
|
2021-09-28 07:48:56 +02:00
|
|
|
private authEnabled = true;
|
2021-09-27 09:54:55 +02:00
|
|
|
|
2019-08-15 23:59:44 +02:00
|
|
|
/**
|
|
|
|
* Creates a new identity auth client
|
|
|
|
* @param {string} identityUrl The URL to contact the identity server with.
|
|
|
|
* When provided, this class will operate solely within memory, refusing to
|
|
|
|
* persist any information such as tokens. Default null (not provided).
|
|
|
|
*/
|
2022-12-16 13:29:59 +01:00
|
|
|
public constructor(identityUrl?: string) {
|
2019-08-15 23:59:44 +02:00
|
|
|
if (identityUrl) {
|
|
|
|
// XXX: We shouldn't have to create a whole new MatrixClient just to
|
|
|
|
// do identity server auth. The functions don't take an identity URL
|
|
|
|
// though, and making all of them take one could lead to developer
|
|
|
|
// confusion about what the idBaseUrl does on a client. Therefore, we
|
|
|
|
// just make a new client and live with it.
|
2019-08-19 18:25:20 +02:00
|
|
|
this.tempClient = createClient({
|
2019-08-15 23:59:44 +02:00
|
|
|
baseUrl: "", // invalid by design
|
|
|
|
idBaseUrl: identityUrl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 09:54:55 +02:00
|
|
|
private get matrixClient(): MatrixClient {
|
2019-08-15 23:59:44 +02:00
|
|
|
return this.tempClient ? this.tempClient : MatrixClientPeg.get();
|
|
|
|
}
|
|
|
|
|
2021-09-27 09:54:55 +02:00
|
|
|
private writeToken(): void {
|
2019-08-15 23:59:44 +02:00
|
|
|
if (this.tempClient) return; // temporary client: ignore
|
2023-05-25 10:39:23 +02:00
|
|
|
if (this.accessToken) {
|
|
|
|
window.localStorage.setItem("mx_is_access_token", this.accessToken);
|
|
|
|
} else {
|
|
|
|
window.localStorage.removeItem("mx_is_access_token");
|
|
|
|
}
|
2019-08-15 23:59:44 +02:00
|
|
|
}
|
|
|
|
|
2023-02-16 18:21:44 +01:00
|
|
|
private readToken(): string | null {
|
2019-08-15 23:59:44 +02:00
|
|
|
if (this.tempClient) return null; // temporary client: ignore
|
|
|
|
return window.localStorage.getItem("mx_is_access_token");
|
2019-07-29 12:58:47 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 09:54:55 +02:00
|
|
|
public hasCredentials(): boolean {
|
|
|
|
return Boolean(this.accessToken);
|
2019-07-29 12:58:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a promise that resolves to the access_token string from the IS
|
2023-02-16 18:21:44 +01:00
|
|
|
public async getAccessToken({ check = true } = {}): Promise<string | null> {
|
2019-07-29 15:41:57 +02:00
|
|
|
if (!this.authEnabled) {
|
|
|
|
// The current IS doesn't support authentication
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-02-16 18:21:44 +01:00
|
|
|
let token: string | null = this.accessToken;
|
2019-07-29 12:58:47 +02:00
|
|
|
if (!token) {
|
2021-09-27 09:54:55 +02:00
|
|
|
token = this.readToken();
|
2019-07-29 12:58:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!token) {
|
2019-08-20 06:54:23 +02:00
|
|
|
token = await this.registerForToken(check);
|
2019-08-02 16:21:53 +02:00
|
|
|
if (token) {
|
|
|
|
this.accessToken = token;
|
2021-09-27 09:54:55 +02:00
|
|
|
this.writeToken();
|
2019-08-02 16:21:53 +02:00
|
|
|
}
|
|
|
|
return token;
|
2019-07-29 12:58:47 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 06:54:23 +02:00
|
|
|
if (check) {
|
|
|
|
try {
|
2021-09-27 09:54:55 +02:00
|
|
|
await this.checkToken(token);
|
2019-08-20 06:54:23 +02:00
|
|
|
} catch (e) {
|
2022-12-12 12:24:14 +01:00
|
|
|
if (e instanceof TermsNotSignedError || e instanceof AbortedIdentityActionError) {
|
2019-08-20 06:54:23 +02:00
|
|
|
// Retrying won't help this
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
// Retry in case token expired
|
|
|
|
token = await this.registerForToken();
|
|
|
|
if (token) {
|
|
|
|
this.accessToken = token;
|
2021-09-27 09:54:55 +02:00
|
|
|
this.writeToken();
|
2019-08-20 06:54:23 +02:00
|
|
|
}
|
2019-08-02 16:21:53 +02:00
|
|
|
}
|
2019-07-29 12:58:47 +02:00
|
|
|
}
|
2019-07-29 15:41:57 +02:00
|
|
|
|
|
|
|
return token;
|
2019-07-29 12:58:47 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 09:54:55 +02:00
|
|
|
private async checkToken(token: string): Promise<void> {
|
2023-04-25 10:28:48 +02:00
|
|
|
const identityServerUrl = this.matrixClient.getIdentityServerUrl()!;
|
2019-10-31 12:58:31 +01:00
|
|
|
|
2019-08-01 13:46:59 +02:00
|
|
|
try {
|
2021-09-27 09:54:55 +02:00
|
|
|
await this.matrixClient.getIdentityAccount(token);
|
2019-08-01 13:46:59 +02:00
|
|
|
} catch (e) {
|
2023-05-16 15:25:43 +02:00
|
|
|
if (e instanceof MatrixError && e.errcode === "M_TERMS_NOT_SIGNED") {
|
2021-09-21 17:48:09 +02:00
|
|
|
logger.log("Identity server requires new terms to be agreed to");
|
2023-06-01 15:43:24 +02:00
|
|
|
await startTermsFlow(this.matrixClient, [new Service(SERVICE_TYPES.IS, identityServerUrl, token)]);
|
2019-08-01 13:46:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2019-07-30 17:56:19 +02:00
|
|
|
|
2019-10-31 12:58:31 +01:00
|
|
|
if (
|
|
|
|
!this.tempClient &&
|
2023-05-23 17:24:12 +02:00
|
|
|
!doesAccountDataHaveIdentityServer(this.matrixClient) &&
|
|
|
|
!(await doesIdentityServerHaveTerms(this.matrixClient, identityServerUrl))
|
2019-10-31 12:58:31 +01:00
|
|
|
) {
|
2022-06-14 18:51:51 +02:00
|
|
|
const { finished } = Modal.createDialog(QuestionDialog, {
|
|
|
|
title: _t("Identity server has no terms of service"),
|
|
|
|
description: (
|
|
|
|
<div>
|
2022-12-12 12:24:14 +01:00
|
|
|
<p>
|
|
|
|
{_t(
|
|
|
|
"This action requires accessing the default identity server " +
|
|
|
|
"<server /> to validate an email address or phone number, " +
|
|
|
|
"but the server does not have any terms of service.",
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
server: () => <b>{abbreviateUrl(identityServerUrl)}</b>,
|
|
|
|
},
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
<p>{_t("Only continue if you trust the owner of the server.")}</p>
|
2022-06-14 18:51:51 +02:00
|
|
|
</div>
|
|
|
|
),
|
|
|
|
button: _t("Trust"),
|
|
|
|
});
|
2019-10-31 12:58:31 +01:00
|
|
|
const [confirmed] = await finished;
|
|
|
|
if (confirmed) {
|
2023-05-23 17:24:12 +02:00
|
|
|
setToDefaultIdentityServer(this.matrixClient);
|
2019-10-31 12:58:31 +01:00
|
|
|
} else {
|
2022-12-12 12:24:14 +01:00
|
|
|
throw new AbortedIdentityActionError("User aborted identity server action without terms");
|
2019-10-31 12:58:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 18:30:10 +02:00
|
|
|
// We should ensure the token in `localStorage` is cleared
|
2019-07-30 11:09:38 +02:00
|
|
|
// appropriately. We already clear storage on sign out, but we'll need
|
|
|
|
// additional clearing when changing ISes in settings as part of future
|
|
|
|
// privacy work.
|
2020-08-03 17:02:26 +02:00
|
|
|
// See also https://github.com/vector-im/element-web/issues/10455.
|
2019-07-29 12:58:47 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 09:54:55 +02:00
|
|
|
public async registerForToken(check = true): Promise<string> {
|
2020-03-10 00:05:13 +01:00
|
|
|
const hsOpenIdToken = await MatrixClientPeg.get().getOpenIdToken();
|
|
|
|
// XXX: The spec is `token`, but we used `access_token` for a Sydent release.
|
2022-12-12 12:24:14 +01:00
|
|
|
const { access_token: accessToken, token } = await this.matrixClient.registerWithIdentityServer(hsOpenIdToken);
|
2020-03-10 00:05:13 +01:00
|
|
|
const identityAccessToken = token ? token : accessToken;
|
2021-09-27 09:54:55 +02:00
|
|
|
if (check) await this.checkToken(identityAccessToken);
|
2020-03-10 00:05:13 +01:00
|
|
|
return identityAccessToken;
|
2019-07-29 12:58:47 +02:00
|
|
|
}
|
|
|
|
}
|