Add Certificate helper function

This commit is contained in:
Adnan Maolood
2020-10-21 17:47:34 -04:00
parent 8c4c00b31a
commit 1634c2c11c
2 changed files with 84 additions and 73 deletions

View File

@@ -311,15 +311,14 @@ func CertificateNotAuthorized(w *ResponseWriter, r *Request) {
w.WriteHeader(StatusCertificateNotAuthorized, "Certificate not authorized")
}
// WithCertificate either responds with CertificateRequired if the client did
// not provide a certificate, or calls f with the first ceritificate provided.
func WithCertificate(w *ResponseWriter, r *Request, f func(*x509.Certificate)) {
// Certificate returns the request certificate. If one is not provided,
// it returns nil and responds with StatusCertificateRequired.
func Certificate(w *ResponseWriter, r *Request) (*x509.Certificate, bool) {
if len(r.TLS.PeerCertificates) == 0 {
CertificateRequired(w, r)
return
return nil, false
}
cert := r.TLS.PeerCertificates[0]
f(cert)
return r.TLS.PeerCertificates[0], true
}
// ResponderFunc is a wrapper around a bare function that implements Handler.