-
Notifications
You must be signed in to change notification settings - Fork 5
/
cert_auth_test.go
98 lines (86 loc) · 2.13 KB
/
cert_auth_test.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gig
import (
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"testing"
"github.com/matryer/is"
)
func TestCertAuth(t *testing.T) {
is := is.New(t)
g := New()
testCases := []struct {
mw MiddlewareFunc
expectedErrNoCert error
expectedErrBadCert error
name string
}{
{
mw: CertAuth(ValidateHasCertificate),
expectedErrNoCert: ErrClientCertificateRequired,
expectedErrBadCert: nil,
name: `ValidateHasCertificate`,
},
{
mw: CertAuth(func(cert *x509.Certificate, c Context) *GeminiError {
if cert == nil {
return ErrClientCertificateRequired
}
if cert.Subject.CommonName != "tester" {
return ErrCertificateNotValid
}
c.Set("subject", cert.Subject.CommonName)
return nil
}),
expectedErrNoCert: ErrClientCertificateRequired,
expectedErrBadCert: ErrCertificateNotValid,
name: `CustomValidator`,
},
{
mw: CertAuthWithConfig(CertAuthConfig{
Skipper: nil,
Validator: nil,
}),
expectedErrNoCert: ErrClientCertificateRequired,
expectedErrBadCert: nil,
name: `NilConfig`,
},
{
mw: CertAuthWithConfig(CertAuthConfig{
Skipper: func(c Context) bool {
c.Set("subject", "tester")
return true
},
}),
expectedErrNoCert: nil,
expectedErrBadCert: nil,
name: `CustomSkipper`,
},
}
for _, test := range testCases {
test := test
t.Run(test.name, func(t *testing.T) {
h := test.mw(func(c Context) error {
return c.Gemini("test")
})
// No certificate
c, _ := g.NewFakeContext("/", nil)
is.Equal(h(c), test.expectedErrNoCert)
// Invalid certificate
c, _ = g.NewFakeContext("/", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{
{Subject: pkix.Name{CommonName: "not-tester"}},
},
})
is.Equal(h(c), test.expectedErrBadCert)
// Valid certificate
c, _ = g.NewFakeContext("/", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{
{Subject: pkix.Name{CommonName: "tester"}},
},
})
is.NoErr(h(c))
is.Equal("tester", c.Get("subject"))
})
}
}