2016-05-06 15:19:56 +02:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
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 14:58:14 +02:00
|
|
|
import Promise from 'bluebird';
|
2017-10-11 18:56:17 +02:00
|
|
|
const request = require('browser-request');
|
2016-05-06 15:19:56 +02:00
|
|
|
|
2017-10-11 18:56:17 +02:00
|
|
|
const SdkConfig = require('./SdkConfig');
|
|
|
|
const MatrixClientPeg = require('./MatrixClientPeg');
|
2016-05-06 15:19:56 +02:00
|
|
|
|
|
|
|
class ScalarAuthClient {
|
2016-09-02 17:03:24 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.scalarToken = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
return this.getScalarToken().then((tok) => {
|
|
|
|
this.scalarToken = tok;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
hasCredentials() {
|
|
|
|
return this.scalarToken != null; // undef or null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a scalar_token string
|
|
|
|
getScalarToken() {
|
2017-10-11 18:56:17 +02:00
|
|
|
const tok = window.localStorage.getItem("mx_scalar_token");
|
2017-07-12 15:02:00 +02:00
|
|
|
if (tok) return Promise.resolve(tok);
|
2016-09-02 17:03:24 +02:00
|
|
|
|
|
|
|
// No saved token, so do the dance to get one. First, we
|
|
|
|
// need an openid bearer token from the HS.
|
|
|
|
return MatrixClientPeg.get().getOpenIdToken().then((token_object) => {
|
|
|
|
// Now we can send that to scalar and exchange it for a scalar token
|
|
|
|
return this.exchangeForScalarToken(token_object);
|
|
|
|
}).then((token_object) => {
|
|
|
|
window.localStorage.setItem("mx_scalar_token", token_object);
|
|
|
|
return token_object;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exchangeForScalarToken(openid_token_object) {
|
2017-10-11 18:56:17 +02:00
|
|
|
const defer = Promise.defer();
|
2016-05-06 15:19:56 +02:00
|
|
|
|
2017-10-11 18:56:17 +02:00
|
|
|
const scalar_rest_url = SdkConfig.get().integrations_rest_url;
|
2016-05-06 15:19:56 +02:00
|
|
|
request({
|
|
|
|
method: 'POST',
|
|
|
|
uri: scalar_rest_url+'/register',
|
|
|
|
body: openid_token_object,
|
|
|
|
json: true,
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
defer.reject(err);
|
2016-06-06 18:55:45 +02:00
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
|
|
|
defer.reject({statusCode: response.statusCode});
|
2016-08-17 18:16:19 +02:00
|
|
|
} else if (!body || !body.scalar_token) {
|
|
|
|
defer.reject(new Error("Missing scalar_token in response"));
|
2016-05-06 15:19:56 +02:00
|
|
|
} else {
|
2016-08-17 18:16:19 +02:00
|
|
|
defer.resolve(body.scalar_token);
|
2016-05-06 15:19:56 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return defer.promise;
|
|
|
|
}
|
2016-09-02 17:03:24 +02:00
|
|
|
|
2017-12-05 22:41:44 +01:00
|
|
|
getScalarPageTitle(url) {
|
|
|
|
const defer = Promise.defer();
|
|
|
|
|
2017-12-08 19:47:00 +01:00
|
|
|
let scalarPageLookupUrl = SdkConfig.get().integrations_rest_url + '/widgets/title_lookup';
|
2017-12-05 22:41:44 +01:00
|
|
|
scalarPageLookupUrl = this.getStarterLink(scalarPageLookupUrl);
|
|
|
|
scalarPageLookupUrl += '&curl=' + encodeURIComponent(url);
|
|
|
|
request({
|
|
|
|
method: 'GET',
|
|
|
|
uri: scalarPageLookupUrl,
|
|
|
|
json: true,
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
defer.reject(err);
|
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
|
|
|
defer.reject({statusCode: response.statusCode});
|
|
|
|
} else if (!body) {
|
2017-12-05 22:57:28 +01:00
|
|
|
defer.reject(new Error("Missing page title in response"));
|
2017-12-05 22:41:44 +01:00
|
|
|
} else {
|
|
|
|
let title = "";
|
|
|
|
if (body.page_title_cache_item && body.page_title_cache_item.cached_title) {
|
|
|
|
title = body.page_title_cache_item.cached_title;
|
|
|
|
}
|
|
|
|
defer.resolve(title);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return defer.promise;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:40:00 +02:00
|
|
|
getScalarInterfaceUrlForRoom(roomId, screen, id) {
|
2017-10-11 18:56:17 +02:00
|
|
|
let url = SdkConfig.get().integrations_ui_url;
|
2016-09-02 17:03:24 +02:00
|
|
|
url += "?scalar_token=" + encodeURIComponent(this.scalarToken);
|
|
|
|
url += "&room_id=" + encodeURIComponent(roomId);
|
2017-08-18 19:40:00 +02:00
|
|
|
if (id) {
|
2017-08-22 10:59:27 +02:00
|
|
|
url += '&integ_id=' + encodeURIComponent(id);
|
2017-08-18 19:40:00 +02:00
|
|
|
}
|
2017-07-06 16:59:59 +02:00
|
|
|
if (screen) {
|
|
|
|
url += '&screen=' + encodeURIComponent(screen);
|
|
|
|
}
|
2016-09-02 17:03:24 +02:00
|
|
|
return url;
|
|
|
|
}
|
2016-09-02 17:36:43 +02:00
|
|
|
|
|
|
|
getStarterLink(starterLinkUrl) {
|
|
|
|
return starterLinkUrl + "?scalar_token=" + encodeURIComponent(this.scalarToken);
|
|
|
|
}
|
2016-05-06 15:19:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScalarAuthClient;
|