-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticator.go
56 lines (49 loc) · 1.19 KB
/
authenticator.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
package authenticator
import (
"github.com/gorilla/securecookie"
"net/http"
"strings"
)
type Authenticator struct {
Login string
Logout string
Secret string
Token string
}
func NewAuthenticator() *Authenticator {
return &Authenticator{"/login", "/logout", "secret", "auth"}
}
func (a *Authenticator) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
var hashKey = []byte(a.Secret)
var blockKey = []byte("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
var s = securecookie.New(hashKey, blockKey)
if !strings.Contains(r.URL.Path, "admin") {
next(rw, r)
return
}
var illegalCookie bool
if cookie, cookieerr := r.Cookie("user"); cookieerr == nil {
results := make(map[string]string)
tampered := s.Decode("user", cookie.Value, &results)
if tampered != nil {
http.Error(rw, "Unauthorized", 401)
illegalCookie = true
} else {
illegalCookie = false
}
} else {
illegalCookie = false
}
if illegalCookie == false {
_, err := r.Cookie("user")
if err != nil {
http.Redirect(rw, r, a.Login, 401)
cookie := http.Cookie{Name: "redirect", Value: r.URL.Path, Path: "/"}
http.SetCookie(rw, &cookie)
} else {
next(rw, r)
}
return
}
next(rw, r)
}