Skip to content

Commit 0079da3

Browse files
authored
Merge pull request #412 from dotnet/bugfix/deleg-client
Allow caller to pass client to authenticator
2 parents decd2d4 + be09a68 commit 0079da3

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

Kerberos.NET/Client/KerberosClient.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,30 @@ public string UserPrincipalName
259259
}
260260
}
261261

262+
/// <summary>
263+
/// Create a new Kerberos client based on the configuration of an existing client or create a new one from scratch.
264+
/// </summary>
265+
/// <param name="delegationClient">The client to copy from</param>
266+
/// <param name="config">The config to pass in if the client is null</param>
267+
/// <param name="logger">The logger to use for the new client</param>
268+
/// <returns></returns>
269+
internal static KerberosClient CopyOrCreate(KerberosClient delegationClient, Krb5Config config, ILoggerFactory logger)
270+
{
271+
if (delegationClient == null)
272+
{
273+
return new KerberosClient(config, logger) { CacheInMemory = true };
274+
}
275+
276+
return new KerberosClient(
277+
delegationClient.Configuration ?? config,
278+
delegationClient.loggerFactory ?? logger,
279+
delegationClient.Transports.ToArray()
280+
)
281+
{
282+
CacheInMemory = true
283+
};
284+
}
285+
262286
/// <summary>
263287
/// Reset any connection state that may be cached from previous attempts.
264288
/// </summary>

Kerberos.NET/KerberosAuthenticator.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Linq;
1010
using System.Security.Claims;
1111
using System.Threading.Tasks;
12+
using Kerberos.NET.Client;
1213
using Kerberos.NET.Configuration;
1314
using Kerberos.NET.Crypto;
1415
using Kerberos.NET.Entities;
@@ -27,17 +28,21 @@ public class KerberosAuthenticator
2728

2829
public UserNameFormat UserNameFormat { get; set; } = UserNameFormat.UserPrincipalName;
2930

30-
public KerberosAuthenticator(string upn, KeyTable keytab, Krb5Config config, ILoggerFactory logger = null)
31+
public KerberosAuthenticator(string upn, KeyTable keytab, KerberosClient delegationClient, ILoggerFactory logger = null)
3132
: this(new KerberosValidator(keytab, logger))
3233
{
3334
if (!string.IsNullOrWhiteSpace(upn))
3435
{
35-
this.s4uProvider = new S4UProviderFactory(upn, keytab, config, logger);
36+
this.s4uProvider = new S4UProviderFactory(upn, keytab, delegationClient, delegationClient?.Configuration, logger);
3637
}
3738
}
3839

40+
public KerberosAuthenticator(string upn, KeyTable keytab, Krb5Config config = null, ILoggerFactory logger = null)
41+
: this(upn, keytab, new KerberosClient(config, logger) { CacheInMemory = true }, logger)
42+
{ }
43+
3944
public KerberosAuthenticator(KeyTable keytab, ILoggerFactory logger = null)
40-
: this(null, keytab, null, logger)
45+
: this(null, keytab, (Krb5Config)null, logger)
4146
{
4247

4348
}

Kerberos.NET/S4UProviderFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ internal class S4UProviderFactory : IS4UProviderFactory
1616
private readonly KerberosClient client;
1717
private readonly KerberosCredential credential;
1818

19-
public S4UProviderFactory(string upn, KeyTable keytab, Krb5Config config = null, ILoggerFactory logger = null)
19+
public S4UProviderFactory(string upn, KeyTable keytab, KerberosClient delegationClient, Krb5Config config = null, ILoggerFactory logger = null)
2020
{
21-
this.client = new KerberosClient(config, logger) { CacheInMemory = true };
21+
this.client = KerberosClient.CopyOrCreate(delegationClient, config, logger);
2222
this.credential = new KeytabCredential(upn, keytab);
2323
}
2424

0 commit comments

Comments
 (0)