-
Notifications
You must be signed in to change notification settings - Fork 5
/
pass_auth_test.go
145 lines (126 loc) · 3.83 KB
/
pass_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gig
import (
"crypto/md5"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"testing"
"github.com/matryer/is"
)
func TestPassAuth(t *testing.T) {
var (
is = is.New(t)
g = New()
invalidErr = errors.New("invalid credentials")
goodCert = x509.Certificate{Raw: []byte{1}}
newCert = x509.Certificate{Raw: []byte{2}}
mw = PassAuth(func(sig string, c Context) (string, error) {
if sig == fmt.Sprintf("%x", md5.Sum(goodCert.Raw)) {
return "", nil
}
return "/login", nil
})
)
g.Handle("/", func(c Context) error {
return c.Gemini("ok")
})
g.PassAuthLoginHandle("/login", func(u, p, sig string, c Context) (string, error) {
if u == "valid-user" && p == "secret" {
return "/private", nil
}
return "", invalidErr
})
g.Handle("/private", func(c Context) error {
return c.Gemini("private")
}, mw)
// Public endpoint
c, res := g.NewFakeContext("/", nil)
g.ServeGemini(c)
is.Equal(res.Written, "20 text/gemini\r\nok")
// No certificate
c, res = g.NewFakeContext("/private", nil)
g.ServeGemini(c)
is.Equal(res.Written, "60 Please create a certificate\r\n")
// New certificate
c, res = g.NewFakeContext("/private", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "30 /login\r\n")
// Try login with new certificate
c, res = g.NewFakeContext("/login", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "10 Enter username\r\n")
c, res = g.NewFakeContext("/login?valid-user", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "30 /login/valid-user\r\n")
c, res = g.NewFakeContext("/login/valid-user", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "11 Enter password\r\n")
c, res = g.NewFakeContext("/login/valid-user?bad-pass", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "50 invalid credentials\r\n")
c, res = g.NewFakeContext("/login/valid-user?secret", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "30 /private\r\n")
// Logged in certificate
c, res = g.NewFakeContext("/private", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&goodCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "20 text/gemini\r\nprivate")
}
func TestPassAuth_Errors(t *testing.T) {
var (
is = is.New(t)
g = New()
newCert = x509.Certificate{Raw: []byte{2}}
mw = PassAuth(func(sig string, c Context) (string, error) {
return "/login", errors.New("oops")
})
)
g.Handle("/", func(c Context) error {
return c.Gemini("ok")
})
g.PassAuthLoginHandle("/login", func(u, p, sig string, c Context) (string, error) {
return "", errors.New("oops")
})
g.Handle("/private", func(c Context) error {
return c.Gemini("private")
}, mw)
// CertCheck fails
c, res := g.NewFakeContext("/private", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "59 Try again later\r\n")
// Bad username
c, res = g.NewFakeContext("/login?%%", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "59 Invalid username received\r\n")
// Bad password
c, res = g.NewFakeContext("/login/valid-user?%%", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "59 Invalid password received\r\n")
// Login fails
c, res = g.NewFakeContext("/login/valid-user?secret", &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{&newCert},
})
g.ServeGemini(c)
is.Equal(res.Written, "50 oops\r\n")
}