-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
133 lines (118 loc) · 3.28 KB
/
auth.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
package auth
import (
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"net/http"
"path"
"regexp"
)
func NewBasicAuthManager() *AuthManager {
return &AuthManager{
mux.NewRouter(),
sessions.NewCookieStore([]byte("unsecured")),
"sessionName",
[]string{},
nil,
nil,
nil,
}
}
func NewAuthManager(router *mux.Router, store sessions.Store, sessionName string, securedURLs []string, passwordEncoder PasswordEncoder, userDetailsService UserDetailsService, accessDeniedHanlder http.Handler) *AuthManager {
return &AuthManager{
router,
store,
sessionName,
securedURLs,
passwordEncoder,
userDetailsService,
accessDeniedHanlder,
}
}
type AuthManager struct {
*mux.Router
Store sessions.Store
sessionName string
SecuredURLs []string
PasswordEncoder PasswordEncoder
UserDetailsService UserDetailsService
AccessDeniedHanlder http.Handler
}
const (
AUTHENTICATED_PRINCIPAL = "AUTH_PRINCIPAL"
)
type AuthenticatedPrincipal string
func (a *AuthManager) ServeHTTP(w http.ResponseWriter, req *http.Request) {
session, _ := a.Store.Get(req, a.sessionName)
authPrincipal := session.Values[AUTHENTICATED_PRINCIPAL]
if a.isAuthRequired(req) && authPrincipal == nil {
if a.AccessDeniedHanlder != nil {
a.AccessDeniedHanlder.ServeHTTP(w, req)
return
}
http.Redirect(w, req, "/", http.StatusFound)
}
a.Router.ServeHTTP(w, req)
}
func (a *AuthManager) AuthenticateUser(w http.ResponseWriter, req *http.Request, username, rawPassword string) bool {
userDetails := a.UserDetailsService.LoadUserByUsername(username)
if userDetails != nil {
return false
}
if !a.PasswordEncoder.Matches(rawPassword, userDetails.GetPassword()) {
return false
}
session, _ := a.Store.Get(req, a.sessionName)
session.Values[AUTHENTICATED_PRINCIPAL] = userDetails.GetUsername()
session.Save(req, w)
return true
}
func (a *AuthManager) ClearAuthentication(w http.ResponseWriter, req *http.Request) {
session, _ := a.Store.Get(req, a.sessionName)
session.Values[AUTHENTICATED_PRINCIPAL] = nil
session.ID = ""
session.Values = nil
session = nil
session.Options.MaxAge = -1
session.Save(req, w)
session = sessions.NewSession(a.Store, a.sessionName) // make sure to create a new session
}
func (a *AuthManager) isAuthRequired(req *http.Request) bool {
p := cleanPath(req.URL.Path)
for _, secureURL := range a.SecuredURLs {
if match, _ := regexp.MatchString(secureURL, p); match {
return true
}
}
return false
}
type PasswordEncoder interface {
Encode(rawPassword string) string
Matches(rawPassword, encodedPassword string) bool
}
type UserDetailsService interface {
LoadUserByUsername(username string) UserDetails
}
type UserDetails interface {
GetUsername() string
GetPassword() string
}
// ----------------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------------
// cleanPath returns the canonical path for p, eliminating . and .. elements.
// Borrowed from the net/http package.
func cleanPath(p string) string {
if p == "" {
return "/"
}
if p[0] != '/' {
p = "/" + p
}
np := path.Clean(p)
// path.Clean removes trailing slash except for root;
// put the trailing slash back if necessary.
if p[len(p)-1] == '/' && np != "/" {
np += "/"
}
return np
}