-
-
Notifications
You must be signed in to change notification settings - Fork 606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use access-token in header #478
Changes from 9 commits
e0a5ede
9b24e66
539abff
d36b872
5da6423
59160a5
c6d2d4c
07868f7
dd0ff3e
6e7f5fe
dc66bbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -385,24 +385,89 @@ module.exports.MatrixHttpApi.prototype = { | |
if (!queryParams) { | ||
queryParams = {}; | ||
} | ||
if (!queryParams.access_token) { | ||
queryParams.access_token = this.opts.accessToken; | ||
if (this.authorizationHeaderSupported === undefined || | ||
this.authorizationHeaderSupported) { | ||
if (isFinite(opts)) { | ||
// opts used to be localTimeoutMs | ||
opts = { | ||
localTimeoutMs: opts, | ||
}; | ||
} | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
if (!opts.headers) { | ||
opts.headers = {}; | ||
} | ||
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; | ||
} | ||
} | ||
if (queryParams.access_token) { | ||
delete queryParams.access_token; | ||
} | ||
} else { | ||
if (!queryParams.access_token) { | ||
queryParams.access_token = this.opts.accessToken; | ||
} | ||
} | ||
|
||
const request_promise = this.request( | ||
const requestPromise = this.request( | ||
callback, method, path, queryParams, data, opts, | ||
); | ||
|
||
const self = this; | ||
request_promise.catch(function(err) { | ||
|
||
if (this.authorizationHeaderSupported === undefined) { | ||
const defer = q.defer(); | ||
const returnPromise = defer.promise; | ||
returnPromise.abort = requestPromise.abort; | ||
|
||
requestPromise.then((resp) => { | ||
self.authorizationHeaderSupported = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like that we now always try to set an Authorization header and then re-send the request with a query parameter if it fails and then fail the request if that fails. I don't like it because:
I would much prefer this to be a option in the constructor to this class, which will always set the header or always send query parameters, negating the need for complex fallback logic and removing the risk of people logging URLs expecting them to not contain authentication information. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default to query params, naturally. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First: Although Header-Support is implemented for a long time it was not allowed in terms of CORS. Is got implemented recently and there was no release since then. Beside that: I understand the "false sense of security"-point. And I have no problem removing the promise-chain-stuff. But especially in terms of CORS: Where should such check be implemented? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave it up to the client to decide if they want to operate in header mode or query param mode. We'll likely keep Riot on query param mode until enough time has passed to let your Synapse PR trickle through, then flip it over to header mode. |
||
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) { | ||
if (err.errcode == 'M_UNKNOWN_TOKEN') { | ||
self.event_emitter.emit("Session.logged_out"); | ||
} | ||
}); | ||
|
||
// 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 | ||
return request_promise; | ||
return requestPromise; | ||
}, | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Source? I'm surprised this PR is adding in backwards compat support in addition to the Authorization header thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I am modifying "opts" I have to check if the parameter is an int or not. I do not know which method actually issued it, but I got errors because it was a number but I wanted to threat is as an object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As reference: It got copied from here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? The docs seem pretty clear that it shouldn't be an int. Either:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like krombel@9e89e71#diff-d2cbeec2e0aad0b77c59efdedd123c51L286 implemented the API change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like https://github.com/matrix-org/matrix-js-sdk/blob/v0.7.13/src/sync.js#L186 and https://github.com/matrix-org/matrix-js-sdk/blob/v0.7.13/src/sync.js#L578 still think the last parameter is an int, meaning that yes, we need the backwards compatible fix in here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what else is still relying on this old behaviour, so please can you update the docs from:
to: