-
-
Notifications
You must be signed in to change notification settings - Fork 602
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 10 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 |
---|---|---|
|
@@ -74,12 +74,18 @@ module.exports.PREFIX_MEDIA_R0 = "/_matrix/media/r0"; | |
* requests. | ||
* @param {Number=} opts.localTimeoutMs The default maximum amount of time to wait | ||
* 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) { | ||
utils.checkObjectHasKeys(opts, ["baseUrl", "request", "prefix"]); | ||
opts.onlyData = opts.onlyData || false; | ||
this.event_emitter = event_emitter; | ||
this.opts = opts; | ||
this.useAuthorizationHeader = false; | ||
if (opts.useAuthorizationHeader) { | ||
this.useAuthorizationHeader = Boolean(opts.useAuthorizationHeader); | ||
} | ||
this.uploads = []; | ||
}; | ||
|
||
|
@@ -385,24 +391,45 @@ module.exports.MatrixHttpApi.prototype = { | |
if (!queryParams) { | ||
queryParams = {}; | ||
} | ||
if (!queryParams.access_token) { | ||
queryParams.access_token = this.opts.accessToken; | ||
if (this.useAuthorizationHeader) { | ||
if (isFinite(opts)) { | ||
// opts used to be localTimeoutMs | ||
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. 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 commentThe 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 commentThe 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 commentThe 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 commentThe 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 commentThe 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 commentThe 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:
|
||
opts = { | ||
localTimeoutMs: opts, | ||
}; | ||
} | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
if (!opts.headers) { | ||
opts.headers = {}; | ||
} | ||
if (!opts.headers.Authorization) { | ||
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) { | ||
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.
JS SDK users do not create a
MatrixHttpApi
, so they will be unable to set this option without gut-wrenchingclient._http
. This is set in the rather poorly defined "base APIs" constructor which is called in the client constructor.Please bubble this down from
MatrixClient
, copying this documentation to theopts
ofMatrixClient
.