Skip to content

Commit 545a743

Browse files
RiotRobott3chguy
andauthored
[Backport staging] Fix authedRequest including Authorization: Bearer undefined for password resets (matrix-org#2829)
Co-authored-by: Michael Telatynski <[email protected]>
1 parent db33f39 commit 545a743

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

spec/unit/http-api/fetch.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,14 @@ describe("FetchHttpApi", () => {
220220
expect(api.authedRequest(Method.Get, "/path")).rejects.toThrow("Ye shall ask for consent"),
221221
]);
222222
});
223+
224+
describe("authedRequest", () => {
225+
it("should not include token if unset", () => {
226+
const fetchFn = jest.fn();
227+
const emitter = new TypedEventEmitter<HttpApiEvent, HttpApiEventHandlerMap>();
228+
const api = new FetchHttpApi(emitter, { baseUrl, prefix, fetchFn });
229+
api.authedRequest(Method.Post, "/account/password");
230+
expect(fetchFn.mock.calls[0][1].headers.Authorization).toBeUndefined();
231+
});
232+
});
223233
});

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7875,7 +7875,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
78757875
* @return {module:http-api.MatrixError} Rejects: with an error response.
78767876
*/
78777877
public setPassword(
7878-
authDict: any,
7878+
authDict: IAuthDict,
78797879
newPassword: string,
78807880
logoutDevices?: boolean,
78817881
): Promise<{}> {

src/http-api/fetch.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,20 @@ export class FetchHttpApi<O extends IHttpOpts> {
143143
): Promise<ResponseType<T, O>> {
144144
if (!queryParams) queryParams = {};
145145

146-
if (this.opts.useAuthorizationHeader) {
147-
if (!opts.headers) {
148-
opts.headers = {};
146+
if (this.opts.accessToken) {
147+
if (this.opts.useAuthorizationHeader) {
148+
if (!opts.headers) {
149+
opts.headers = {};
150+
}
151+
if (!opts.headers.Authorization) {
152+
opts.headers.Authorization = "Bearer " + this.opts.accessToken;
153+
}
154+
if (queryParams.access_token) {
155+
delete queryParams.access_token;
156+
}
157+
} else if (!queryParams.access_token) {
158+
queryParams.access_token = this.opts.accessToken;
149159
}
150-
if (!opts.headers.Authorization) {
151-
opts.headers.Authorization = "Bearer " + this.opts.accessToken;
152-
}
153-
if (queryParams.access_token) {
154-
delete queryParams.access_token;
155-
}
156-
} else if (!queryParams.access_token) {
157-
queryParams.access_token = this.opts.accessToken;
158160
}
159161

160162
const requestPromise = this.request<T>(method, path, queryParams, body, opts);

0 commit comments

Comments
 (0)