/
login_handler.go
289 lines (269 loc) · 7.03 KB
/
login_handler.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package authentication
import (
"bytes"
"crypto/rand"
"crypto/subtle"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"github.com/proidiot/gone/log"
"github.com/stuphlabs/pullcord/config"
"github.com/stuphlabs/pullcord/util"
)
// XSRFTokenLength is the length of XSRF token strings.
const XSRFTokenLength = 64
const msgInvalidCredentials = "Invalid Credentials"
// LoginHandler is a login handling system that presents a login page backed by
// a PasswordChecker for users that are not yet logged in, while seamlessly
// forwarding all requests downstream for users that are logged in. A
// LoginHandler has an identifier (which it uses to differentiate its login
// tokens and authentication flags from other components, possibly including
// other LoginHandlers), a PasswordChecker (which it allows users to
// authenticate against in conjunction with its own XSRF token), and a
// downstream RequestFilter (possibly an entire pipeline).
type LoginHandler struct {
Identifier string
PasswordChecker PasswordChecker
Downstream http.Handler
}
func init() {
config.MustRegisterResourceType(
"loginhandler",
func() json.Unmarshaler {
return new(LoginHandler)
},
)
}
// UnmarshalJSON implements encoding/json.Unmarshaler.
func (h *LoginHandler) UnmarshalJSON(input []byte) error {
var t struct {
Identifier string
PasswordChecker config.Resource
Downstream config.Resource
}
dec := json.NewDecoder(bytes.NewReader(input))
if e := dec.Decode(&t); e != nil {
_ = log.Err("Unable to decode LoginHandler")
return e
}
p := t.PasswordChecker.Unmarshalled
switch p := p.(type) {
case PasswordChecker:
h.PasswordChecker = p
default:
_ = log.Err(
fmt.Sprintf(
"Registry value is not a PasswordChecker: %#v",
t.PasswordChecker,
),
)
return config.UnexpectedResourceType
}
if d, ok := t.Downstream.Unmarshalled.(http.Handler); ok {
h.Downstream = d
} else {
_ = log.Err(
fmt.Sprintf(
"Registry value is not a RequestFilter: %#v",
t.Downstream,
),
)
return config.UnexpectedResourceType
}
h.Identifier = t.Identifier
return nil
}
func (h *LoginHandler) ServeHTTP(
w http.ResponseWriter,
request *http.Request,
) {
errString := ""
rawsesh := request.Context().Value(ctxKeySession)
if rawsesh == nil {
_ = log.Crit(
"login handler was unable to retrieve session from" +
" context",
)
util.InternalServerError.ServeHTTP(w, request)
return
}
sesh := rawsesh.(Session)
authSeshKey := "authenticated-" + h.Identifier
xsrfKey := "xsrf-" + h.Identifier
usernameKey := "username-" + h.Identifier
passwordKey := "password-" + h.Identifier
if authd, err := sesh.GetValue(
authSeshKey,
); err == nil && authd == true {
_ = log.Debug("login handler passing request along")
h.Downstream.ServeHTTP(w, request)
return
} else if err != NoSuchSessionValueError {
_ = log.Err(
fmt.Sprintf(
"login handler error during auth status"+
" retrieval: %v",
err,
),
)
util.InternalServerError.ServeHTTP(w, request)
return
}
if xsrfStored, err := sesh.GetValue(
xsrfKey,
); err != nil && err != NoSuchSessionValueError {
_ = log.Err(
fmt.Sprintf(
"login handler error during xsrf token"+
" retrieval: %v",
err,
),
)
util.InternalServerError.ServeHTTP(w, request)
return
} else if err == NoSuchSessionValueError {
_ = log.Info("login handler received new request")
} else if err = request.ParseForm(); err != nil {
err = log.Warning(
fmt.Sprintf(
"login handler error during ParseForm: %#v",
err,
),
)
if err != nil {
// this is too suspicious
util.Forbidden.ServeHTTP(w, request)
return
}
errString = "Bad request"
} else if xsrfRcvd, present :=
request.PostForm[xsrfKey]; !present {
_ = log.Info("login handler did not receive xsrf token")
errString = msgInvalidCredentials
} else if len(xsrfRcvd) != 1 || 1 != subtle.ConstantTimeCompare(
[]byte(xsrfStored.(string)),
[]byte(xsrfRcvd[0]),
) {
_ = log.Info("login handler received bad xsrf token")
errString = msgInvalidCredentials
} else if uVals, present :=
request.PostForm[usernameKey]; !present {
_ = log.Info("login handler did not receive username")
errString = msgInvalidCredentials
} else if pVals, present :=
request.PostForm[passwordKey]; !present {
_ = log.Info("login handler did not receive password")
errString = msgInvalidCredentials
} else if len(uVals) != 1 || len(pVals) != 1 {
_ = log.Info(
"login handler received multi values for username or" +
" password",
)
errString = "Bad request"
} else if err = h.PasswordChecker.CheckPassword(
uVals[0],
pVals[0],
); err == NoSuchIdentifierError {
_ = log.Info("login handler received bad username")
errString = msgInvalidCredentials
} else if err == BadPasswordError {
_ = log.Info("login handler received bad password")
errString = msgInvalidCredentials
} else if err != nil {
_ = log.Err(
fmt.Sprintf(
"login handler error during CheckPassword: %#v",
err,
),
)
util.InternalServerError.ServeHTTP(w, request)
return
} else if err = sesh.SetValue(authSeshKey, true); err != nil {
_ = log.Err(
fmt.Sprintf(
"login handler error during auth set: %#v",
err,
),
)
util.InternalServerError.ServeHTTP(w, request)
return
} else {
err = log.Notice(
fmt.Sprintf(
"login successful for: %s",
uVals[0],
),
)
if err != nil {
util.InternalServerError.ServeHTTP(w, request)
return
}
h.Downstream.ServeHTTP(w, request)
return
}
rawXSRFToken := make([]byte, XSRFTokenLength)
if rsize, err := rand.Read(
rawXSRFToken[:],
); err != nil || rsize != XSRFTokenLength {
_ = log.Err(
fmt.Sprintf(
"login handler error during xsrf generation:"+
" len expected: %d, actual: %d,"+
" err: %#v",
XSRFTokenLength,
rsize,
err,
),
)
util.InternalServerError.ServeHTTP(w, request)
return
}
nextXSRFToken := hex.EncodeToString(rawXSRFToken)
if err := sesh.SetValue(xsrfKey, nextXSRFToken); err != nil {
_ = log.Err(
fmt.Sprintf(
"login handler error during xsrf set: %#v",
err,
),
)
util.InternalServerError.ServeHTTP(w, request)
return
}
errMarkup := ""
if errString != "" {
errMarkup = fmt.Sprintf(
"<label class=\"error\">%s</label><br />",
errString,
)
}
_, err := fmt.Fprintf(
w,
"<html><head><title>Pullcord Login</title></head><body>"+
"<form method=\"POST\" action=\"%s\"><fieldset>"+
"<legend>Pullcord Login</legend>%s"+
"<label for=\"username\">Username:</label>"+
"<input type=\"text\" name=\"%s\" id=\"username\" />"+
"<label for=\"password\">Password:</label>"+
"<input type=\"password\" name=\"%s\""+
"id=\"password\" /><input type=\"hidden\" name=\"%s\""+
" value=\"%s\" /><input type=\"submit\""+
" value=\"Login\"/></fieldset></form></body></html>",
request.URL.Path,
errMarkup,
usernameKey,
passwordKey,
xsrfKey,
nextXSRFToken,
)
if err != nil {
_ = log.Error(
fmt.Sprintf(
"Unable to write login form: %s",
err.Error(),
),
)
util.InternalServerError.ServeHTTP(w, request)
}
return
}