-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauth.go
281 lines (248 loc) · 7.62 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package searchrefiner
import (
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net/http"
)
func HandleAccountLogin(c *gin.Context) {
c.HTML(http.StatusOK, "account_login.html", nil)
}
func HandleAccountCreate(c *gin.Context) {
c.HTML(http.StatusOK, "account_create.html", nil)
}
func (s Server) ApiAccountLogin(c *gin.Context) {
var username, password string
if v, ok := c.GetPostForm("username"); ok {
username = v
} else {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: "no username supplied", BackLink: "/account/login"})
return
}
if v, ok := c.GetPostForm("password"); ok {
password = v
} else {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: "no password supplied", BackLink: "/account/login"})
return
}
if s.Perm.UserState().CorrectPassword(username, password) {
err := s.Perm.UserState().Login(c.Writer, username)
if err != nil {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: err.Error(), BackLink: "/account/login"})
return
}
log.Info(fmt.Sprintf("[login=%s]", username))
c.Redirect(http.StatusFound, "/")
return
}
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: "invalid login credentials", BackLink: "/account/login"})
c.Status(http.StatusUnauthorized)
return
}
func (s Server) ApiAccountCreate(c *gin.Context) {
var username, password, password2 string
if v, ok := c.GetPostForm("username"); ok {
username = v
} else {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: "no username supplied", BackLink: "/account/create"})
return
}
if v, ok := c.GetPostForm("password"); ok {
password = v
} else {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: "passwords do not match", BackLink: "/account/create"})
return
}
if v, ok := c.GetPostForm("password2"); ok {
password2 = v
} else {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: "passwords do not match", BackLink: "/account/create"})
return
}
if password != password2 {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: "passwords do not match", BackLink: "/account/create"})
return
}
if s.Perm.UserState().HasUser(username) {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: "a user with that name already exists", BackLink: "/account/create"})
return
}
isAdmin := false
for _, u := range s.Config.Admins {
if u == username {
s.Perm.UserState().AddUser(username, password, username)
s.Perm.UserState().SetAdminStatus(username)
isAdmin = true
break
}
}
if !isAdmin {
s.Perm.UserState().AddUser(username, password, username)
}
s.Perm.UserState().MarkConfirmed(username)
err := s.Perm.UserState().Login(c.Writer, username)
if err != nil {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: err.Error(), BackLink: "/account/create"})
return
}
c.Redirect(http.StatusFound, "/")
return
}
func (s Server) ApiAccountLogout(c *gin.Context) {
username := s.Perm.UserState().Username(c.Request)
if s.Perm.UserState().IsLoggedIn(username) {
s.Perm.UserState().Logout(username)
s.Perm.UserState().ClearCookie(c.Writer)
}
c.Redirect(http.StatusFound, "/account/login")
return
}
func (s Server) ApiAccountUsername(c *gin.Context) {
username := s.Perm.UserState().Username(c.Request)
if !s.Perm.UserState().IsLoggedIn(username) {
c.String(http.StatusOK, "anonymous")
return
}
c.String(http.StatusOK, username)
return
}
func (s Server) HandleAdmin(c *gin.Context) {
u, err := s.Perm.UserState().AllUnconfirmedUsernames()
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: err.Error(), BackLink: "/"})
return
}
conf, err := s.Perm.UserState().AllUsernames()
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: err.Error(), BackLink: "/"})
return
}
storage, err := s.getAllPluginStorage()
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: err.Error(), BackLink: "/"})
return
}
type admin struct {
Unconfirmed []string
Confirmed []string
Storage map[string]map[string]map[string]string
}
c.HTML(http.StatusOK, "admin.html", admin{Unconfirmed: u, Confirmed: conf, Storage: storage})
}
func (s Server) ApiAdminConfirm(c *gin.Context) {
if v, ok := c.GetPostForm("username"); ok {
s.Perm.UserState().Confirm(v)
} else {
c.HTML(http.StatusUnauthorized, "error.html", ErrorPage{Error: "invalid credentials", BackLink: "/"})
return
}
c.Redirect(http.StatusFound, "/admin")
}
func (s Server) ApiAdminUpdateStorage(c *gin.Context) {
var (
plugin string
bucket string
key string
value string
)
if v, ok := c.GetPostForm("plugin"); ok {
plugin = v
} else {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "cannot update storage", BackLink: "/admin"})
return
}
if v, ok := c.GetPostForm("bucket"); ok {
bucket = v
} else {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "cannot update storage", BackLink: "/admin"})
return
}
if v, ok := c.GetPostForm("key"); ok {
key = v
} else {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "cannot update storage", BackLink: "/admin"})
return
}
if v, ok := c.GetPostForm("value"); ok {
value = v
} else {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "cannot update storage", BackLink: "/admin"})
return
}
var ps *PluginStorage
ps, ok := s.Storage[plugin]
if !ok {
var err error
ps, err = OpenPluginStorage(plugin)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "cannot update storage", BackLink: "/admin"})
return
}
s.Storage[plugin] = ps
}
err := ps.PutValue(bucket, key, value)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: err.Error(), BackLink: "/admin"})
return
}
c.Redirect(http.StatusFound, "/admin")
}
func (s Server) ApiAdminDeleteStorage(c *gin.Context) {
var (
plugin string
bucket string
key string
)
if v, ok := c.GetPostForm("plugin"); ok {
plugin = v
} else {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "cannot update storage", BackLink: "/admin"})
return
}
if v, ok := c.GetPostForm("bucket"); ok {
bucket = v
} else {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "cannot update storage", BackLink: "/admin"})
return
}
if v, ok := c.GetPostForm("key"); ok {
key = v
} else {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "cannot update storage", BackLink: "/admin"})
return
}
ps, ok := s.Storage[plugin]
if !ok {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "cannot update storage", BackLink: "/admin"})
return
}
err := ps.DeleteKey(bucket, key)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: err.Error(), BackLink: "/admin"})
return
}
c.Redirect(http.StatusFound, "/admin")
}
func (s Server) ApiAdminCSVStorage(c *gin.Context) {
plugin, ok := c.GetPostForm("plugin")
if !ok {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "plugin not found", BackLink: "/admin"})
return
}
bucket, ok := c.GetPostForm("bucket")
if !ok {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: "bucket not found", BackLink: "/admin"})
return
}
var resp string
if ps, ok := s.Storage[plugin]; ok {
var err error
resp, err = ps.ToCSV(bucket)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", ErrorPage{Error: err.Error(), BackLink: "/admin"})
return
}
}
c.Data(http.StatusFound, "text/plain", []byte(resp))
return
}