-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_user.go
150 lines (128 loc) · 3.59 KB
/
handler_user.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 server
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type UserHandler struct {
http.Handler
userStore UserStore
}
func NewUserHandler(store UserStore) *UserHandler {
uh := new(UserHandler)
uh.userStore = store
router := http.NewServeMux()
router.HandleFunc("/login", http.HandlerFunc(uh.LoginHandler))
router.HandleFunc("/logout", http.HandlerFunc(uh.LogoutHandler))
router.HandleFunc("/signup", http.HandlerFunc(uh.SignupHandler))
router.HandleFunc("/reset", http.HandlerFunc(uh.ResetHandler))
uh.Handler = router
return uh
}
func (uh *UserHandler) LoginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
if ct := r.Header.Get("Content-Type"); !strings.Contains(ct, "application/json") {
http.Error(w, fmt.Sprintf("Unsupported Media Type %s", ct), http.StatusUnsupportedMediaType)
return
}
// read credentials from request body
var c Credentials
err := json.NewDecoder(r.Body).Decode(&c)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// login
token, login_err := uh.userStore.Login(c)
if login_err != nil {
http.Error(w, login_err.Error(), http.StatusBadRequest)
return
}
// return success
w.WriteHeader(http.StatusOK)
w.Write([]byte(token))
}
func (uh *UserHandler) LogoutHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
if r.Header.Get("Authorization") == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
authHeader := r.Header.Get("Authorization")
splitToken := strings.Split(authHeader, "Bearer ")
if len(splitToken) != 2 {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
token := splitToken[1]
// logout
logout_err := uh.userStore.Logout(token)
if logout_err != nil {
http.Error(w, logout_err.Error(), http.StatusBadRequest)
return
}
// return success
w.WriteHeader(http.StatusOK)
return
}
func (uh *UserHandler) SignupHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
if ct := r.Header.Get("Content-Type"); !strings.Contains(ct, "application/json") {
http.Error(w, fmt.Sprintf("Unsupported Media Type %s", ct), http.StatusUnsupportedMediaType)
return
}
// read credentials from request body
var c Credentials
err := json.NewDecoder(r.Body).Decode(&c)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// signup
signup_err := uh.userStore.Signup(c)
if signup_err != nil {
http.Error(w, signup_err.Error(), http.StatusBadRequest)
return
}
// return success
w.WriteHeader(http.StatusCreated)
return
}
func (uh *UserHandler) ResetHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
// get authorization token
authHeader := r.Header.Get("Authorization")
splitToken := strings.Split(authHeader, "Bearer ")
if len(splitToken) != 2 {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
token := splitToken[1]
// get user from request context
user_id, err := uh.userStore.UserIdFromToken(token)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// reset user
reset_err := uh.userStore.ResetUser(user_id)
if reset_err != nil {
http.Error(w, reset_err.Error(), http.StatusBadRequest)
return
}
// return success
w.WriteHeader(http.StatusOK)
}