-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsession.go
150 lines (135 loc) · 3.29 KB
/
session.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
package main
import (
"context"
"encoding/gob"
"errors"
"net/http"
"time"
"github.com/gorilla/securecookie"
)
var (
UserNotFound = errors.New("找不到此帳號")
WrongPassword = errors.New("密碼錯誤")
AccountAlreadyExist = errors.New("帳號已經存在")
)
type Session struct {
ID string
ExpireAt time.Time
}
func init() {
gob.Register(Session{})
}
func session(r *http.Request) (acct Account, ok bool) {
acct, ok = r.Context().Value(KeyAccount).(Account)
return
}
func (h Handler) auth(next http.HandlerFunc, recordLevel, acctLevel AuthorityLevel) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
authority := Authority{
Record: recordLevel,
Account: acctLevel,
}
if acct, ok := r.Context().Value(KeyAccount).(Account); ok && acct.Authority.permission(authority) {
next.ServeHTTP(w, r)
} else {
h.message(w, r, "權限不足", "您沒有權限檢視此頁面")
}
}
}
func (h Handler) identify(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s := securecookie.New(hashKey, blockKey)
cookie, err := r.Cookie("session")
if err != nil {
next.ServeHTTP(w, r)
return
}
var session Session
if err = s.Decode("session", cookie.Value, &session); err != nil {
logout(w, r)
next.ServeHTTP(w, r)
return
}
if time.Now().After(session.ExpireAt) {
// session expire
logout(w, r)
next.ServeHTTP(w, r)
return
}
var acct Account
if err = h.db.Set("gorm:auto_preload", true).First(&acct, "id = ?", session.ID).Error; err != nil {
logout(w, r)
next.ServeHTTP(w, r)
return
}
ctx := r.Context()
ctx = context.WithValue(ctx, KeyAccount, acct)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}
func newSession(id string) *http.Cookie {
s := securecookie.New(hashKey, blockKey)
var encoded string
var err error
session := Session{
ID: id,
ExpireAt: expire(),
}
if encoded, err = s.Encode("session", session); err != nil {
panic(err)
}
return &http.Cookie{
Name: "session",
Value: encoded,
Path: "/",
Expires: session.ExpireAt,
}
}
func expire() time.Time {
return time.Now().AddDate(0, 0, 7)
}
func (h Handler) login(w http.ResponseWriter, r *http.Request) {
acct, err := h.getAccount(r.FormValue("username"))
if err == AccountNotFound {
h.HTML(w, addMessage(r, UserNotFound.Error()), "login.htm", nil)
return
}
password := r.FormValue("password")
if !acct.login(password) {
h.HTML(w, addMessage(r, WrongPassword.Error()), "login.htm", nil)
return
}
http.SetCookie(w, newSession(acct.ID))
if acct.EmptyPassword() {
http.Redirect(w, r, "/reset", 303)
} else {
http.Redirect(w, r, "/", 303)
}
}
func logout(w http.ResponseWriter, r *http.Request) {
if cookie, err := r.Cookie("session"); err == nil {
cookie.MaxAge = -1
http.SetCookie(w, cookie)
}
http.Redirect(w, r, "/login", 303)
}
func (h Handler) register(w http.ResponseWriter, r *http.Request) {
next := func(msg string) {
h.registerPage(w, addMessage(r, msg))
}
acct, err := NewAccount(
h.db,
r.FormValue("account_id"),
r.FormValue("name"),
r.FormValue("password"),
r.FormValue("class"),
r.FormValue("number"),
r.FormValue("authority"),
)
if err != nil {
next(err.Error())
return
}
next("成功註冊" + acct.Name)
}