Do functions support authorizing access to a Function by using client certificates, in a consumption plan? Something similar to the approach described here? Basically, I'm looking for the Functions runtime to immediately reject connection requests if the caller does not present a valid client certificate, without me having to implement that authorization routine in the code.
Here's the code I came up with, note: this is for Azure Functions v1, when req is an HttpRequestMessage
Caller:
X509Certificate2 clientCert = req.GetClientCertificate();
if (!IsValidClientCertificate(clientCert))
{
return req.CreateErrorResponse(HttpStatusCode.Unauthorized, "A valid client certificate is not found");
}
For Azure Functions v2, you can get the client certificate from the HttpRequest
using req.HttpContext.Connection.ClientCertificate
Basic validation function:
static bool IsValidClientCertificate(X509Certificate2 clientCert)
{
// check the cert's thumbprint against expected thumbprint
if (clientCert.Thumbprint != "<expected thumprint>"
{
return false;
}
// check that we're within the cert's validity period
if (DateTime.Now > clientCert.NotAfter || DateTime.Now < clientCert.NotBefore)
{
return false;
}
// optionally check cert chaining validity
// if(!clientCert.Verify()) { return false; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With