-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
142 lines (131 loc) · 3.48 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
134
135
136
137
138
139
140
141
142
package server
import (
"context"
"net/http"
"net/url"
"github.com/ReformedDevs/anonbot/db"
"github.com/flosch/pongo2"
)
const (
contextUser = "user"
sessionName = "session"
sessionUserID = "userID"
sessionAccessToken = "accessToken"
sessionAccessSecret = "accessSecret"
invalidCredentials = "invalid username or password"
)
func (s *Server) loadUser(r *http.Request) *http.Request {
session, _ := s.store.Get(r, sessionName)
v, _ := session.Values[sessionUserID]
if v != nil {
u := &db.User{}
if err := s.database.C.First(u, v).Error; err == nil {
r = r.WithContext(context.WithValue(r.Context(), contextUser, u))
}
}
return r
}
func (s *Server) requireLogin(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Context().Value(contextUser) == nil {
u := url.QueryEscape(r.URL.RequestURI())
http.Redirect(w, r, "/login?url="+u, http.StatusFound)
return
}
fn(w, r)
}
}
func (s *Server) requireAdmin(fn http.HandlerFunc) http.HandlerFunc {
return s.requireLogin(func(w http.ResponseWriter, r *http.Request) {
if !r.Context().Value(contextUser).(*db.User).IsAdmin {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
fn(w, r)
})
}
func (s *Server) login(w http.ResponseWriter, r *http.Request) {
ctx := pongo2.Context{
"title": "Login",
}
if r.Method == http.MethodPost {
for {
var (
username = r.Form.Get("username")
password = r.Form.Get("password")
u = &db.User{}
)
ctx["username"] = username
ctx["password"] = password
if err := s.database.C.Where("username = ?", username).First(&u).Error; err != nil {
ctx["error"] = invalidCredentials
break
}
if !u.IsActive {
ctx["error"] = "account is not active"
break
}
if err := u.Authenticate(password); err != nil {
ctx["error"] = invalidCredentials
break
}
session, _ := s.store.Get(r, sessionName)
session.Values[sessionUserID] = u.ID
session.Save(r, w)
redir := r.URL.Query().Get("url")
if len(redir) == 0 {
redir = "/"
}
http.Redirect(w, r, redir, http.StatusFound)
return
}
}
s.render(w, r, "login.html", ctx)
}
func (s *Server) register(w http.ResponseWriter, r *http.Request) {
ctx := pongo2.Context{
"title": "Register",
}
if r.Method == http.MethodPost {
for {
var (
username = r.Form.Get("username")
password = r.Form.Get("password")
password2 = r.Form.Get("password2")
email = r.Form.Get("email")
u = &db.User{
Username: username,
Email: email,
}
)
ctx["username"] = username
ctx["email"] = email
if len(username) == 0 || len(password) == 0 || len(email) == 0 {
ctx["error"] = "invalid input"
break
}
if password != password2 {
ctx["error"] = "passwords do not match"
break
}
if err := u.SetPassword(password); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := s.database.C.Create(u).Error; err != nil {
ctx["error"] = "unable to create user"
break
}
s.addAlert(w, r, "Thank you for registering. Please wait for an admin to activate your account.")
http.Redirect(w, r, "/", http.StatusFound)
return
}
}
s.render(w, r, "register.html", ctx)
}
func (s *Server) logout(w http.ResponseWriter, r *http.Request) {
session, _ := s.store.Get(r, sessionName)
session.Values[sessionUserID] = nil
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
}