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 17, 2023
1 parent 1a587a3 commit ef0fe53
Show file tree
Hide file tree
Showing 2 changed files with 26 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
12 changes: 12 additions & 0 deletions src/EventStore.Plugins/Authentication/ByteExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Linq;

namespace EventStore.Plugins.Authentication;

internal static class ByteExtensions {
internal static string PEM(this byte[] bytes, string label)
{
var base64String = string.Join('\n', Convert.ToBase64String(bytes).Chunk(64).Select(s => new string(s)));
return $"-----BEGIN {label}-----\n" + base64String + "\n" + $"-----END {label}-----";
}
}

0 comments on commit ef0fe53

Please sign in to comment.