2016-05-06 13:19:56 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var q = require("q");
|
|
|
|
var request = require('browser-request');
|
|
|
|
|
|
|
|
var SdkConfig = require('./SdkConfig');
|
2016-09-02 15:03:24 +00:00
|
|
|
var MatrixClientPeg = require('./MatrixClientPeg');
|
2016-05-06 13:19:56 +00:00
|
|
|
|
|
|
|
class ScalarAuthClient {
|
2016-09-02 15:03:24 +00: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() {
|
|
|
|
var tok = window.localStorage.getItem("mx_scalar_token");
|
|
|
|
if (tok) return q(tok);
|
|
|
|
|
|
|
|
// 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) {
|
2016-05-06 13:19:56 +00:00
|
|
|
var defer = q.defer();
|
|
|
|
|
|
|
|
var scalar_rest_url = SdkConfig.get().integrations_rest_url;
|
|
|
|
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 16:55:45 +00:00
|
|
|
} else if (response.statusCode / 100 !== 2) {
|
|
|
|
defer.reject({statusCode: response.statusCode});
|
2016-08-17 16:16:19 +00:00
|
|
|
} else if (!body || !body.scalar_token) {
|
|
|
|
defer.reject(new Error("Missing scalar_token in response"));
|
2016-05-06 13:19:56 +00:00
|
|
|
} else {
|
2016-08-17 16:16:19 +00:00
|
|
|
defer.resolve(body.scalar_token);
|
2016-05-06 13:19:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return defer.promise;
|
|
|
|
}
|
2016-09-02 15:03:24 +00:00
|
|
|
|
|
|
|
getScalarInterfaceUrlForRoom(roomId) {
|
|
|
|
var url = SdkConfig.get().integrations_ui_url;
|
|
|
|
url += "?scalar_token=" + encodeURIComponent(this.scalarToken);
|
|
|
|
url += "&room_id=" + encodeURIComponent(roomId);
|
|
|
|
return url;
|
|
|
|
}
|
2016-05-06 13:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScalarAuthClient;
|
|
|
|
|