Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize Password Updates with Keycloak #262

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading