2019-07-29 13:58:47 +03: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-03-19 05:50:34 +03:00
|
|
|
import { SERVICE_TYPES } from 'matrix-js-sdk/src/service-types';
|
2021-03-19 06:05:51 +03:00
|
|
|
import { createClient } from 'matrix-js-sdk/src/matrix';
|
2019-08-01 14:46:59 +03:00
|
|
|
|
2021-06-29 15:11:58 +03:00
|
|
|
import { MatrixClientPeg } from './MatrixClientPeg';
|
2019-10-31 14:58:31 +03:00
|
|
|
import Modal from './Modal';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from './index';
|
2019-10-31 14:58:31 +03:00
|
|
|
import { _t } from './languageHandler';
|
2019-08-02 17:21:53 +03:00
|
|
|
import { Service, startTermsFlow, TermsNotSignedError } from './Terms';
|
2019-10-31 14:58:31 +03:00
|
|
|
import {
|
|
|
|
doesAccountDataHaveIdentityServer,
|
|
|
|
doesIdentityServerHaveTerms,
|
|
|
|
useDefaultIdentityServer,
|
|
|
|
} from './utils/IdentityServerUtils';
|
|
|
|
import { abbreviateUrl } from './utils/UrlUtils';
|
|
|
|
|
2021-09-21 18:48:09 +03:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2019-10-31 14:58:31 +03:00
|
|
|
export class AbortedIdentityActionError extends Error {}
|
2019-07-29 13:58:47 +03:00
|
|
|
|
|
|
|
export default class IdentityAuthClient {
|
2019-08-16 00:59:44 +03: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).
|
|
|
|
*/
|
|
|
|
constructor(identityUrl = null) {
|
2019-07-29 13:58:47 +03:00
|
|
|
this.accessToken = null;
|
2019-07-29 16:41:57 +03:00
|
|
|
this.authEnabled = true;
|
2019-08-16 00:59:44 +03: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 19:25:20 +03:00
|
|
|
this.tempClient = createClient({
|
2019-08-16 00:59:44 +03:00
|
|
|
baseUrl: "", // invalid by design
|
|
|
|
idBaseUrl: identityUrl,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Indicates that we're using the real client, not some workaround.
|
|
|
|
this.tempClient = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get _matrixClient() {
|
|
|
|
return this.tempClient ? this.tempClient : MatrixClientPeg.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
_writeToken() {
|
|
|
|
if (this.tempClient) return; // temporary client: ignore
|
2019-08-16 01:08:18 +03:00
|
|
|
window.localStorage.setItem("mx_is_access_token", this.accessToken);
|
2019-08-16 00:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_readToken() {
|
|
|
|
if (this.tempClient) return null; // temporary client: ignore
|
|
|
|
return window.localStorage.getItem("mx_is_access_token");
|
2019-07-29 13:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hasCredentials() {
|
|
|
|
return this.accessToken != null; // undef or null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a promise that resolves to the access_token string from the IS
|
2019-09-11 15:36:10 +03:00
|
|
|
async getAccessToken({ check = true } = {}) {
|
2019-07-29 16:41:57 +03:00
|
|
|
if (!this.authEnabled) {
|
|
|
|
// The current IS doesn't support authentication
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-29 13:58:47 +03:00
|
|
|
let token = this.accessToken;
|
|
|
|
if (!token) {
|
2019-08-16 00:59:44 +03:00
|
|
|
token = this._readToken();
|
2019-07-29 13:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!token) {
|
2019-08-20 07:54:23 +03:00
|
|
|
token = await this.registerForToken(check);
|
2019-08-02 17:21:53 +03:00
|
|
|
if (token) {
|
|
|
|
this.accessToken = token;
|
2019-08-16 00:59:44 +03:00
|
|
|
this._writeToken();
|
2019-08-02 17:21:53 +03:00
|
|
|
}
|
|
|
|
return token;
|
2019-07-29 13:58:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-20 07:54:23 +03:00
|
|
|
if (check) {
|
|
|
|
try {
|
|
|
|
await this._checkToken(token);
|
|
|
|
} catch (e) {
|
2019-10-31 14:58:31 +03:00
|
|
|
if (
|
|
|
|
e instanceof TermsNotSignedError ||
|
|
|
|
e instanceof AbortedIdentityActionError
|
|
|
|
) {
|
2019-08-20 07:54:23 +03:00
|
|
|
// Retrying won't help this
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
// Retry in case token expired
|
|
|
|
token = await this.registerForToken();
|
|
|
|
if (token) {
|
|
|
|
this.accessToken = token;
|
|
|
|
this._writeToken();
|
|
|
|
}
|
2019-08-02 17:21:53 +03:00
|
|
|
}
|
2019-07-29 13:58:47 +03:00
|
|
|
}
|
2019-07-29 16:41:57 +03:00
|
|
|
|
|
|
|
return token;
|
2019-07-29 13:58:47 +03:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:30:10 +03:00
|
|
|
async _checkToken(token) {
|
2019-10-31 14:58:31 +03:00
|
|
|
const identityServerUrl = this._matrixClient.getIdentityServerUrl();
|
|
|
|
|
2019-08-01 14:46:59 +03:00
|
|
|
try {
|
2019-08-16 00:59:44 +03:00
|
|
|
await this._matrixClient.getIdentityAccount(token);
|
2019-08-01 14:46:59 +03:00
|
|
|
} catch (e) {
|
|
|
|
if (e.errcode === "M_TERMS_NOT_SIGNED") {
|
2021-09-21 18:48:09 +03:00
|
|
|
logger.log("Identity server requires new terms to be agreed to");
|
2019-08-01 14:46:59 +03:00
|
|
|
await startTermsFlow([new Service(
|
2019-08-02 16:43:36 +03:00
|
|
|
SERVICE_TYPES.IS,
|
2019-10-31 14:58:31 +03:00
|
|
|
identityServerUrl,
|
2019-08-01 14:46:59 +03:00
|
|
|
token,
|
|
|
|
)]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2019-07-30 18:56:19 +03:00
|
|
|
|
2019-10-31 14:58:31 +03:00
|
|
|
if (
|
|
|
|
!this.tempClient &&
|
|
|
|
!doesAccountDataHaveIdentityServer() &&
|
2021-09-21 18:48:09 +03:00
|
|
|
!(await doesIdentityServerHaveTerms(identityServerUrl))
|
2019-10-31 14:58:31 +03:00
|
|
|
) {
|
|
|
|
const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
|
|
|
|
const { finished } = Modal.createTrackedDialog('Default identity server terms warning', '',
|
|
|
|
QuestionDialog, {
|
2021-08-03 19:07:37 +03:00
|
|
|
title: _t("Identity server has no terms of service"),
|
|
|
|
description: (
|
|
|
|
<div>
|
|
|
|
<p>{ _t(
|
|
|
|
"This action requires accessing the default identity server " +
|
2019-11-01 19:35:16 +03:00
|
|
|
"<server /> to validate an email address or phone number, " +
|
|
|
|
"but the server does not have any terms of service.", {},
|
2021-08-03 19:07:37 +03:00
|
|
|
{
|
|
|
|
server: () => <b>{ abbreviateUrl(identityServerUrl) }</b>,
|
|
|
|
},
|
|
|
|
) }</p>
|
|
|
|
<p>{ _t(
|
|
|
|
"Only continue if you trust the owner of the server.",
|
|
|
|
) }</p>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
button: _t("Trust"),
|
2021-04-27 18:23:27 +03:00
|
|
|
});
|
2019-10-31 14:58:31 +03:00
|
|
|
const [confirmed] = await finished;
|
|
|
|
if (confirmed) {
|
2021-01-20 16:49:15 +03:00
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
2019-10-31 14:58:31 +03:00
|
|
|
useDefaultIdentityServer();
|
|
|
|
} else {
|
|
|
|
throw new AbortedIdentityActionError(
|
|
|
|
"User aborted identity server action without terms",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 19:30:10 +03:00
|
|
|
// We should ensure the token in `localStorage` is cleared
|
2019-07-30 12:09:38 +03: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 18:02:26 +03:00
|
|
|
// See also https://github.com/vector-im/element-web/issues/10455.
|
2019-07-29 13:58:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-20 07:54:23 +03:00
|
|
|
async registerForToken(check=true) {
|
2020-03-10 02:05:13 +03:00
|
|
|
const hsOpenIdToken = await MatrixClientPeg.get().getOpenIdToken();
|
|
|
|
// XXX: The spec is `token`, but we used `access_token` for a Sydent release.
|
|
|
|
const { access_token: accessToken, token } =
|
|
|
|
await this._matrixClient.registerWithIdentityServer(hsOpenIdToken);
|
|
|
|
const identityAccessToken = token ? token : accessToken;
|
|
|
|
if (check) await this._checkToken(identityAccessToken);
|
|
|
|
return identityAccessToken;
|
2019-07-29 13:58:47 +03:00
|
|
|
}
|
|
|
|
}
|