Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Merge pull request #262 from alihdev/keycloak-user-update-passowrd
Browse files Browse the repository at this point in the history
Synchronize Password Updates with Keycloak
  • Loading branch information
EngincanV authored Jul 26, 2024
2 parents 327fbcc + 7184263 commit eab6174
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<FieldChange> GetDifferentFields()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private async Task<IdentityUserUpdatingArgs> CreateIdentityUserUpdatingArgsAsync
{
var userRoles = existingUser.Roles.Select(q => q.RoleId).ToList();
var roles = await _roleRepository.GetListAsync();

var args = new IdentityUserUpdatingArgs
{
Email = input.Email,
Expand All @@ -87,7 +87,8 @@ private async Task<IdentityUserUpdatingArgs> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public interface IKeycloakService : ITransientDependency
Task<bool> DeleteRoleByIdAsync(string id, CancellationToken cancellationToken = default);

Task<bool> UpdateRoleAsync(string id, Role role, CancellationToken cancellationToken = default);

Task<bool> SetNewPassword(string username, string newPassword, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,29 @@ public async Task<bool> UpdateRoleAsync(string id, Role role, CancellationToken

return result;
}

public async Task<bool> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Keycloak.Net.Core" Version="1.0.22" />
<PackageReference Include="Keycloak.Net.Core" Version="1.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit eab6174

Please sign in to comment.