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

Issue 61 - Allow Keycloak administration across realms #62

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions src/Keycloak.Net/KeycloakClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,43 @@ public partial class KeycloakClient
private readonly string _password;
private readonly string _clientSecret;
private readonly Func<string> _getToken;
private readonly string _authRealm;

private KeycloakClient(string url)
{
_url = url;
}

public KeycloakClient(string url, string userName, string password)
public KeycloakClient(string url, string userName, string password, string authRealm = null)
: this(url)
{
_userName = userName;
_password = password;
_authRealm = authRealm;
}

public KeycloakClient(string url, string clientSecret)
public KeycloakClient(string url, string clientSecret, string authRealm = null)
: this(url)
{
_clientSecret = clientSecret;
_authRealm = authRealm;
}

public KeycloakClient(string url, Func<string> getToken)
public KeycloakClient(string url, Func<string> getToken, string authRealm = null)
: this(url)
{
_getToken = getToken;
_authRealm = authRealm;
}

public void SetSerializer(ISerializer serializer)
{
_serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
}

private IFlurlRequest GetBaseUrl(string authenticationRealm) => new Url(_url)
private IFlurlRequest GetBaseUrl(string targetRealm) => new Url(_url)
.AppendPathSegment("/auth")
.ConfigureRequest(settings => settings.JsonSerializer = _serializer)
.WithAuthentication(_getToken, _url, authenticationRealm, _userName, _password, _clientSecret);
.WithAuthentication(_getToken, _url, _authRealm ?? targetRealm, _userName, _password, _clientSecret);
}
}