2016-05-06 21:19:56 +08:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2019-07-10 01:51:56 +08:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2016-05-06 21:19:56 +08: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.
|
|
|
|
*/
|
|
|
|
|
2017-07-12 20:58:14 +08:00
|
|
|
import Promise from 'bluebird';
|
2017-12-17 11:13:27 +08:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2019-07-10 01:51:56 +08:00
|
|
|
import { Service, presentTermsForServices, TermsNotSignedError } from './Terms';
|
2017-10-12 00:56:17 +08:00
|
|
|
const request = require('browser-request');
|
2016-05-06 21:19:56 +08:00
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
const SdkConfig = require('./SdkConfig');
|
|
|
|
const MatrixClientPeg = require('./MatrixClientPeg');
|
2016-05-06 21:19:56 +08:00
|
|
|
|
2019-07-10 01:51:56 +08:00
|
|
|
import * as Matrix from 'matrix-js-sdk';
|
|
|
|
|
2019-03-13 18:16:44 +08:00
|
|
|
// The version of the integration manager API we're intending to work with
|
|
|
|
const imApiVersion = "1.1";
|
|
|
|
|
2016-05-06 21:19:56 +08:00
|
|
|
class ScalarAuthClient {
|
2016-09-02 23:03:24 +08:00
|
|
|
constructor() {
|
|
|
|
this.scalarToken = null;
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:29:19 +08:00
|
|
|
/**
|
|
|
|
* Determines if setting up a ScalarAuthClient is even possible
|
|
|
|
* @returns {boolean} true if possible, false otherwise.
|
|
|
|
*/
|
|
|
|
static isPossible() {
|
|
|
|
return SdkConfig.get()['integrations_rest_url'] && SdkConfig.get()['integrations_ui_url'];
|
|
|
|
}
|
|
|
|
|
2016-09-02 23:03:24 +08:00
|
|
|
connect() {
|
|
|
|
return this.getScalarToken().then((tok) => {
|
|
|
|
this.scalarToken = tok;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
hasCredentials() {
|
|
|
|
return this.scalarToken != null; // undef or null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a scalar_token string
|
|
|
|
getScalarToken() {
|
2019-06-18 05:29:19 +08:00
|
|
|
let token = this.scalarToken;
|
|
|
|
if (!token) token = window.localStorage.getItem("mx_scalar_token");
|
2016-09-02 23:03:24 +08:00
|
|
|
|
2017-12-17 11:04:32 +08:00
|
|
|
if (!token) {
|
|
|
|
return this.registerForToken();
|
|
|
|
} else {
|
2019-07-10 01:51:56 +08:00
|
|
|
return this._checkToken(token);
|
2017-12-17 11:04:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 01:51:56 +08:00
|
|
|
_getAccountName(token) {
|
2018-10-12 10:54:53 +08:00
|
|
|
const url = SdkConfig.get().integrations_rest_url + "/account";
|
2017-12-17 11:04:32 +08:00
|
|
|
|
2018-10-02 08:57:27 +08:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
request({
|
|
|
|
method: "GET",
|
|
|
|
uri: url,
|
2019-03-13 18:16:44 +08:00
|
|
|
qs: {scalar_token: token, v: imApiVersion},
|
2018-10-02 08:57:27 +08:00
|
|
|
json: true,
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
2019-07-10 01:51:56 +08:00
|
|
|
} else if (body && body.errcode === 'M_TERMS_NOT_SIGNED') {
|
|
|
|
reject(new TermsNotSignedError());
|
2018-10-02 08:57:27 +08:00
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
2019-07-10 01:51:56 +08:00
|
|
|
reject(body);
|
2018-10-02 08:57:27 +08:00
|
|
|
} else if (!body || !body.user_id) {
|
|
|
|
reject(new Error("Missing user_id in response"));
|
|
|
|
} else {
|
|
|
|
resolve(body.user_id);
|
|
|
|
}
|
|
|
|
});
|
2018-10-12 10:54:53 +08:00
|
|
|
});
|
2017-12-17 11:04:32 +08:00
|
|
|
}
|
2016-09-02 23:03:24 +08:00
|
|
|
|
2019-07-10 01:51:56 +08:00
|
|
|
_checkToken(token) {
|
|
|
|
return this._getAccountName(token).then(userId => {
|
|
|
|
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) {
|
|
|
|
console.log("Integrations manager requires new terms to be agreed to");
|
|
|
|
return presentTermsForServices([new Service(
|
2019-07-10 19:08:26 +08:00
|
|
|
Matrix.SERVICE_TYPES.IM,
|
2019-07-10 01:51:56 +08:00
|
|
|
SdkConfig.get().integrations_rest_url,
|
|
|
|
token,
|
|
|
|
)]).then(() => {
|
|
|
|
return token;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-17 11:04:32 +08:00
|
|
|
registerForToken() {
|
|
|
|
// Get openid bearer token from the HS as the first part of our dance
|
2019-07-04 19:59:20 +08:00
|
|
|
return MatrixClientPeg.get().getOpenIdToken().then((tokenObject) => {
|
2016-09-02 23:03:24 +08:00
|
|
|
// Now we can send that to scalar and exchange it for a scalar token
|
2019-07-04 19:59:20 +08:00
|
|
|
return this.exchangeForScalarToken(tokenObject);
|
2019-07-10 01:51:56 +08:00
|
|
|
}).then((tokenObject) => {
|
|
|
|
// Validate it (this mostly checks to see if the IM needs us to agree to some terms)
|
|
|
|
return this._checkToken(tokenObject);
|
2019-07-04 19:59:20 +08:00
|
|
|
}).then((tokenObject) => {
|
|
|
|
window.localStorage.setItem("mx_scalar_token", tokenObject);
|
|
|
|
return tokenObject;
|
2016-09-02 23:03:24 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-04 19:59:20 +08:00
|
|
|
exchangeForScalarToken(openidTokenObject) {
|
|
|
|
const scalarRestUrl = SdkConfig.get().integrations_rest_url;
|
2016-05-06 21:19:56 +08:00
|
|
|
|
2018-10-02 08:57:27 +08:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
request({
|
|
|
|
method: 'POST',
|
2019-07-04 19:59:20 +08:00
|
|
|
uri: scalarRestUrl + '/register',
|
2019-03-13 18:16:44 +08:00
|
|
|
qs: {v: imApiVersion},
|
2019-07-04 19:59:20 +08:00
|
|
|
body: openidTokenObject,
|
2018-10-02 08:57:27 +08:00
|
|
|
json: true,
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
|
|
|
reject({statusCode: response.statusCode});
|
|
|
|
} else if (!body || !body.scalar_token) {
|
|
|
|
reject(new Error("Missing scalar_token in response"));
|
|
|
|
} else {
|
|
|
|
resolve(body.scalar_token);
|
|
|
|
}
|
|
|
|
});
|
2018-10-12 10:54:53 +08:00
|
|
|
});
|
2016-05-06 21:19:56 +08:00
|
|
|
}
|
2016-09-02 23:03:24 +08:00
|
|
|
|
2017-12-06 05:41:44 +08:00
|
|
|
getScalarPageTitle(url) {
|
2017-12-09 02:47:00 +08:00
|
|
|
let scalarPageLookupUrl = SdkConfig.get().integrations_rest_url + '/widgets/title_lookup';
|
2017-12-06 05:41:44 +08:00
|
|
|
scalarPageLookupUrl = this.getStarterLink(scalarPageLookupUrl);
|
|
|
|
scalarPageLookupUrl += '&curl=' + encodeURIComponent(url);
|
|
|
|
|
2018-10-02 08:57:27 +08: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) {
|
|
|
|
reject({statusCode: response.statusCode});
|
|
|
|
} 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 10:54:53 +08:00
|
|
|
});
|
2017-12-06 05:41:44 +08:00
|
|
|
}
|
|
|
|
|
2018-02-26 07:00:46 +08: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.
|
|
|
|
* @param {string} widgetType [description]
|
|
|
|
* @param {string} widgetId [description]
|
|
|
|
* @return {Promise} Resolves on completion
|
|
|
|
*/
|
2018-02-24 08:10:28 +08:00
|
|
|
disableWidgetAssets(widgetType, widgetId) {
|
|
|
|
let url = SdkConfig.get().integrations_rest_url + '/widgets/set_assets_state';
|
|
|
|
url = this.getStarterLink(url);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request({
|
|
|
|
method: 'GET',
|
|
|
|
uri: url,
|
|
|
|
json: true,
|
|
|
|
qs: {
|
|
|
|
'widget_type': widgetType,
|
|
|
|
'widget_id': widgetId,
|
|
|
|
'state': 'disable',
|
|
|
|
},
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
|
|
|
reject({statusCode: response.statusCode});
|
|
|
|
} else if (!body) {
|
|
|
|
reject(new Error("Failed to set widget assets state"));
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-09 19:44:27 +08:00
|
|
|
getScalarInterfaceUrlForRoom(room, screen, id) {
|
|
|
|
const roomId = room.roomId;
|
|
|
|
const roomName = room.name;
|
2017-10-12 00:56:17 +08:00
|
|
|
let url = SdkConfig.get().integrations_ui_url;
|
2016-09-02 23:03:24 +08:00
|
|
|
url += "?scalar_token=" + encodeURIComponent(this.scalarToken);
|
|
|
|
url += "&room_id=" + encodeURIComponent(roomId);
|
2018-02-09 19:44:27 +08:00
|
|
|
url += "&room_name=" + encodeURIComponent(roomName);
|
2017-12-17 11:13:27 +08:00
|
|
|
url += "&theme=" + encodeURIComponent(SettingsStore.getValue("theme"));
|
2017-08-19 01:40:00 +08:00
|
|
|
if (id) {
|
2017-08-22 16:59:27 +08:00
|
|
|
url += '&integ_id=' + encodeURIComponent(id);
|
2017-08-19 01:40:00 +08:00
|
|
|
}
|
2017-07-06 22:59:59 +08:00
|
|
|
if (screen) {
|
|
|
|
url += '&screen=' + encodeURIComponent(screen);
|
|
|
|
}
|
2016-09-02 23:03:24 +08:00
|
|
|
return url;
|
|
|
|
}
|
2016-09-02 23:36:43 +08:00
|
|
|
|
|
|
|
getStarterLink(starterLinkUrl) {
|
|
|
|
return starterLinkUrl + "?scalar_token=" + encodeURIComponent(this.scalarToken);
|
|
|
|
}
|
2016-05-06 21:19:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScalarAuthClient;
|