You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
remove fallback to query-params and set Authorization-Header based on construcor-option
This commit is contained in:
@@ -74,12 +74,18 @@ module.exports.PREFIX_MEDIA_R0 = "/_matrix/media/r0";
|
|||||||
* requests.
|
* requests.
|
||||||
* @param {Number=} opts.localTimeoutMs The default maximum amount of time to wait
|
* @param {Number=} opts.localTimeoutMs The default maximum amount of time to wait
|
||||||
* before timing out the request. If not specified, there is no timeout.
|
* before timing out the request. If not specified, there is no timeout.
|
||||||
|
* @param {bool=} opts.useAuthorizationHeader True to use Authorization header instead of
|
||||||
|
* query param to send the access token to the server. Defaults to false
|
||||||
*/
|
*/
|
||||||
module.exports.MatrixHttpApi = function MatrixHttpApi(event_emitter, opts) {
|
module.exports.MatrixHttpApi = function MatrixHttpApi(event_emitter, opts) {
|
||||||
utils.checkObjectHasKeys(opts, ["baseUrl", "request", "prefix"]);
|
utils.checkObjectHasKeys(opts, ["baseUrl", "request", "prefix"]);
|
||||||
opts.onlyData = opts.onlyData || false;
|
opts.onlyData = opts.onlyData || false;
|
||||||
this.event_emitter = event_emitter;
|
this.event_emitter = event_emitter;
|
||||||
this.opts = opts;
|
this.opts = opts;
|
||||||
|
this.useAuthorizationHeader = false;
|
||||||
|
if (opts.useAuthorizationHeader) {
|
||||||
|
this.useAuthorizationHeader = Boolean(opts.useAuthorizationHeader);
|
||||||
|
}
|
||||||
this.uploads = [];
|
this.uploads = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -385,8 +391,7 @@ module.exports.MatrixHttpApi.prototype = {
|
|||||||
if (!queryParams) {
|
if (!queryParams) {
|
||||||
queryParams = {};
|
queryParams = {};
|
||||||
}
|
}
|
||||||
if (this.authorizationHeaderSupported === undefined ||
|
if (this.useAuthorizationHeader) {
|
||||||
this.authorizationHeaderSupported) {
|
|
||||||
if (isFinite(opts)) {
|
if (isFinite(opts)) {
|
||||||
// opts used to be localTimeoutMs
|
// opts used to be localTimeoutMs
|
||||||
opts = {
|
opts = {
|
||||||
@@ -400,12 +405,7 @@ module.exports.MatrixHttpApi.prototype = {
|
|||||||
opts.headers = {};
|
opts.headers = {};
|
||||||
}
|
}
|
||||||
if (!opts.headers.Authorization) {
|
if (!opts.headers.Authorization) {
|
||||||
if (queryParams.access_token) {
|
opts.headers.Authorization = "Bearer " + this.opts.accessToken;
|
||||||
opts.headers.Authorization = "Bearer " + queryParams.access_token;
|
|
||||||
delete queryParams.access_token;
|
|
||||||
} else {
|
|
||||||
opts.headers.Authorization = "Bearer " + this.opts.accessToken;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (queryParams.access_token) {
|
if (queryParams.access_token) {
|
||||||
delete queryParams.access_token;
|
delete queryParams.access_token;
|
||||||
@@ -421,44 +421,6 @@ module.exports.MatrixHttpApi.prototype = {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
if (this.authorizationHeaderSupported === undefined) {
|
|
||||||
const defer = q.defer();
|
|
||||||
const returnPromise = defer.promise;
|
|
||||||
returnPromise.abort = requestPromise.abort;
|
|
||||||
|
|
||||||
requestPromise.then((resp) => {
|
|
||||||
self.authorizationHeaderSupported = true;
|
|
||||||
defer.resolve(resp);
|
|
||||||
}, (err) => {
|
|
||||||
if (err.errcode == 'M_MISSING_TOKEN' ||
|
|
||||||
err.toString().indexOf("Error: CORS request rejected") != -1) {
|
|
||||||
self.authorizationHeaderSupported = false;
|
|
||||||
queryParams.access_token = opts.headers.Authorization.substr(7);
|
|
||||||
delete opts.headers.Authorization;
|
|
||||||
const secondPromise = self.request(
|
|
||||||
callback, method, path, queryParams, data, opts,
|
|
||||||
);
|
|
||||||
returnPromise.abort = secondPromise.abort;
|
|
||||||
secondPromise.then((resp) => {
|
|
||||||
defer.resolve(resp);
|
|
||||||
}, (err) => {
|
|
||||||
if (err.errcode == 'M_UNKNOWN_TOKEN') {
|
|
||||||
self.event_emitter.emit("Session.logged_out");
|
|
||||||
}
|
|
||||||
defer.reject(err);
|
|
||||||
});
|
|
||||||
} else if (err.errcode == 'M_UNKNOWN_TOKEN') {
|
|
||||||
self.event_emitter.emit("Session.logged_out");
|
|
||||||
defer.reject(err);
|
|
||||||
} else {
|
|
||||||
defer.reject(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return returnPromise;
|
|
||||||
}
|
|
||||||
|
|
||||||
requestPromise.catch(function(err) {
|
requestPromise.catch(function(err) {
|
||||||
if (err.errcode == 'M_UNKNOWN_TOKEN') {
|
if (err.errcode == 'M_UNKNOWN_TOKEN') {
|
||||||
self.event_emitter.emit("Session.logged_out");
|
self.event_emitter.emit("Session.logged_out");
|
||||||
|
|||||||
Reference in New Issue
Block a user