From d9cb762a321e4b76e907fac16c625a76bc11b753 Mon Sep 17 00:00:00 2001 From: Joy A Date: Tue, 4 Apr 2023 12:24:58 +0530 Subject: [PATCH] avniproject/avni-server#397 | User change own password on Keycloak --- .../src/framework/http/requests.js | 12 ++++++++++++ .../src/service/KeycloakAuthService.js | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/openchs-android/src/framework/http/requests.js b/packages/openchs-android/src/framework/http/requests.js index b31e01a53..becbb39b8 100644 --- a/packages/openchs-android/src/framework/http/requests.js +++ b/packages/openchs-android/src/framework/http/requests.js @@ -84,6 +84,14 @@ let _post = (endpoint, file, fetchWithoutTimeout, bypassAuth = false) => { return params.then((headers) => fetchFactory(endpoint, "POST", headers, fetchWithoutTimeout)) }; +let _put = (endpoint, body, fetchWithoutTimeout, bypassAuth = false) => { + const params = _addAuthIfRequired(makeRequest("json", {body: JSON.stringify(body)}), bypassAuth); + General.logDebug('Requests', `PUT: ${endpoint}`); + return params.then((headers) => { + return fetchFactory(endpoint, "PUT", headers, fetchWithoutTimeout) + }) +}; + export let post = _post; export let get = (endpoint, bypassAuth = false) => { @@ -94,6 +102,10 @@ export let getJSON = (endpoint, bypassAuth = false) => { return _get(endpoint, bypassAuth); }; +export let putJSON = (endpoint, body, fetchWithoutTimeout = false, bypassAuth = false) => { + return _put(endpoint, body, fetchWithoutTimeout, bypassAuth) +}; + export let postUrlFormEncoded = (endpoint, body) => { const formBody = new URLSearchParams(body).toString(); return fetchFactory(endpoint, "POST", {headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*/*'}, body: formBody}, true) diff --git a/packages/openchs-android/src/service/KeycloakAuthService.js b/packages/openchs-android/src/service/KeycloakAuthService.js index aea0e605e..b1d7ddfa8 100644 --- a/packages/openchs-android/src/service/KeycloakAuthService.js +++ b/packages/openchs-android/src/service/KeycloakAuthService.js @@ -1,5 +1,5 @@ import Service from "../framework/bean/Service"; -import { postUrlFormEncoded } from "../framework/http/requests"; +import { postUrlFormEncoded, putJSON } from "../framework/http/requests"; import General from "../utility/General"; import AuthenticationError, { NO_USER } from "./AuthenticationError"; import BaseAuthProviderService from "./BaseAuthProviderService"; @@ -79,8 +79,17 @@ class KeycloakAuthService extends BaseAuthProviderService { // return new AuthenticationError(NO_USER, "No user or needs login"); } - async changePassword() { - throw new Error("Not implemented for Keycloak"); + async changePassword(oldPassword, newPassword) { + const settings = this.settingsService.getSettings(); + this.authenticate(settings.userId, oldPassword) + .then(async (authResult) => { + if (authResult.status === 'LOGIN_SUCCESS') { + const changePasswordEndpoint = `${settings.serverURL}/user/changePassword`; + await putJSON(changePasswordEndpoint, {newPassword}) + .catch((e) => e); + } else throw new Error('Unable to authenticate user'); + }) + .catch(e => e); } async logout() {