Skip to content

Commit

Permalink
intercept first authedRequest to determine if accessToken can be send…
Browse files Browse the repository at this point in the history
… by header (clearer structure)
  • Loading branch information
krombel committed Jul 4, 2017
1 parent 07868f7 commit 2aececb
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/http-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,18 +422,44 @@ module.exports.MatrixHttpApi.prototype = {

const self = this;

if (this.authorization_header_supported === undefined) {
const defer = q.defer();
const returnPromise = defer.promise;
returnPromise.abort = requestPromise.abort;

requestPromise.then((resp) => {
self.authorization_header_supported = true;
defer.resolve(resp);
}, (err) => {
if (err.errcode == 'M_MISSING_TOKEN' ||
err.toString().indexOf("Error: CORS request rejected") != -1) {
self.authorization_header_supported = 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) {
if (err.errcode == 'M_MISSING_TOKEN' ||
err.toString().indexOf("Error: CORS request rejected") != -1) {
self.authorization_header_supported = false;
queryParams.access_token = opts.headers.Authorization.substr(7);
delete opts.headers.Authorization;
const secondPromise = self.request(
callback, method, path, queryParams, data, opts,
);
requestPromise.abort = secondPromise.abort;
return secondPromise;
}
if (err.errcode == 'M_UNKNOWN_TOKEN') {
self.event_emitter.emit("Session.logged_out");
}
Expand Down

0 comments on commit 2aececb

Please sign in to comment.