forked from furkansenharputlu/f-license
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
169 lines (134 loc) · 3.91 KB
/
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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/furkansenharputlu/f-license/config"
"github.com/furkansenharputlu/f-license/lcs"
"github.com/furkansenharputlu/f-license/storage"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
func GenerateLicense(w http.ResponseWriter, r *http.Request) {
bytes, _ := ioutil.ReadAll(r.Body)
var l lcs.License
_ = json.Unmarshal(bytes, &l)
err := l.Generate()
if err != nil {
logrus.WithError(err).Error("License couldn't be generated")
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
err = storage.LicenseHandler.AddIfNotExisting(&l)
if err != nil {
logrus.WithError(err).Error("License couldn't be stored")
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
ReturnResponse(w, http.StatusOK, map[string]interface{}{
"id": l.ID.Hex(),
"token": l.Token,
})
}
func GetLicense(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
var l lcs.License
err := storage.LicenseHandler.GetByID(id, &l)
if err != nil {
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
ReturnResponse(w, 200, l)
}
func GetAllLicenses(w http.ResponseWriter, r *http.Request) {
var licenses []*lcs.License
err := storage.LicenseHandler.GetAll(&licenses)
if err != nil {
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
ReturnResponse(w, 200, licenses)
}
func ChangeLicenseActiveness(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
inactivate := strings.Contains(r.URL.Path, "/inactivate")
err := storage.LicenseHandler.Activate(id, inactivate)
if err != nil {
logrus.WithError(err).Error("Error while activeness change")
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
var message string
if inactivate {
message = "Inactivated"
} else {
message = "Activated"
}
ReturnResponse(w, 200, map[string]interface{}{
"message": message,
})
}
func VerifyLicense(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
var l lcs.License
err := storage.LicenseHandler.GetByToken(token, &l)
if err != nil {
logrus.WithError(err).Error("Error while getting license")
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
ok, err := l.IsLicenseValid(token)
if err != nil {
ReturnResponse(w, http.StatusUnauthorized, map[string]interface{}{
"valid": false,
"message": err.Error(),
})
return
}
ReturnResponse(w, 200, map[string]interface{}{
"valid": ok,
})
}
func DeleteLicense(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
err := storage.LicenseHandler.DeleteByID(id)
if err != nil {
logrus.WithError(err).Error("Error while deleting license")
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
ReturnResponse(w, 200, map[string]interface{}{
"message": "License successfully deleted",
})
}
func Ping(w http.ResponseWriter, r *http.Request) {
}
func ReturnResponse(w http.ResponseWriter, statusCode int, resp interface{}) {
bytes, _ := json.Marshal(resp)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
_, _ = fmt.Fprintf(w, string(bytes))
}
func ReturnError(w http.ResponseWriter, statusCode int, errMsg string) {
resp := map[string]interface{}{
"error": errMsg,
}
bytes, _ := json.Marshal(resp)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
_, _ = fmt.Fprintf(w, string(bytes))
}
func AuthenticationMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != config.Global.AdminSecret {
ReturnResponse(w, http.StatusUnauthorized, map[string]interface{}{
"message": "Authorization failed",
})
return
}
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}