Skip to content

Commit

Permalink
Add support for client certificate authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
shaan1337 committed Nov 16, 2023
1 parent 1a587a3 commit 8a2b601
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/EventStore.Plugins/Authentication/AuthenticationRequest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;

namespace EventStore.Plugins.Authentication {
public abstract class AuthenticationRequest {
Expand All @@ -19,6 +20,11 @@ public abstract class AuthenticationRequest {
/// </summary>
public readonly string SuppliedPassword;

/// <summary>
/// Whether or not a client certificate was supplied with the request
/// </summary>
public readonly bool HasSuppliedClientCertificate;

/// <summary>
/// All supplied authentication tokens for the request
/// </summary>
Expand All @@ -31,6 +37,7 @@ protected AuthenticationRequest(string id, IReadOnlyDictionary<string, string> t
Tokens = tokens;
Name = GetToken("uid");
SuppliedPassword = GetToken("pwd");
HasSuppliedClientCertificate = GetToken("clientCert") != null;
}

protected AuthenticationRequest(string id, string name, string suppliedPassword)
Expand All @@ -40,6 +47,13 @@ protected AuthenticationRequest(string id, string name, string suppliedPassword)
}) {
}

protected AuthenticationRequest(string id, string name, X509Certificate2 clientCertificate)
: this(id, new Dictionary<string, string> {
["uid"] = name,
["clientCert"] = clientCertificate.Export(X509ContentType.Cert).PEM("CERTIFICATE")
}) {
}

/// <summary>
/// Gets the token corresponding to <param name="key" />.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/EventStore.Plugins/Authentication/ByteExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace EventStore.Plugins.Authentication;

internal static class ByteExtensions {
public static string PEM(this byte[] bytes, string label) {
return $"-----BEGIN {label}-----\n" + Convert.ToBase64String(bytes) + "\n" + $"-----END {label}-----\n";
}
}

0 comments on commit 8a2b601

Please sign in to comment.