-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathauthenticator_test.go
105 lines (87 loc) · 2.31 KB
/
authenticator_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
package diecast
import (
"encoding/base64"
"fmt"
"net/http/httptest"
"net/url"
"testing"
"github.com/ghetzel/testify/require"
htpasswd "github.com/tg123/go-htpasswd"
)
func musturl(in string) *url.URL {
if u, err := url.Parse(in); err == nil {
return u
} else {
panic(fmt.Sprintf("invalid url: %v", err))
}
}
func TestAuthenticatorConfigs(t *testing.T) {
var assert = require.New(t)
var auth0 = AuthenticatorConfig{
Name: `admin`,
Type: `always`,
Paths: []string{
`/admin`,
`/admin/*`,
},
Except: []string{
`/admin/assets/*`,
},
}
var auth1 = AuthenticatorConfig{
Name: `primary`,
Type: `always`,
Except: []string{
`/logout`,
`*/assets/*`,
},
}
var auths = AuthenticatorConfigs{auth0, auth1}
auth, err := auths.Authenticator(httptest.NewRequest(`GET`, `/`, nil))
assert.NoError(err)
assert.Equal(`primary`, auth.Name())
auth, err = auths.Authenticator(httptest.NewRequest(`GET`, `/admin`, nil))
assert.NoError(err)
assert.Equal(`admin`, auth.Name())
auth, err = auths.Authenticator(httptest.NewRequest(`GET`, `/admin/assets/its/cool/yay.css`, nil))
assert.NoError(err)
assert.Nil(auth)
}
func TestBasicAuthenticator(t *testing.T) {
var assert = require.New(t)
auth, err := NewBasicAuthenticator(&AuthenticatorConfig{
Options: map[string]interface{}{
`credentials`: map[string]interface{}{
`tester01`: `{SHA}u3/Rg4+2cdohm4CmQtP9Qq45HX0=`,
},
},
})
htp, err := htpasswd.AcceptSha(`{SHA}u3/Rg4+2cdohm4CmQtP9Qq45HX0=`)
assert.NoError(err)
assert.NotNil(htp)
assert.True(htp.MatchesPassword(`t3st`))
var req = httptest.NewRequest(`GET`, `/`, nil)
req.Header.Set(`Authorization`, `Basic `+base64.StdEncoding.EncodeToString(
[]byte(url.UserPassword(`tester01`, `t3st`).String()),
))
assert.True(auth.Authenticate(
httptest.NewRecorder(),
req,
))
req = httptest.NewRequest(`GET`, `/`, nil)
req.Header.Set(`Authorization`, `Basic `+base64.StdEncoding.EncodeToString(
[]byte(url.UserPassword(`tester01`, `WRONGPASSWORD`).String()),
))
assert.False(auth.Authenticate(
httptest.NewRecorder(),
req,
))
req = httptest.NewRequest(`GET`, `/`, nil)
req.Header.Set(`Authorization`, `Basic `+base64.StdEncoding.EncodeToString(
[]byte(url.UserPassword(`wrongUser`, `t3st`).String()),
))
assert.False(auth.Authenticate(
httptest.NewRecorder(),
req,
))
}