-
Notifications
You must be signed in to change notification settings - Fork 5
/
cert_auth.go
79 lines (64 loc) · 1.78 KB
/
cert_auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package gig
import (
"crypto/x509"
)
type (
// CertAuthConfig defines the config for CertAuth middleware.
CertAuthConfig struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
// Validator is a function to validate client certificate.
// Required.
Validator CertAuthValidator
}
// CertAuthValidator defines a function to validate CertAuth credentials.
CertAuthValidator func(*x509.Certificate, Context) *GeminiError
)
var (
// DefaultCertAuthConfig is the default CertAuth middleware config.
DefaultCertAuthConfig = CertAuthConfig{
Skipper: DefaultSkipper,
Validator: ValidateHasCertificate,
}
)
// ValidateHasCertificate returns ErrClientCertificateRequired if no certificate is sent.
// It also stores subject name in context under "subject".
func ValidateHasCertificate(cert *x509.Certificate, c Context) *GeminiError {
if cert == nil {
return ErrClientCertificateRequired
}
c.Set("subject", cert.Subject.CommonName)
return nil
}
// CertAuth returns an CertAuth middleware.
//
// For valid credentials it calls the next handler.
func CertAuth(fn CertAuthValidator) MiddlewareFunc {
c := DefaultCertAuthConfig
c.Validator = fn
return CertAuthWithConfig(c)
}
// CertAuthWithConfig returns an CertAuth middleware with config.
// See `CertAuth()`.
func CertAuthWithConfig(config CertAuthConfig) MiddlewareFunc {
// Defaults
if config.Validator == nil {
config.Validator = DefaultCertAuthConfig.Validator
}
if config.Skipper == nil {
config.Skipper = DefaultCertAuthConfig.Skipper
}
return func(next HandlerFunc) HandlerFunc {
return func(c Context) error {
if config.Skipper(c) {
return next(c)
}
// Verify credentials
err := config.Validator(c.Certificate(), c)
if err != nil {
return err
}
return next(c)
}
}
}