-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathapi.go
169 lines (135 loc) · 4.16 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"
"errors"
"fmt"
"io/ioutil"
"net/http"
tykerror "github.com/TykTechnologies/tyk-identity-broker/error"
"github.com/TykTechnologies/tyk-identity-broker/tap"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
var APILogTag string = "API"
type APIOKMessage struct {
Status string
ID string
Data interface{}
}
func HandleAPIOK(data interface{}, id string, code int, w http.ResponseWriter, r *http.Request) {
okObj := APIOKMessage{
Status: "ok",
ID: id,
Data: data,
}
responseMsg, err := json.Marshal(&okObj)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": APILogTag,
"error": err,
}).Error("[OK Handler] Couldn't marshal message stats")
fmt.Fprintf(w, "System Error")
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
fmt.Fprintf(w, string(responseMsg))
}
func HandleAPIError(tag string, errorMsg string, rawErr error, code int, w http.ResponseWriter, r *http.Request) {
log.WithFields(logrus.Fields{
"prefix": tag,
"error": errorMsg,
}).Error(rawErr)
errorObj := tykerror.APIErrorMessage{Status: "error", Error: errorMsg}
responseMsg, err := json.Marshal(&errorObj)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": tag,
"error": err,
}).Error("[Error Handler] Couldn't marshal error stats")
fmt.Fprintf(w, "System Error")
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
fmt.Fprintf(w, string(responseMsg))
}
// ------ Middleware methods -------
func IsAuthenticated(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != config.Secret {
HandleAPIError(APILogTag, "Authorization failed", errors.New("Header mismatch"), 401, w, r)
return
}
h.ServeHTTP(w, r)
})
}
// ------ End Middleware methods -------
func HandleGetProfileList(w http.ResponseWriter, r *http.Request) {
profiles := AuthConfigStore.GetAll("")
HandleAPIOK(profiles, "", 200, w, r)
}
func HandleGetProfile(w http.ResponseWriter, r *http.Request) {
key := mux.Vars(r)["id"]
thisProfile := tap.Profile{}
keyErr := AuthConfigStore.GetKey(key, thisProfile.OrgID, &thisProfile)
if keyErr != nil {
HandleAPIError(APILogTag, "Profile not found", keyErr, 404, w, r)
return
}
HandleAPIOK(thisProfile, key, 200, w, r)
}
func HandleAddProfile(w http.ResponseWriter, r *http.Request) {
key := mux.Vars(r)["id"]
profileData, err := ioutil.ReadAll(r.Body)
if err != nil {
HandleAPIError(APILogTag, "Invalid request data", err, 400, w, r)
return
}
thisProfile := tap.Profile{}
decodeErr := json.Unmarshal(profileData, &thisProfile)
if decodeErr != nil {
HandleAPIError(APILogTag, "Failed to decode body data", decodeErr, 400, w, r)
return
}
if thisProfile.ID != key {
HandleAPIError(APILogTag, "Object ID and URI resource ID do not match", errors.New("ID Mismatch"), 400, w, r)
return
}
httpErr := tap.AddProfile(thisProfile, AuthConfigStore, GlobalDataLoader.Flush)
if httpErr != nil {
HandleAPIError(APILogTag, httpErr.Message, httpErr.Error, httpErr.Code, w, r)
return
}
HandleAPIOK(thisProfile, key, 201, w, r)
}
func HandleUpdateProfile(w http.ResponseWriter, r *http.Request) {
key := mux.Vars(r)["id"]
profileData, err := ioutil.ReadAll(r.Body)
if err != nil {
HandleAPIError(APILogTag, "Invalid request data", err, 400, w, r)
return
}
thisProfile := tap.Profile{}
decodeErr := json.Unmarshal(profileData, &thisProfile)
if decodeErr != nil {
HandleAPIError(APILogTag, "Failed to decode body data", decodeErr, 400, w, r)
return
}
updateErr := tap.UpdateProfile(key, thisProfile, AuthConfigStore, GlobalDataLoader.Flush)
if updateErr != nil {
HandleAPIError(APILogTag, updateErr.Message, updateErr.Error, updateErr.Code, w, r)
return
}
HandleAPIOK(thisProfile, key, 200, w, r)
}
func HandleDeleteProfile(w http.ResponseWriter, r *http.Request) {
key := mux.Vars(r)["id"]
err := tap.DeleteProfile(key, "", AuthConfigStore, GlobalDataLoader.Flush)
if err != nil {
HandleAPIError(APILogTag, err.Message, err.Error, err.Code, w, r)
return
}
data := make(map[string]string)
HandleAPIOK(data, key, 200, w, r)
}