From dc2e7b98806ac8c03c1ce8c6b75f287e25c05935 Mon Sep 17 00:00:00 2001 From: Ali Hassan <97635301+alihdev@users.noreply.github.com> Date: Thu, 18 Jul 2024 13:33:41 +0300 Subject: [PATCH 1/2] Fix update passowrd in UpdateAsync IdentityUserAppService (sync with keycloak) fix issue with updating the password (it will be updated only in the identity user) --- .../Users/KeycloakUserUpdatingJob.cs | 6 +++++ .../Identity/EShopIdentityUserAppService.cs | 5 ++-- .../Keycloak/Service/IKeycloakService.cs | 2 ++ .../Keycloak/Service/KeycloakService.cs | 25 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Users/KeycloakUserUpdatingJob.cs b/services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Users/KeycloakUserUpdatingJob.cs index f3bc74a4..a00a3ca7 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Users/KeycloakUserUpdatingJob.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Users/KeycloakUserUpdatingJob.cs @@ -92,6 +92,11 @@ await _keycloakService.AddRealmRolesToUserAsync(keycloakUser.Id, _logger.LogInformation($"Keycloak user with the username:{args.UserName} has been updated."); } } + + if (!args.Password.IsNullOrEmpty() && keycloakUser != null) + { + await _keycloakService.SetNewPassword(keycloakUser.UserName, args.Password); + } } catch (Exception e) { @@ -116,6 +121,7 @@ public class IdentityUserUpdatingArgs public bool OldIsActive { get; init; } public string[] RoleNames { get; init; } public string[] OldRoleNames { get; init; } + public string Password { get; init; } public IEnumerable GetDifferentFields() { diff --git a/services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityUserAppService.cs b/services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityUserAppService.cs index 7c8c3833..6f7b9523 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityUserAppService.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityUserAppService.cs @@ -73,7 +73,7 @@ private async Task CreateIdentityUserUpdatingArgsAsync { var userRoles = existingUser.Roles.Select(q => q.RoleId).ToList(); var roles = await _roleRepository.GetListAsync(); - + var args = new IdentityUserUpdatingArgs { Email = input.Email, @@ -87,7 +87,8 @@ private async Task CreateIdentityUserUpdatingArgsAsync IsActive = input.IsActive, OldIsActive = existingUser.IsActive, RoleNames = input.RoleNames, - OldRoleNames = roles.Where(q => userRoles.Contains(q.Id)).Select(q => q.Name).ToArray() + OldRoleNames = roles.Where(q => userRoles.Contains(q.Id)).Select(q => q.Name).ToArray(), + Password = input.Password }; return args; diff --git a/services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/IKeycloakService.cs b/services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/IKeycloakService.cs index aa09d8d3..a67cf8c2 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/IKeycloakService.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/IKeycloakService.cs @@ -33,4 +33,6 @@ public interface IKeycloakService : ITransientDependency Task DeleteRoleByIdAsync(string id, CancellationToken cancellationToken = default); Task UpdateRoleAsync(string id, Role role, CancellationToken cancellationToken = default); + + Task SetNewPassword(string username, string newPassword, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/KeycloakService.cs b/services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/KeycloakService.cs index 73f2b41b..d97e6f3c 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/KeycloakService.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/KeycloakService.cs @@ -158,4 +158,29 @@ public async Task UpdateRoleAsync(string id, Role role, CancellationToken return result; } + + public async Task SetNewPassword(string username, string newPassword, CancellationToken cancellationToken = default) + { + var users = await _keycloakClient.GetUsersAsync(_keycloakOptions.RealmName, username: username, cancellationToken: cancellationToken); + + if (!users.Any()) return false; + + var user = users.First(); + + var newCredentials = new Credentials + { + Type = "password", + Value = newPassword, + Temporary = false + }; + + var isSuccessReset = await _keycloakClient.ResetUserPasswordAsync( + realm: _keycloakOptions.RealmName, + userId: user.Id, + credentials: newCredentials, + cancellationToken: cancellationToken + ); + + return isSuccessReset; + } } \ No newline at end of file From 71842636916bba3cfe04d23e9b53216d57a75043 Mon Sep 17 00:00:00 2001 From: Engincan VESKE Date: Fri, 26 Jul 2024 22:54:47 +0300 Subject: [PATCH 2/2] Update EShopOnAbp.IdentityService.HttpApi.Host.csproj --- .../EShopOnAbp.IdentityService.HttpApi.Host.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/EShopOnAbp.IdentityService.HttpApi.Host.csproj b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/EShopOnAbp.IdentityService.HttpApi.Host.csproj index 8493eb57..396a88db 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/EShopOnAbp.IdentityService.HttpApi.Host.csproj +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/EShopOnAbp.IdentityService.HttpApi.Host.csproj @@ -7,7 +7,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive