From 4405e933ddcf0c6a2570af15de664b1fe8e6f78d Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 28 Apr 2020 16:23:01 +0200 Subject: [PATCH] Add change password feature --- lib/src/client.dart | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/lib/src/client.dart b/lib/src/client.dart index e4ecdcd..eae0e16 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1931,4 +1931,49 @@ class Client { ); return; } + + /// Changes the password. You should either set oldPasswort or another authentication flow. + Future changePassword(String newPassword, + {String oldPassword, Map auth}) async { + try { + await jsonRequest( + type: HTTPType.POST, + action: '/client/r0/account/password', + data: { + 'new_password': newPassword, + if (oldPassword != null) + 'auth': { + 'type': 'm.login.password', + 'user': userID, + 'password': oldPassword, + }, + if (auth != null) 'auth': auth, + }, + ); + } on MatrixException catch (matrixException) { + if (!matrixException.requireAdditionalAuthentication) { + rethrow; + } + if (matrixException.authenticationFlows.length != 1 || + !matrixException.authenticationFlows.first.stages + .contains('m.login.password')) { + rethrow; + } + if (oldPassword == null) { + rethrow; + } + return changePassword( + newPassword, + auth: { + 'type': 'm.login.password', + 'user': userID, + 'identifier': {'type': 'm.id.user', 'user': userID}, + 'password': oldPassword, + 'session': matrixException.session, + }, + ); + } catch (_) { + rethrow; + } + } }