This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
handlers_api.go
106 lines (88 loc) · 2.76 KB
/
handlers_api.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
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
// handleAPIKeyGenerate handles generating an API key for a user
func (s *server) handleAPIKeyGenerate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
keyVal := s.getJSONRequestBody(r, w)
APIKeyName := keyVal["name"].(string)
UserID := vars["id"]
UserCookieID := r.Context().Value(contextKeyUserID).(string)
if UserID != UserCookieID {
w.WriteHeader(http.StatusForbidden)
return
}
APIKey, keyErr := s.database.GenerateAPIKey(UserID, APIKeyName)
if keyErr != nil {
log.Println("error attempting to generate api key : " + keyErr.Error() + "\n")
w.WriteHeader(http.StatusInternalServerError)
return
}
s.respondWithJSON(w, http.StatusOK, APIKey)
}
}
// handleUserAPIKeys handles getting user API keys
func (s *server) handleUserAPIKeys() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
UserID := vars["id"]
UserCookieID := r.Context().Value(contextKeyUserID).(string)
if UserID != UserCookieID {
w.WriteHeader(http.StatusForbidden)
return
}
APIKeys, keysErr := s.database.GetUserAPIKeys(UserID)
if keysErr != nil {
log.Println("error retrieving api keys : " + keysErr.Error() + "\n")
w.WriteHeader(http.StatusInternalServerError)
return
}
s.respondWithJSON(w, http.StatusOK, APIKeys)
}
}
// handleUserAPIKeyUpdate handles getting user API keys
func (s *server) handleUserAPIKeyUpdate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
UserID := vars["id"]
UserCookieID := r.Context().Value(contextKeyUserID).(string)
if UserID != UserCookieID {
w.WriteHeader(http.StatusForbidden)
return
}
APK := vars["keyID"]
keyVal := s.getJSONRequestBody(r, w)
active := keyVal["active"].(bool)
APIKeys, keysErr := s.database.UpdateUserAPIKey(UserID, APK, active)
if keysErr != nil {
log.Println("error updating api key : " + keysErr.Error() + "\n")
w.WriteHeader(http.StatusInternalServerError)
return
}
s.respondWithJSON(w, http.StatusOK, APIKeys)
}
}
// handleUserAPIKeyDelete handles getting user API keys
func (s *server) handleUserAPIKeyDelete() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
UserID := vars["id"]
UserCookieID := r.Context().Value(contextKeyUserID).(string)
if UserID != UserCookieID {
w.WriteHeader(http.StatusForbidden)
return
}
APK := vars["keyID"]
APIKeys, keysErr := s.database.DeleteUserAPIKey(UserID, APK)
if keysErr != nil {
log.Println("error deleting api key : " + keysErr.Error() + "\n")
w.WriteHeader(http.StatusInternalServerError)
return
}
s.respondWithJSON(w, http.StatusOK, APIKeys)
}
}