2016-05-06 16:19:56 +03:00
|
|
|
/*
|
2021-04-06 14:26:50 +03:00
|
|
|
Copyright 2016, 2019, 2021 The Matrix.org Foundation C.I.C.
|
2016-05-06 16:19:56 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-07-22 20:54:04 +03:00
|
|
|
import url from 'url';
|
2017-12-17 06:13:27 +03:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2021-04-06 14:26:50 +03:00
|
|
|
import { Service, startTermsFlow, TermsInteractionCallback, TermsNotSignedError } from './Terms';
|
2021-06-29 15:11:58 +03:00
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
2019-12-21 00:41:07 +03:00
|
|
|
import request from "browser-request";
|
2016-05-06 16:19:56 +03:00
|
|
|
|
2019-12-13 00:37:32 +03:00
|
|
|
import SdkConfig from "./SdkConfig";
|
2021-06-29 15:11:58 +03:00
|
|
|
import { WidgetType } from "./widgets/WidgetType";
|
|
|
|
import { SERVICE_TYPES } from "matrix-js-sdk/src/service-types";
|
2021-04-27 13:22:17 +03:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2019-07-09 20:51:56 +03:00
|
|
|
|
2019-03-13 13:16:44 +03:00
|
|
|
// The version of the integration manager API we're intending to work with
|
|
|
|
const imApiVersion = "1.1";
|
|
|
|
|
2020-04-22 09:34:08 +03:00
|
|
|
// TODO: Generify the name of this class and all components within - it's not just for Scalar.
|
|
|
|
|
2019-08-10 01:05:05 +03:00
|
|
|
export default class ScalarAuthClient {
|
2021-04-06 14:26:50 +03:00
|
|
|
private scalarToken: string;
|
|
|
|
private termsInteractionCallback: TermsInteractionCallback;
|
|
|
|
private isDefaultManager: boolean;
|
|
|
|
|
2021-04-26 16:54:36 +03:00
|
|
|
constructor(private apiUrl: string, private uiUrl: string) {
|
2016-09-02 18:03:24 +03:00
|
|
|
this.scalarToken = null;
|
2019-07-23 17:11:38 +03:00
|
|
|
// `undefined` to allow `startTermsFlow` to fallback to a default
|
|
|
|
// callback if this is unset.
|
|
|
|
this.termsInteractionCallback = undefined;
|
2019-08-10 01:05:05 +03:00
|
|
|
|
|
|
|
// We try and store the token on a per-manager basis, but need a fallback
|
|
|
|
// for the default manager.
|
|
|
|
const configApiUrl = SdkConfig.get()['integrations_rest_url'];
|
|
|
|
const configUiUrl = SdkConfig.get()['integrations_ui_url'];
|
|
|
|
this.isDefaultManager = apiUrl === configApiUrl && configUiUrl === uiUrl;
|
2016-09-02 18:03:24 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 16:02:53 +03:00
|
|
|
private writeTokenToStore() {
|
2019-08-10 01:05:05 +03:00
|
|
|
window.localStorage.setItem("mx_scalar_token_at_" + this.apiUrl, this.scalarToken);
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
private readTokenFromStore(): string {
|
2019-08-10 01:05:05 +03: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;
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
private readToken(): string {
|
2019-08-10 01:05:05 +03:00
|
|
|
if (this.scalarToken) return this.scalarToken;
|
2021-04-26 16:02:53 +03:00
|
|
|
return this.readTokenFromStore();
|
2019-06-18 00:29:19 +03:00
|
|
|
}
|
|
|
|
|
2019-07-23 17:11:38 +03:00
|
|
|
setTermsInteractionCallback(callback) {
|
|
|
|
this.termsInteractionCallback = callback;
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
connect(): Promise<void> {
|
2016-09-02 18:03:24 +03:00
|
|
|
return this.getScalarToken().then((tok) => {
|
|
|
|
this.scalarToken = tok;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
hasCredentials(): boolean {
|
2016-09-02 18:03:24 +03:00
|
|
|
return this.scalarToken != null; // undef or null
|
|
|
|
}
|
|
|
|
|
2019-07-11 18:00:24 +03:00
|
|
|
// Returns a promise that resolves to a scalar_token string
|
2021-04-27 13:22:17 +03:00
|
|
|
getScalarToken(): Promise<string> {
|
2021-04-26 16:02:53 +03:00
|
|
|
const token = this.readToken();
|
2016-09-02 18:03:24 +03:00
|
|
|
|
2017-12-17 06:04:32 +03:00
|
|
|
if (!token) {
|
|
|
|
return this.registerForToken();
|
|
|
|
} else {
|
2021-04-26 16:02:53 +03:00
|
|
|
return this.checkToken(token).catch((e) => {
|
2019-07-15 16:05:39 +03:00
|
|
|
if (e instanceof TermsNotSignedError) {
|
|
|
|
// retrying won't help this
|
|
|
|
throw e;
|
|
|
|
}
|
2019-07-11 18:00:24 +03:00
|
|
|
return this.registerForToken();
|
|
|
|
});
|
2017-12-17 06:04:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
private getAccountName(token: string): Promise<string> {
|
2019-08-10 01:05:05 +03:00
|
|
|
const url = this.apiUrl + "/account";
|
2017-12-17 06:04:32 +03:00
|
|
|
|
2018-10-02 03:57:27 +03:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
request({
|
|
|
|
method: "GET",
|
|
|
|
uri: url,
|
2021-06-29 15:11:58 +03:00
|
|
|
qs: { scalar_token: token, v: imApiVersion },
|
2018-10-02 03:57:27 +03:00
|
|
|
json: true,
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
2019-07-09 20:51:56 +03:00
|
|
|
} else if (body && body.errcode === 'M_TERMS_NOT_SIGNED') {
|
|
|
|
reject(new TermsNotSignedError());
|
2018-10-02 03:57:27 +03:00
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
2019-07-09 20:51:56 +03:00
|
|
|
reject(body);
|
2018-10-02 03:57:27 +03:00
|
|
|
} else if (!body || !body.user_id) {
|
|
|
|
reject(new Error("Missing user_id in response"));
|
|
|
|
} else {
|
|
|
|
resolve(body.user_id);
|
|
|
|
}
|
|
|
|
});
|
2018-10-12 05:54:53 +03:00
|
|
|
});
|
2017-12-17 06:04:32 +03:00
|
|
|
}
|
2016-09-02 18:03:24 +03:00
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
private checkToken(token: string): Promise<string> {
|
2021-04-26 16:02:53 +03:00
|
|
|
return this.getAccountName(token).then(userId => {
|
2019-07-09 20:51:56 +03:00
|
|
|
const me = MatrixClientPeg.get().getUserId();
|
|
|
|
if (userId !== me) {
|
|
|
|
throw new Error("Scalar token is owned by someone else: " + me);
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}).catch((e) => {
|
|
|
|
if (e instanceof TermsNotSignedError) {
|
2019-08-10 01:05:05 +03:00
|
|
|
console.log("Integration manager requires new terms to be agreed to");
|
2019-07-22 20:54:04 +03: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
|
2019-07-23 12:09:16 +03: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.
|
|
|
|
|
|
|
|
// 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.
|
2019-08-10 01:05:05 +03:00
|
|
|
const parsedImRestUrl = url.parse(this.apiUrl);
|
2019-07-22 20:54:04 +03:00
|
|
|
parsedImRestUrl.path = '';
|
|
|
|
parsedImRestUrl.pathname = '';
|
2019-07-23 17:11:38 +03:00
|
|
|
return startTermsFlow([new Service(
|
2021-03-19 05:50:34 +03:00
|
|
|
SERVICE_TYPES.IM,
|
2021-04-06 14:26:50 +03:00
|
|
|
url.format(parsedImRestUrl),
|
2019-07-09 20:51:56 +03:00
|
|
|
token,
|
2019-07-23 17:11:38 +03:00
|
|
|
)], this.termsInteractionCallback).then(() => {
|
2019-07-09 20:51:56 +03:00
|
|
|
return token;
|
|
|
|
});
|
2019-07-11 18:00:24 +03:00
|
|
|
} else {
|
|
|
|
throw e;
|
2019-07-09 20:51:56 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
registerForToken(): Promise<string> {
|
2017-12-17 06:04:32 +03:00
|
|
|
// Get openid bearer token from the HS as the first part of our dance
|
2019-07-04 14:59:20 +03:00
|
|
|
return MatrixClientPeg.get().getOpenIdToken().then((tokenObject) => {
|
2016-09-02 18:03:24 +03:00
|
|
|
// Now we can send that to scalar and exchange it for a scalar token
|
2019-07-04 14:59:20 +03:00
|
|
|
return this.exchangeForScalarToken(tokenObject);
|
2019-08-10 01:05:05 +03:00
|
|
|
}).then((token) => {
|
2019-07-09 20:51:56 +03:00
|
|
|
// Validate it (this mostly checks to see if the IM needs us to agree to some terms)
|
2021-04-26 16:02:53 +03:00
|
|
|
return this.checkToken(token);
|
2019-08-10 01:05:05 +03:00
|
|
|
}).then((token) => {
|
|
|
|
this.scalarToken = token;
|
2021-04-26 16:02:53 +03:00
|
|
|
this.writeTokenToStore();
|
2019-08-10 01:05:05 +03:00
|
|
|
return token;
|
2016-09-02 18:03:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
exchangeForScalarToken(openidTokenObject: any): Promise<string> {
|
2019-08-10 01:05:05 +03:00
|
|
|
const scalarRestUrl = this.apiUrl;
|
2016-05-06 16:19:56 +03:00
|
|
|
|
2018-10-02 03:57:27 +03:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
request({
|
|
|
|
method: 'POST',
|
2019-07-04 14:59:20 +03:00
|
|
|
uri: scalarRestUrl + '/register',
|
2021-06-29 15:11:58 +03:00
|
|
|
qs: { v: imApiVersion },
|
2019-07-04 14:59:20 +03:00
|
|
|
body: openidTokenObject,
|
2018-10-02 03:57:27 +03:00
|
|
|
json: true,
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
2021-04-01 16:15:21 +03:00
|
|
|
reject(new Error(`Scalar request failed: ${response.statusCode}`));
|
2018-10-02 03:57:27 +03:00
|
|
|
} else if (!body || !body.scalar_token) {
|
|
|
|
reject(new Error("Missing scalar_token in response"));
|
|
|
|
} else {
|
|
|
|
resolve(body.scalar_token);
|
|
|
|
}
|
|
|
|
});
|
2018-10-12 05:54:53 +03:00
|
|
|
});
|
2016-05-06 16:19:56 +03:00
|
|
|
}
|
2016-09-02 18:03:24 +03:00
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
getScalarPageTitle(url: string): Promise<string> {
|
2019-08-10 01:05:05 +03:00
|
|
|
let scalarPageLookupUrl = this.apiUrl + '/widgets/title_lookup';
|
2017-12-06 00:41:44 +03:00
|
|
|
scalarPageLookupUrl = this.getStarterLink(scalarPageLookupUrl);
|
|
|
|
scalarPageLookupUrl += '&curl=' + encodeURIComponent(url);
|
|
|
|
|
2018-10-02 03:57:27 +03:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
request({
|
|
|
|
method: 'GET',
|
|
|
|
uri: scalarPageLookupUrl,
|
|
|
|
json: true,
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
2021-04-01 16:15:21 +03:00
|
|
|
reject(new Error(`Scalar request failed: ${response.statusCode}`));
|
2018-10-02 03:57:27 +03:00
|
|
|
} else if (!body) {
|
|
|
|
reject(new Error("Missing page title in response"));
|
|
|
|
} else {
|
|
|
|
let title = "";
|
|
|
|
if (body.page_title_cache_item && body.page_title_cache_item.cached_title) {
|
|
|
|
title = body.page_title_cache_item.cached_title;
|
|
|
|
}
|
|
|
|
resolve(title);
|
|
|
|
}
|
|
|
|
});
|
2018-10-12 05:54:53 +03:00
|
|
|
});
|
2017-12-06 00:41:44 +03:00
|
|
|
}
|
|
|
|
|
2018-02-26 02:00:46 +03: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-22 01:01:10 +03:00
|
|
|
* @param {WidgetType} widgetType The Widget Type to disable assets for
|
|
|
|
* @param {string} widgetId The widget ID to disable assets for
|
2018-02-26 02:00:46 +03:00
|
|
|
* @return {Promise} Resolves on completion
|
|
|
|
*/
|
2021-04-27 13:22:17 +03:00
|
|
|
disableWidgetAssets(widgetType: WidgetType, widgetId: string): Promise<void> {
|
2019-08-10 01:05:05 +03:00
|
|
|
let url = this.apiUrl + '/widgets/set_assets_state';
|
2018-02-24 03:10:28 +03:00
|
|
|
url = this.getStarterLink(url);
|
2021-04-06 14:26:50 +03:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2018-02-24 03:10:28 +03:00
|
|
|
request({
|
2020-04-22 01:01:10 +03:00
|
|
|
method: 'GET', // XXX: Actions shouldn't be GET requests
|
2018-02-24 03:10:28 +03:00
|
|
|
uri: url,
|
|
|
|
json: true,
|
|
|
|
qs: {
|
2020-04-22 01:01:10 +03:00
|
|
|
'widget_type': widgetType.preferred,
|
2018-02-24 03:10:28 +03:00
|
|
|
'widget_id': widgetId,
|
|
|
|
'state': 'disable',
|
|
|
|
},
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
2021-04-01 16:15:21 +03:00
|
|
|
reject(new Error(`Scalar request failed: ${response.statusCode}`));
|
2018-02-24 03:10:28 +03:00
|
|
|
} else if (!body) {
|
|
|
|
reject(new Error("Failed to set widget assets state"));
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
getScalarInterfaceUrlForRoom(room: Room, screen: string, id: string): string {
|
2018-02-09 14:44:27 +03:00
|
|
|
const roomId = room.roomId;
|
|
|
|
const roomName = room.name;
|
2019-08-10 01:05:05 +03:00
|
|
|
let url = this.uiUrl;
|
2016-09-02 18:03:24 +03:00
|
|
|
url += "?scalar_token=" + encodeURIComponent(this.scalarToken);
|
|
|
|
url += "&room_id=" + encodeURIComponent(roomId);
|
2018-02-09 14:44:27 +03:00
|
|
|
url += "&room_name=" + encodeURIComponent(roomName);
|
2017-12-17 06:13:27 +03:00
|
|
|
url += "&theme=" + encodeURIComponent(SettingsStore.getValue("theme"));
|
2017-08-18 20:40:00 +03:00
|
|
|
if (id) {
|
2017-08-22 11:59:27 +03:00
|
|
|
url += '&integ_id=' + encodeURIComponent(id);
|
2017-08-18 20:40:00 +03:00
|
|
|
}
|
2017-07-06 17:59:59 +03:00
|
|
|
if (screen) {
|
|
|
|
url += '&screen=' + encodeURIComponent(screen);
|
|
|
|
}
|
2016-09-02 18:03:24 +03:00
|
|
|
return url;
|
|
|
|
}
|
2016-09-02 18:36:43 +03:00
|
|
|
|
2021-04-27 13:22:17 +03:00
|
|
|
getStarterLink(starterLinkUrl: string): string {
|
2016-09-02 18:36:43 +03:00
|
|
|
return starterLinkUrl + "?scalar_token=" + encodeURIComponent(this.scalarToken);
|
|
|
|
}
|
2016-05-06 16:19:56 +03:00
|
|
|
}
|