forked from NXRSE/bank
-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpApiHandlers.go
271 lines (226 loc) · 7 KB
/
httpApiHandlers.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"errors"
"fmt"
"net/http"
"github.com/bvnk/bank/accounts"
"github.com/bvnk/bank/appauth"
"github.com/bvnk/bank/transactions"
"github.com/gorilla/mux"
)
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "API Index")
}
func getTokenFromHeader(w http.ResponseWriter, r *http.Request) (token string, err error) {
// Get token from header
token = r.Header.Get("X-Auth-Token")
if token == "" {
return "", errors.New("httpApiHandlers: Could not retrieve token from headers")
}
// Check token
err = appauth.CheckToken(token)
if err != nil {
return "", errors.New("httpApiHandlers: Token invalid")
}
return
}
// Extend token
func AuthIndex(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
//Extend token
response, err := appauth.ProcessAppAuth([]string{token, "appauth", "1"})
fmt.Println(response)
fmt.Println(err)
Response(response, err, w, r)
return
}
// Get token
func AuthLogin(w http.ResponseWriter, r *http.Request) {
fmt.Println("Get token")
user := r.FormValue("User")
password := r.FormValue("Password")
response, err := appauth.ProcessAppAuth([]string{"0", "appauth", "2", user, password})
Response(response, err, w, r)
return
}
// Create auth account
func AuthCreate(w http.ResponseWriter, r *http.Request) {
user := r.FormValue("User")
password := r.FormValue("Password")
response, err := appauth.ProcessAppAuth([]string{"0", "appauth", "3", user, password})
Response(response, err, w, r)
return
}
// Remove auth account
func AuthRemove(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
user := r.FormValue("User")
password := r.FormValue("Password")
response, err := appauth.ProcessAppAuth([]string{token, "appauth", "4", user, password})
Response(response, err, w, r)
return
}
func AccountIndex(w http.ResponseWriter, r *http.Request) {
fmt.Println("Account Index")
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
response, err := accounts.ProcessAccount([]string{token, "acmt", "1001"})
Response(response, err, w, r)
return
}
func AccountCreate(w http.ResponseWriter, r *http.Request) {
// Get values from POST
accountHolderGivenName := r.FormValue("AccountHolderGivenName")
accountHolderFamilyName := r.FormValue("AccountHolderFamilyName")
accountHolderDateOfBirth := r.FormValue("AccountHolderDateOfBirth")
accountHolderIdentificationNumber := r.FormValue("AccountHolderIdentificationNumber")
accountHolderContactNumber1 := r.FormValue("AccountHolderContactNumber1")
accountHolderContactNumber2 := r.FormValue("AccountHolderContactNumber2")
accountHolderEmailAddress := r.FormValue("AccountHolderEmailAddress")
accountHolderAddressLine1 := r.FormValue("AccountHolderAddressLine1")
accountHolderAddressLine2 := r.FormValue("AccountHolderAddressLine2")
accountHolderAddressLine3 := r.FormValue("AccountHolderAddressLine3")
accountHolderPostalCode := r.FormValue("AccountHolderPostalCode")
req := []string{
"0",
"acmt",
"1",
accountHolderGivenName,
accountHolderFamilyName,
accountHolderDateOfBirth,
accountHolderIdentificationNumber,
accountHolderContactNumber1,
accountHolderContactNumber2,
accountHolderEmailAddress,
accountHolderAddressLine1,
accountHolderAddressLine2,
accountHolderAddressLine3,
accountHolderPostalCode,
}
response, err := accounts.ProcessAccount(req)
Response(response, err, w, r)
return
}
func AccountGet(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
vars := mux.Vars(r)
accountId := vars["accountId"]
response, err := accounts.ProcessAccount([]string{token, "acmt", "1002", accountId})
Response(response, err, w, r)
return
}
func AccountRetrieve(w http.ResponseWriter, r *http.Request) {
// Set these in the header as they are sensitive
ID := r.Header.Get("X-IDNumber")
givenName := r.Header.Get("X-GivenName")
familyName := r.Header.Get("X-FamilyName")
email := r.Header.Get("X-EmailAddress")
response, err := accounts.ProcessAccount([]string{"", "acmt", "1006", ID, givenName, familyName, email})
Response(response, err, w, r)
return
}
func AccountGetAll(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
response, err := accounts.ProcessAccount([]string{token, "acmt", "1000"})
Response(response, err, w, r)
return
}
func AccountTokenPost(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
pushToken := r.FormValue("PushToken")
platform := r.FormValue("Platform")
response, err := accounts.ProcessAccount([]string{token, "acmt", "1003", pushToken, platform})
Response(response, err, w, r)
return
}
func AccountTokenDelete(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
pushToken := r.FormValue("PushToken")
platform := r.FormValue("Platform")
response, err := accounts.ProcessAccount([]string{token, "acmt", "1004", pushToken, platform})
Response(response, err, w, r)
return
}
func AccountSearch(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
searchTerm := r.FormValue("Search")
response, err := accounts.ProcessAccount([]string{token, "acmt", "1005", searchTerm})
Response(response, err, w, r)
return
}
func TransactionCreditInitiation(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
senderDetails := r.FormValue("SenderDetails")
recipientDetails := r.FormValue("RecipientDetails")
amount := r.FormValue("Amount")
lat := r.FormValue("Lat")
lon := r.FormValue("Lon")
desc := r.FormValue("Desc")
response, err := transactions.ProcessPAIN([]string{token, "pain", "1", senderDetails, recipientDetails, amount, lat, lon, desc})
Response(response, err, w, r)
return
}
func TransactionDepositInitiation(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
accountDetails := r.FormValue("AccountDetails")
amount := r.FormValue("Amount")
lat := r.FormValue("Lat")
lon := r.FormValue("Lon")
desc := r.FormValue("Desc")
response, err := transactions.ProcessPAIN([]string{token, "pain", "1000", accountDetails, amount, lat, lon, desc})
Response(response, err, w, r)
return
}
func TransactionList(w http.ResponseWriter, r *http.Request) {
token, err := getTokenFromHeader(w, r)
if err != nil {
Response("", err, w, r)
return
}
vars := mux.Vars(r)
perPage := vars["perPage"]
page := vars["page"]
timestamp := vars["timestamp"]
response, err := transactions.ProcessPAIN([]string{token, "pain", "1001", page, perPage, timestamp})
Response(response, err, w, r)
return
}