2016-05-06 13:19:56 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2016-2019 , 2021 The Matrix.org Foundation C.I.C.
|
2016-05-06 13:19:56 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2016-05-06 13:19:56 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-08-04 07:36:16 +00:00
|
|
|
import { SERVICE_TYPES, Room, IOpenIDToken } from "matrix-js-sdk/src/matrix";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2017-12-17 03:13:27 +00:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2021-04-06 11:26:50 +00:00
|
|
|
import { Service, startTermsFlow, TermsInteractionCallback, TermsNotSignedError } from "./Terms";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
2019-12-12 21:37:32 +00:00
|
|
|
import SdkConfig from "./SdkConfig";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { WidgetType } from "./widgets/WidgetType";
|
2023-05-17 11:50:00 +00:00
|
|
|
import { parseUrl } from "./utils/UrlUtils";
|
2021-09-21 15:48:09 +00:00
|
|
|
|
2019-03-13 10:16:44 +00:00
|
|
|
// The version of the integration manager API we're intending to work with
|
|
|
|
const imApiVersion = "1.1";
|
|
|
|
|
2020-04-22 06:34:08 +00:00
|
|
|
// TODO: Generify the name of this class and all components within - it's not just for Scalar.
|
|
|
|
|
2019-08-09 22:05:05 +00:00
|
|
|
export default class ScalarAuthClient {
|
2023-02-15 13:36:22 +00:00
|
|
|
private scalarToken: string | null;
|
|
|
|
private termsInteractionCallback?: TermsInteractionCallback;
|
2021-04-06 11:26:50 +00:00
|
|
|
private isDefaultManager: boolean;
|
|
|
|
|
2024-01-02 18:56:39 +00:00
|
|
|
public constructor(
|
|
|
|
private apiUrl: string,
|
|
|
|
private uiUrl: string,
|
|
|
|
) {
|
2016-09-02 15:03:24 +00:00
|
|
|
this.scalarToken = null;
|
2019-07-23 14:11:38 +00:00
|
|
|
// `undefined` to allow `startTermsFlow` to fallback to a default
|
|
|
|
// callback if this is unset.
|
|
|
|
this.termsInteractionCallback = undefined;
|
2019-08-09 22:05:05 +00:00
|
|
|
|
|
|
|
// We try and store the token on a per-manager basis, but need a fallback
|
|
|
|
// for the default manager.
|
2022-03-18 16:12:36 +00:00
|
|
|
const configApiUrl = SdkConfig.get("integrations_rest_url");
|
|
|
|
const configUiUrl = SdkConfig.get("integrations_ui_url");
|
2019-08-09 22:05:05 +00:00
|
|
|
this.isDefaultManager = apiUrl === configApiUrl && configUiUrl === uiUrl;
|
2016-09-02 15:03:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private writeTokenToStore(): void {
|
2023-04-25 08:28:48 +00:00
|
|
|
window.localStorage.setItem("mx_scalar_token_at_" + this.apiUrl, this.scalarToken ?? "");
|
2019-08-09 22:05:05 +00:00
|
|
|
if (this.isDefaultManager) {
|
|
|
|
// We remove the old token from storage to migrate upwards. This is safe
|
|
|
|
// to do because even if the user switches to /app when this is on /develop
|
|
|
|
// they'll at worst register for a new token.
|
|
|
|
window.localStorage.removeItem("mx_scalar_token"); // no-op when not present
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
private readTokenFromStore(): string | null {
|
2019-08-09 22:05:05 +00:00
|
|
|
let token = window.localStorage.getItem("mx_scalar_token_at_" + this.apiUrl);
|
|
|
|
if (!token && this.isDefaultManager) {
|
|
|
|
token = window.localStorage.getItem("mx_scalar_token");
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
private readToken(): string | null {
|
2019-08-09 22:05:05 +00:00
|
|
|
if (this.scalarToken) return this.scalarToken;
|
2021-04-26 13:02:53 +00:00
|
|
|
return this.readTokenFromStore();
|
2019-06-17 21:29:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public setTermsInteractionCallback(callback: TermsInteractionCallback): void {
|
2019-07-23 14:11:38 +00:00
|
|
|
this.termsInteractionCallback = callback;
|
|
|
|
}
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public connect(): Promise<void> {
|
2016-09-02 15:03:24 +00:00
|
|
|
return this.getScalarToken().then((tok) => {
|
|
|
|
this.scalarToken = tok;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public hasCredentials(): boolean {
|
2016-09-02 15:03:24 +00:00
|
|
|
return this.scalarToken != null; // undef or null
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:00:24 +00:00
|
|
|
// Returns a promise that resolves to a scalar_token string
|
2022-12-16 12:29:59 +00:00
|
|
|
public getScalarToken(): Promise<string> {
|
2021-04-26 13:02:53 +00:00
|
|
|
const token = this.readToken();
|
2016-09-02 15:03:24 +00:00
|
|
|
|
2017-12-17 03:04:32 +00:00
|
|
|
if (!token) {
|
|
|
|
return this.registerForToken();
|
|
|
|
} else {
|
2021-04-26 13:02:53 +00:00
|
|
|
return this.checkToken(token).catch((e) => {
|
2019-07-15 13:05:39 +00:00
|
|
|
if (e instanceof TermsNotSignedError) {
|
|
|
|
// retrying won't help this
|
|
|
|
throw e;
|
|
|
|
}
|
2019-07-11 15:00:24 +00:00
|
|
|
return this.registerForToken();
|
|
|
|
});
|
2017-12-17 03:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:59:07 +00:00
|
|
|
private async getAccountName(token: string): Promise<string> {
|
|
|
|
const url = new URL(this.apiUrl + "/account");
|
|
|
|
url.searchParams.set("scalar_token", token);
|
|
|
|
url.searchParams.set("v", imApiVersion);
|
|
|
|
|
|
|
|
const res = await fetch(url, {
|
|
|
|
method: "GET",
|
2018-10-12 02:54:53 +00:00
|
|
|
});
|
2022-10-12 17:59:07 +00:00
|
|
|
|
|
|
|
const body = await res.json();
|
|
|
|
if (body?.errcode === "M_TERMS_NOT_SIGNED") {
|
|
|
|
throw new TermsNotSignedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!body?.user_id) {
|
|
|
|
throw new Error("Missing user_id in response");
|
|
|
|
}
|
|
|
|
|
|
|
|
return body.user_id;
|
2017-12-17 03:04:32 +00:00
|
|
|
}
|
2016-09-02 15:03:24 +00:00
|
|
|
|
2021-04-27 10:22:17 +00:00
|
|
|
private checkToken(token: string): Promise<string> {
|
2021-04-26 13:02:53 +00:00
|
|
|
return this.getAccountName(token)
|
|
|
|
.then((userId) => {
|
2023-06-21 16:29:44 +00:00
|
|
|
const me = MatrixClientPeg.safeGet().getUserId();
|
2019-07-09 17:51:56 +00:00
|
|
|
if (userId !== me) {
|
|
|
|
throw new Error("Scalar token is owned by someone else: " + me);
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
if (e instanceof TermsNotSignedError) {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log("Integration manager requires new terms to be agreed to");
|
2019-07-22 17:54:04 +00:00
|
|
|
// The terms endpoints are new and so live on standard _matrix prefixes,
|
|
|
|
// but IM rest urls are currently configured with paths, so remove the
|
|
|
|
// path from the base URL before passing it to the js-sdk
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2019-07-23 09:09:16 +00:00
|
|
|
// We continue to use the full URL for the calls done by
|
|
|
|
// matrix-react-sdk, but the standard terms API called
|
|
|
|
// by the js-sdk lives on the standard _matrix path. This means we
|
|
|
|
// don't support running IMs on a non-root path, but it's the only
|
|
|
|
// realistic way of transitioning to _matrix paths since configs in
|
|
|
|
// the wild contain bits of the API path.
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2019-07-23 09:09:16 +00:00
|
|
|
// Once we've fully transitioned to _matrix URLs, we can give people
|
|
|
|
// a grace period to update their configs, then use the rest url as
|
|
|
|
// a regular base url.
|
2023-05-17 11:50:00 +00:00
|
|
|
const parsedImRestUrl = parseUrl(this.apiUrl);
|
2019-07-22 17:54:04 +00:00
|
|
|
parsedImRestUrl.pathname = "";
|
2019-07-23 14:11:38 +00:00
|
|
|
return startTermsFlow(
|
2023-06-21 16:29:44 +00:00
|
|
|
MatrixClientPeg.safeGet(),
|
2023-05-17 11:50:00 +00:00
|
|
|
[new Service(SERVICE_TYPES.IM, parsedImRestUrl.toString(), token)],
|
2019-07-23 14:11:38 +00:00
|
|
|
this.termsInteractionCallback,
|
|
|
|
).then(() => {
|
2019-07-09 17:51:56 +00:00
|
|
|
return token;
|
|
|
|
});
|
2019-07-11 15:00:24 +00:00
|
|
|
} else {
|
|
|
|
throw e;
|
2019-07-09 17:51:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public registerForToken(): Promise<string> {
|
2017-12-17 03:04:32 +00:00
|
|
|
// Get openid bearer token from the HS as the first part of our dance
|
2023-06-21 16:29:44 +00:00
|
|
|
return MatrixClientPeg.safeGet()
|
2019-07-04 11:59:20 +00:00
|
|
|
.getOpenIdToken()
|
|
|
|
.then((tokenObject) => {
|
2016-09-02 15:03:24 +00:00
|
|
|
// Now we can send that to scalar and exchange it for a scalar token
|
2019-07-04 11:59:20 +00:00
|
|
|
return this.exchangeForScalarToken(tokenObject);
|
2019-08-09 22:05:05 +00:00
|
|
|
})
|
|
|
|
.then((token) => {
|
2019-07-09 17:51:56 +00:00
|
|
|
// Validate it (this mostly checks to see if the IM needs us to agree to some terms)
|
2021-04-26 13:02:53 +00:00
|
|
|
return this.checkToken(token);
|
2019-08-09 22:05:05 +00:00
|
|
|
})
|
|
|
|
.then((token) => {
|
|
|
|
this.scalarToken = token;
|
2021-04-26 13:02:53 +00:00
|
|
|
this.writeTokenToStore();
|
2019-08-09 22:05:05 +00:00
|
|
|
return token;
|
2016-09-02 15:03:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:59:07 +00:00
|
|
|
public async exchangeForScalarToken(openidTokenObject: IOpenIDToken): Promise<string> {
|
|
|
|
const scalarRestUrl = new URL(this.apiUrl + "/register");
|
|
|
|
scalarRestUrl.searchParams.set("v", imApiVersion);
|
|
|
|
|
|
|
|
const res = await fetch(scalarRestUrl, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(openidTokenObject),
|
2022-10-24 14:30:49 +00:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2018-10-12 02:54:53 +00:00
|
|
|
});
|
2022-10-12 17:59:07 +00:00
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(`Scalar request failed: ${res.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const body = await res.json();
|
|
|
|
if (!body?.scalar_token) {
|
|
|
|
throw new Error("Missing scalar_token in response");
|
|
|
|
}
|
|
|
|
|
|
|
|
return body.scalar_token;
|
2016-05-06 13:19:56 +00:00
|
|
|
}
|
2016-09-02 15:03:24 +00:00
|
|
|
|
2022-10-12 17:59:07 +00:00
|
|
|
public async getScalarPageTitle(url: string): Promise<string> {
|
|
|
|
const scalarPageLookupUrl = new URL(this.getStarterLink(this.apiUrl + "/widgets/title_lookup"));
|
|
|
|
scalarPageLookupUrl.searchParams.set("curl", encodeURIComponent(url));
|
|
|
|
|
|
|
|
const res = await fetch(scalarPageLookupUrl, {
|
|
|
|
method: "GET",
|
2018-10-12 02:54:53 +00:00
|
|
|
});
|
2022-10-12 17:59:07 +00:00
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(`Scalar request failed: ${res.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const body = await res.json();
|
|
|
|
return body?.page_title_cache_item?.cached_title;
|
2017-12-05 21:41:44 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 23:00:46 +00:00
|
|
|
/**
|
|
|
|
* Mark all assets associated with the specified widget as "disabled" in the
|
|
|
|
* integration manager database.
|
|
|
|
* This can be useful to temporarily prevent purchased assets from being displayed.
|
2020-04-21 22:01:10 +00:00
|
|
|
* @param {WidgetType} widgetType The Widget Type to disable assets for
|
|
|
|
* @param {string} widgetId The widget ID to disable assets for
|
2018-02-25 23:00:46 +00:00
|
|
|
* @return {Promise} Resolves on completion
|
|
|
|
*/
|
2022-10-12 17:59:07 +00:00
|
|
|
public async disableWidgetAssets(widgetType: WidgetType, widgetId: string): Promise<void> {
|
|
|
|
const url = new URL(this.getStarterLink(this.apiUrl + "/widgets/set_assets_state"));
|
|
|
|
url.searchParams.set("widget_type", widgetType.preferred);
|
|
|
|
url.searchParams.set("widget_id", widgetId);
|
|
|
|
url.searchParams.set("state", "disable");
|
|
|
|
|
|
|
|
const res = await fetch(url, {
|
|
|
|
method: "GET", // XXX: Actions shouldn't be GET requests
|
2018-02-24 00:10:28 +00:00
|
|
|
});
|
2022-10-12 17:59:07 +00:00
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(`Scalar request failed: ${res.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const body = await res.text();
|
|
|
|
if (!body) {
|
|
|
|
throw new Error("Failed to set widget assets state");
|
|
|
|
}
|
2018-02-24 00:10:28 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
public getScalarInterfaceUrlForRoom(room: Room, screen?: string, id?: string): string {
|
2018-02-09 11:44:27 +00:00
|
|
|
const roomId = room.roomId;
|
|
|
|
const roomName = room.name;
|
2019-08-09 22:05:05 +00:00
|
|
|
let url = this.uiUrl;
|
2023-04-25 08:28:48 +00:00
|
|
|
if (this.scalarToken) url += "?scalar_token=" + encodeURIComponent(this.scalarToken);
|
2016-09-02 15:03:24 +00:00
|
|
|
url += "&room_id=" + encodeURIComponent(roomId);
|
2018-02-09 11:44:27 +00:00
|
|
|
url += "&room_name=" + encodeURIComponent(roomName);
|
2017-12-17 03:13:27 +00:00
|
|
|
url += "&theme=" + encodeURIComponent(SettingsStore.getValue("theme"));
|
2017-08-18 17:40:00 +00:00
|
|
|
if (id) {
|
2017-08-22 08:59:27 +00:00
|
|
|
url += "&integ_id=" + encodeURIComponent(id);
|
2017-08-18 17:40:00 +00:00
|
|
|
}
|
2017-07-06 14:59:59 +00:00
|
|
|
if (screen) {
|
|
|
|
url += "&screen=" + encodeURIComponent(screen);
|
|
|
|
}
|
2016-09-02 15:03:24 +00:00
|
|
|
return url;
|
|
|
|
}
|
2016-09-02 15:36:43 +00:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public getStarterLink(starterLinkUrl: string): string {
|
2023-04-25 08:28:48 +00:00
|
|
|
if (!this.scalarToken) return starterLinkUrl;
|
2016-09-02 15:36:43 +00:00
|
|
|
return starterLinkUrl + "?scalar_token=" + encodeURIComponent(this.scalarToken);
|
|
|
|
}
|
2016-05-06 13:19:56 +00:00
|
|
|
}
|