forked from palark/ovpn-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-api-key.go
95 lines (86 loc) · 2.7 KB
/
router-api-key.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"log"
"net/http"
"regexp"
"rpiadm/backend/preference"
)
func (app *OvpnAdmin) handleApiKey(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s\n", r.RemoteAddr, r.RequestURI)
log.Printf("%s api key %s", r.Method, r.URL)
regId := regexp.MustCompile("^/api/config/api-key/(.*)$")
if r.Method == "DELETE" {
matches := regId.FindStringSubmatch(r.URL.Path)
if len(matches) > 0 {
app.deleteApiKeyById(w, matches[1])
return
} else {
returnErrorMessage(w, http.StatusBadRequest, errors.New("bad request"))
return
}
}
var apiKeyUpdateUpdate preference.ApiKeyUpdate
err := json.NewDecoder(r.Body).Decode(&apiKeyUpdateUpdate)
if err != nil {
log.Printf(err.Error())
return
}
if r.Method == "PUT" {
matches := regId.FindStringSubmatch(r.URL.Path)
if len(matches) > 0 {
app.updateApiKeyById(w, matches[1], apiKeyUpdateUpdate)
return
} else {
returnErrorMessage(w, http.StatusBadRequest, errors.New("bad request"))
return
}
} else if r.Method == "POST" && r.URL.Path == "/api/config/api-key/" {
app.CreateApiKey(w, apiKeyUpdateUpdate)
return
}
returnErrorMessage(w, http.StatusBadRequest, errors.New("bad request"))
}
func (app *OvpnAdmin) CreateApiKey(w http.ResponseWriter, update preference.ApiKeyUpdate) {
apiKey, err := preference.CreateApiKey(*ovpnConfigDir, &app.applicationPreferences, update)
if err != nil {
returnErrorMessage(w, http.StatusBadRequest, errors.New(fmt.Sprintf("failed to create api key %s", err)))
return
}
err = returnJson(w, preference.ApiKeyMapper(*apiKey))
if err != nil {
log.Printf("error sending response")
}
}
func (app *OvpnAdmin) deleteApiKeyById(w http.ResponseWriter, idStr string) {
id, err := uuid.Parse(idStr)
if err != nil {
returnErrorMessage(w, http.StatusBadRequest, errors.New("invalid uuid"))
return
}
err = preference.DeleteApiKey(*ovpnConfigDir, &app.applicationPreferences, id)
if err != nil {
returnErrorMessage(w, http.StatusBadRequest, errors.New(fmt.Sprintf("failed to delete api key %s", err)))
return
}
w.WriteHeader(http.StatusNoContent)
}
func (app *OvpnAdmin) updateApiKeyById(w http.ResponseWriter, idStr string, update preference.ApiKeyUpdate) {
id, err := uuid.Parse(idStr)
if err != nil {
returnErrorMessage(w, http.StatusBadRequest, errors.New("invalid uuid"))
return
}
apiKey, err := preference.UpdateApiKey(*ovpnConfigDir, &app.applicationPreferences, id, update)
if err != nil {
returnErrorMessage(w, http.StatusBadRequest, errors.New(fmt.Sprintf("failed to delete api key %s", err)))
return
}
err = returnJson(w, preference.ApiKeyMapper(*apiKey))
if err != nil {
log.Printf("error sending response")
}
}