From 7bfba25b5581eee94f506a119f2c6b8c2db30aba Mon Sep 17 00:00:00 2001 From: JMendes Date: Fri, 20 Aug 2021 01:40:38 +0100 Subject: [PATCH] Issue 61 - Allow Keycloak administration across realms by specifying a different realm for authentication. To avoid constructor ambiguity, use named arguments. (For example: var client = new KeycloakClient("https://some.url", "tHiSiSmYcLiEnTsEcReT", authRealm: "master");) --- src/Keycloak.Net/KeycloakClient.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Keycloak.Net/KeycloakClient.cs b/src/Keycloak.Net/KeycloakClient.cs index 0e00371d..819c44f2 100755 --- a/src/Keycloak.Net/KeycloakClient.cs +++ b/src/Keycloak.Net/KeycloakClient.cs @@ -21,29 +21,33 @@ public partial class KeycloakClient private readonly string _password; private readonly string _clientSecret; private readonly Func _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 getToken) + public KeycloakClient(string url, Func getToken, string authRealm = null) : this(url) { _getToken = getToken; + _authRealm = authRealm; } public void SetSerializer(ISerializer serializer) @@ -51,9 +55,9 @@ 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); } }