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
Implement failover when server does not allow setting the Authorized-header (CORS)
This commit is contained in:
@@ -400,33 +400,46 @@ module.exports.MatrixHttpApi.prototype = {
|
|||||||
opts.headers = {};
|
opts.headers = {};
|
||||||
}
|
}
|
||||||
if (!opts.headers.Authorization) {
|
if (!opts.headers.Authorization) {
|
||||||
|
if (queryParams.access_token) {
|
||||||
|
opts.headers.Authorization = "Bearer " + queryParams.access_token;
|
||||||
|
delete queryParams.access_token;
|
||||||
|
} else {
|
||||||
opts.headers.Authorization = "Bearer " + this.opts.accessToken;
|
opts.headers.Authorization = "Bearer " + this.opts.accessToken;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (queryParams.access_token) {
|
||||||
|
delete queryParams.access_token;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!queryParams.access_token) {
|
if (!queryParams.access_token) {
|
||||||
queryParams.access_token = this.opts.accessToken;
|
queryParams.access_token = this.opts.accessToken;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const request_promise = this.request(
|
const requestPromise = this.request(
|
||||||
callback, method, path, queryParams, data, opts,
|
callback, method, path, queryParams, data, opts,
|
||||||
);
|
);
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
request_promise.catch(function(err) {
|
|
||||||
if (err.errcode == 'M_UNKNOWN_TOKEN') {
|
requestPromise.catch(function(err) {
|
||||||
if (self.authorization_header_supported === undefined) {
|
if (err.toString().indexOf("Error: CORS request rejected") != -1) {
|
||||||
self.authorization_header_supported = false;
|
self.authorization_header_supported = false;
|
||||||
return self.authedRequest(
|
queryParams.access_token = opts.headers.Authorization.substr(7);
|
||||||
callback, method, path, queryParams, data, opts);
|
delete opts.headers.Authorization;
|
||||||
|
const secondPromise = self.request(
|
||||||
|
callback, method, path, queryParams, data, opts,
|
||||||
|
);
|
||||||
|
requestPromise.abort = secondPromise.abort;
|
||||||
}
|
}
|
||||||
|
if (err.errcode == 'M_UNKNOWN_TOKEN') {
|
||||||
self.event_emitter.emit("Session.logged_out");
|
self.event_emitter.emit("Session.logged_out");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// return the original promise, otherwise tests break due to it having to
|
// return the original promise, otherwise tests break due to it having to
|
||||||
// go around the event loop one more time to process the result of the request
|
// go around the event loop one more time to process the result of the request
|
||||||
return request_promise;
|
return requestPromise;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user