Skip to content

Commit 5937e6a

Browse files
authored
Support for MSC2457 logout_devices param for setPassword() (#2285)
1 parent c6c22e3 commit 5937e6a

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

spec/unit/matrix-client.spec.ts

+53
Original file line numberDiff line numberDiff line change
@@ -1056,4 +1056,57 @@ describe("MatrixClient", function() {
10561056
});
10571057
});
10581058
});
1059+
1060+
describe("setPassword", () => {
1061+
const auth = { session: 'abcdef', type: 'foo' };
1062+
const newPassword = 'newpassword';
1063+
const callback = () => {};
1064+
1065+
const passwordTest = (expectedRequestContent: any, expectedCallback?: Function) => {
1066+
const [callback, method, path, queryParams, requestContent] = client.http.authedRequest.mock.calls[0];
1067+
if (expectedCallback) {
1068+
expect(callback).toBe(expectedCallback);
1069+
} else {
1070+
expect(callback).toBeFalsy();
1071+
}
1072+
expect(method).toBe('POST');
1073+
expect(path).toEqual('/account/password');
1074+
expect(queryParams).toBeFalsy();
1075+
expect(requestContent).toEqual(expectedRequestContent);
1076+
};
1077+
1078+
beforeEach(() => {
1079+
client.http.authedRequest.mockClear().mockResolvedValue({});
1080+
});
1081+
1082+
it("no logout_devices specified", async () => {
1083+
await client.setPassword(auth, newPassword);
1084+
passwordTest({ auth, new_password: newPassword });
1085+
});
1086+
1087+
it("no logout_devices specified + callback", async () => {
1088+
await client.setPassword(auth, newPassword, callback);
1089+
passwordTest({ auth, new_password: newPassword }, callback);
1090+
});
1091+
1092+
it("overload logoutDevices=true", async () => {
1093+
await client.setPassword(auth, newPassword, true);
1094+
passwordTest({ auth, new_password: newPassword, logout_devices: true });
1095+
});
1096+
1097+
it("overload logoutDevices=true + callback", async () => {
1098+
await client.setPassword(auth, newPassword, true, callback);
1099+
passwordTest({ auth, new_password: newPassword, logout_devices: true }, callback);
1100+
});
1101+
1102+
it("overload logoutDevices=false", async () => {
1103+
await client.setPassword(auth, newPassword, false);
1104+
passwordTest({ auth, new_password: newPassword, logout_devices: false });
1105+
});
1106+
1107+
it("overload logoutDevices=false + callback", async () => {
1108+
await client.setPassword(auth, newPassword, false, callback);
1109+
passwordTest({ auth, new_password: newPassword, logout_devices: false }, callback);
1110+
});
1111+
});
10591112
});

src/client.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -7819,18 +7819,45 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
78197819
* Make a request to change your password.
78207820
* @param {Object} authDict
78217821
* @param {string} newPassword The new desired password.
7822+
* @param {boolean} logoutDevices Should all sessions be logged out after the password change. Defaults to true.
78227823
* @param {module:client.callback} callback Optional.
78237824
* @return {Promise} Resolves: TODO
78247825
* @return {module:http-api.MatrixError} Rejects: with an error response.
78257826
*/
7826-
public setPassword(authDict: any, newPassword: string, callback?: Callback): Promise<any> { // TODO: Types
7827+
public setPassword(
7828+
authDict: any,
7829+
newPassword: string,
7830+
callback?: Callback,
7831+
): Promise<{}>;
7832+
public setPassword(
7833+
authDict: any,
7834+
newPassword: string,
7835+
logoutDevices: boolean,
7836+
callback?: Callback,
7837+
): Promise<{}>;
7838+
public setPassword(
7839+
authDict: any,
7840+
newPassword: string,
7841+
logoutDevices?: Callback | boolean,
7842+
callback?: Callback,
7843+
): Promise<{}> {
7844+
if (typeof logoutDevices === 'function') {
7845+
callback = logoutDevices;
7846+
}
7847+
if (typeof logoutDevices !== 'boolean') {
7848+
// Use backwards compatible behaviour of not specifying logout_devices
7849+
// This way it is left up to the server:
7850+
logoutDevices = undefined;
7851+
}
7852+
78277853
const path = "/account/password";
78287854
const data = {
78297855
'auth': authDict,
78307856
'new_password': newPassword,
7857+
'logout_devices': logoutDevices,
78317858
};
78327859

7833-
return this.http.authedRequest(
7860+
return this.http.authedRequest<{}>(
78347861
callback, Method.Post, path, null, data,
78357862
);
78367863
}

0 commit comments

Comments
 (0)