-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlocal.go
More file actions
54 lines (50 loc) · 1.46 KB
/
local.go
File metadata and controls
54 lines (50 loc) · 1.46 KB
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
/*Package dmv simple authentication schemes for Martini*/
package dmv
import (
"errors"
"github.com/codegangsta/martini"
"net/http"
)
// Local is mapped to the martini.Context from the martini.Handler
// returned from AuthLocal.
type Local struct {
Errors []error
Username string
Password string
}
// LocalOptions are used to pass conditional arguments to AuthLocal.
type LocalOptions struct {
// The form field to represent a username.
UsernameField string
// The form field to represent a password.
PasswordField string
}
// AuthLocal attempts to get a username and password from a request.
//
// m.Post("/login", dmv.AuthLocal(), func(l *dmv.Local) {
// if len(l.Errors) > 0 {
// // Return "invalid username or password" message or perhaps 401.
// }
// // Lookup user by l.Username
// // Compare password of found user to l.Password
// })
func AuthLocal(opts *LocalOptions) martini.Handler {
if opts.UsernameField == "" {
opts.UsernameField = "username"
}
if opts.PasswordField == "" {
opts.PasswordField = "password"
}
return func(req *http.Request, c martini.Context) {
l := &Local{}
l.Username = req.FormValue(opts.UsernameField)
if l.Username == "" {
l.Errors = append(l.Errors, errors.New("username field not found or empty"))
}
l.Password = req.FormValue(opts.PasswordField)
if l.Password == "" {
l.Errors = append(l.Errors, errors.New("password field not found or empty"))
}
c.Map(l)
}
}