-
Notifications
You must be signed in to change notification settings - Fork 9
/
admin_web_app.go
288 lines (240 loc) · 7.08 KB
/
admin_web_app.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
282
283
284
285
286
287
288
package web
import (
"html/template"
"net/http"
"net/url"
"github.com/gorilla/csrf"
"github.com/gorilla/mux"
"github.com/inklabs/goauth2"
)
func (a *webApp) addAdminRoutes(r *mux.Router) {
csrfMiddleware := csrf.Protect(
a.csrfAuthKey,
csrf.Secure(false),
csrf.SameSite(csrf.SameSiteStrictMode),
)
r.HandleFunc("/admin-login", a.showAdminLogin).Methods(http.MethodGet)
r.HandleFunc("/admin-login", a.submitAdminLogin).Methods(http.MethodPost)
admin := r.PathPrefix("/admin").Subrouter()
admin.HandleFunc("/add-user", a.showAddUser).Methods(http.MethodGet)
admin.HandleFunc("/add-user", a.submitAddUser).Methods(http.MethodPost)
admin.HandleFunc("/list-users", a.listUsers)
admin.HandleFunc("/list-client-applications", a.listClientApplications)
admin.Use(
a.adminAuthorization,
csrfMiddleware,
)
}
type clientApplication struct {
ClientID string
ClientSecret string
CreateTimestamp uint64
}
type listClientApplicationsTemplateVars struct {
flashMessageVars
ClientApplications []clientApplication
}
func (a *webApp) listClientApplications(w http.ResponseWriter, _ *http.Request) {
var clientApplications []clientApplication
for _, ca := range a.projections.clientApplications.GetAll() {
clientApplications = append(clientApplications, clientApplication{
ClientID: ca.ClientID,
ClientSecret: ca.ClientSecret,
CreateTimestamp: ca.CreateTimestamp,
})
}
a.renderTemplate(w, "admin/list-client-applications.gohtml", listClientApplicationsTemplateVars{
ClientApplications: clientApplications,
})
}
type flashMessageVars struct {
Errors []string
Messages []string
}
type resourceOwnerUser struct {
UserID string
Username string
GrantingUserID string
CreateTimestamp uint64
IsAdmin bool
CanOnboardAdminApplications bool
}
type listUsersTemplateVars struct {
flashMessageVars
Users []resourceOwnerUser
}
func (a *webApp) listUsers(w http.ResponseWriter, r *http.Request) {
var users []resourceOwnerUser
for _, user := range a.projections.users.GetAll() {
users = append(users, resourceOwnerUser{
UserID: user.UserID,
Username: user.Username,
GrantingUserID: user.GrantingUserID,
CreateTimestamp: user.CreateTimestamp,
IsAdmin: user.IsAdmin,
CanOnboardAdminApplications: user.CanOnboardAdminApplications,
})
}
a.renderTemplate(w, "admin/list-users.gohtml", listUsersTemplateVars{
Users: users,
flashMessageVars: a.getFlashMessageVars(w, r),
})
}
type adminLoginTemplateVars struct {
flashMessageVars
Username string
CSRFField template.HTML
}
func (a *webApp) showAdminLogin(w http.ResponseWriter, r *http.Request) {
a.renderTemplate(w, "admin/login.gohtml", adminLoginTemplateVars{
Username: "", // TODO: Add when form post fails on redirect
CSRFField: csrf.TemplateField(r),
flashMessageVars: a.getFlashMessageVars(w, r),
})
}
func (a *webApp) submitAdminLogin(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
writeInvalidRequestResponse(w)
return
}
username := r.Form.Get("username")
password := r.Form.Get("password")
redirectURI := r.Form.Get("redirect")
userID, err := a.projections.emailToUserID.GetUserID(username)
if err != nil {
writeInvalidRequestResponse(w)
return
}
events := SavedEvents(a.goAuth2App.Dispatch(goauth2.RequestAccessTokenViaROPCGrant{
UserID: userID,
ClientID: ClientIDTODO,
ClientSecret: ClientSecretTODO,
Username: username,
Password: password,
Scope: "read_write",
}))
var accessTokenEvent goauth2.AccessTokenWasIssuedToUserViaROPCGrant
if !events.Get(&accessTokenEvent) {
writeInvalidRequestResponse(w)
return
}
user, err := a.projections.users.Get(userID)
if err != nil {
writeInvalidRequestResponse(w)
return
}
authenticatedUser := AuthenticatedUser{
UserID: userID,
Username: user.Username,
IsAdmin: user.IsAdmin,
}
err = a.setAuthenticatedUser(w, r, authenticatedUser)
if err != nil {
writeInternalServerErrorResponse(w)
return
}
if redirectURI == "" {
redirectURI = "/admin/list-users"
}
uri, err := url.Parse(redirectURI)
if err != nil {
writeInvalidRequestResponse(w)
return
}
http.Redirect(w, r, uri.String(), http.StatusSeeOther)
}
type addUserTemplateVars struct {
flashMessageVars
Username string
CSRFField template.HTML
}
func (a *webApp) showAddUser(w http.ResponseWriter, r *http.Request) {
a.renderTemplate(w, "admin/add-user.gohtml", addUserTemplateVars{
Username: "", // TODO: Add when form post fails on redirect
CSRFField: csrf.TemplateField(r),
flashMessageVars: a.getFlashMessageVars(w, r),
})
}
func (a *webApp) submitAddUser(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
writeInvalidRequestResponse(w)
return
}
username := r.Form.Get("username")
password := r.Form.Get("password")
// confirmPassword := r.Form.Get("confirm_password")
if username == "" || password == "" {
redirectURI := url.URL{
Path: "/admin/add-user",
}
a.FlashError(w, r, "username or password are required")
http.Redirect(w, r, redirectURI.String(), http.StatusFound)
return
}
authenticatedUser, err := a.getAuthenticatedUser(r)
if err != nil {
writeInternalServerErrorResponse(w)
return
}
userID := a.uuidGenerator.New()
events := SavedEvents(a.goAuth2App.Dispatch(goauth2.OnBoardUser{
UserID: userID,
Username: username,
Password: password,
GrantingUserID: authenticatedUser.UserID,
}))
var userWasOnBoarded goauth2.UserWasOnBoarded
if !events.Get(&userWasOnBoarded) {
redirectURI := url.URL{
Path: "/admin/add-user",
}
http.Redirect(w, r, redirectURI.String(), http.StatusFound)
return
}
a.FlashMessage(w, r, "User (%s) was added", username)
uri := url.URL{
Path: "/admin/list-users",
}
http.Redirect(w, r, uri.String(), http.StatusFound)
}
type AuthenticatedUser struct {
UserID string
Username string
IsAdmin bool
}
func (a *webApp) getAuthenticatedUser(r *http.Request) (AuthenticatedUser, error) {
session, err := a.sessionStore.Get(r, sessionName)
if err != nil {
return AuthenticatedUser{}, err
}
if user, ok := session.Values["user"].(AuthenticatedUser); ok {
return user, nil
}
return AuthenticatedUser{}, err
}
func (a *webApp) setAuthenticatedUser(w http.ResponseWriter, r *http.Request, user AuthenticatedUser) error {
session, err := a.sessionStore.Get(r, sessionName)
if err != nil {
return err
}
session.Values["user"] = user
return session.Save(r, w)
}
func (a *webApp) adminAuthorization(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authenticatedUser, err := a.getAuthenticatedUser(r)
if err == nil && authenticatedUser.IsAdmin {
h.ServeHTTP(w, r)
return
}
params := &url.Values{}
params.Set("redirect", r.RequestURI)
loginUri := url.URL{
Path: "/admin-login",
RawQuery: params.Encode(),
}
http.Redirect(w, r, loginUri.String(), http.StatusSeeOther)
})
}