-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadminhandler.go
More file actions
140 lines (114 loc) · 3.64 KB
/
Copy pathadminhandler.go
File metadata and controls
140 lines (114 loc) · 3.64 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
goboardbackend "github.com/dguihal/goboard/internal/backend"
goboardcookie "github.com/dguihal/goboard/internal/cookie"
goboarduser "github.com/dguihal/goboard/internal/user"
"github.com/gorilla/mux"
)
const tokenMinLen int = 0
const tokenWarnLen int = 12
// AdminHandler represents the handler of admin URLs
type AdminHandler struct {
GoBoardHandler
adminToken string
}
// NewAdminHandler creates an AdminHandler object
func NewAdminHandler(adminToken string) (a *AdminHandler) {
a = &AdminHandler{}
a.supportedOps = []SupportedOp{
{"/admin/user/", "/admin/user/{login}", "DELETE", a.deleteUser}, // Delete a user
{"/admin/user/", "/admin/user/{login}", "GET", a.getUser}, // Get a user info
{"/admin/post/", "/admin/post/{id}", "DELETE", a.deletePost}, // Delete a post
}
if len(adminToken) <= tokenMinLen {
log.Println("Admin token empty : for security reasongs, this means that no admin operations will be authorized")
} else if len(adminToken) < tokenWarnLen {
log.Println("Admin token len <", tokenWarnLen, ": Come on I'm sure you can do a lot better")
}
a.adminToken = adminToken
return
}
func (a *AdminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
reqAdminToken := r.Header.Get("Token-Id")
if !a.checkAdminToken(reqAdminToken) {
w.WriteHeader(http.StatusUnauthorized)
return
}
for _, op := range a.supportedOps {
if r.Method == op.Method && strings.HasPrefix(r.URL.Path, op.PathBase) {
// Call specific handling method
op.handler(w, r)
return
}
}
// If we are here : no methods has been found (shouldn't happen)
w.WriteHeader(http.StatusNotFound)
}
func (a *AdminHandler) deleteUser(w http.ResponseWriter, r *http.Request) {
login := (mux.Vars(r))["login"]
if err := goboarduser.DeleteUser(a.Db, login); err != nil {
if uerr, ok := err.(*goboarduser.Error); ok {
if uerr.ErrCode == goboarduser.UserDoesNotExistsError {
w.WriteHeader(http.StatusNotFound)
if _, err := w.Write([]byte(fmt.Sprintf("User %s Not found", login))); err != nil {
log.Printf("Error writing response: %v", err)
}
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err.Error())
} else {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err.Error())
}
}
if err := goboardcookie.DeleteCookiesForUser(a.Db, login); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err.Error())
return
}
w.WriteHeader(http.StatusOK)
}
func (a *AdminHandler) deletePost(w http.ResponseWriter, rq *http.Request) {
postID := (mux.Vars(rq))["id"]
id, err := strconv.ParseUint(postID, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
if _, wErr := w.Write([]byte(err.Error())); wErr != nil {
log.Printf("Error writing response: %v", wErr)
}
return
}
if err := goboardbackend.DeletePost(a.Db, id); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err.Error())
return
}
w.WriteHeader(http.StatusOK)
}
func (a *AdminHandler) getUser(w http.ResponseWriter, r *http.Request) {
login := (mux.Vars(r))["login"]
if user, err := goboarduser.GetUser(a.Db, login); err != nil {
w.WriteHeader(http.StatusNotFound)
} else {
data, err := json.Marshal(user)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err.Error())
} else {
w.WriteHeader(http.StatusOK)
if _, err := w.Write(data); err != nil {
log.Printf("Error writing response: %v", err)
}
}
}
}
func (a *AdminHandler) checkAdminToken(token string) bool {
return len(a.adminToken) > tokenMinLen && token == a.adminToken
}