-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
371 lines (303 loc) · 9.35 KB
/
main.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/golang-jwt/jwt"
"github.com/gorilla/mux"
"golang.org/x/crypto/bcrypt"
)
//Structs for user, merchant & transaction
type User struct {
Id int `json:"id"`
Name string `json:"name"`
Password string `json:"password"`
Balance int `json:"balance"`
}
type Merchant struct {
Id int `json:"id"`
Name string `json:"name"`
Balance int `json:"balance"`
}
type Transaction struct {
Id int `json:"id"`
From int `json:"from"`
To int `json:"to"`
Amount int `json:"amount"`
CreatedAt string `json:"created_at"`
}
type Page struct {
Title string
}
// Secret token to encrypt/decrypt JWT tokens
var jwtTokenSecret = "abc123456def"
// Initialization
var Users []User
var Merchants []Merchant
var Transactions []Transaction
var tr_count = 2 // Keep track of transaction count, will be incremented for transaction IDs
// This function return all users in json format
func returnUsers(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnUsers") // For logging purposes
json.NewEncoder(w).Encode(Users)
}
// This function a specific user with id as a condition
func returnUser(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
key := vars["id"]
var uid int
if i, err := strconv.Atoi(key); err == nil {
uid = i
}
for _, user := range Users {
if user.Id == uid {
json.NewEncoder(w).Encode(user)
}
}
}
// This function return all merchants in json format
func returnMerchants(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnMerchants")
json.NewEncoder(w).Encode(Merchants)
}
// This function return all transactions in json format
func returnTransactions(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnTransactions")
json.NewEncoder(w).Encode(Transactions)
}
// This display the login template as well as the login logic itself
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: Login Page")
conditionsMap := map[string]interface{}{}
// Check if user already logged in
username, _ := ExtractTokenUsername(r)
if username != "" { // User already logged in
conditionsMap["Username"] = username
conditionsMap["LoginError"] = false
http.Redirect(w, r, "/payment", http.StatusSeeOther)
}
// Verify username and password from POST value
if r.FormValue("username") != "" && r.FormValue("password") != "" {
username := r.FormValue("username")
password := r.FormValue("password")
var pwd string
// Check if username and password match from data
for _, user := range Users {
if user.Name == username {
pwd = user.Password
}
}
hashedPassword := []byte(pwd)
// Use the bcrypt library to compare hash and password
if err := bcrypt.CompareHashAndPassword(hashedPassword, []byte(password)); err != nil {
log.Println("Wrong credentials.")
conditionsMap["LoginError"] = true
} else {
log.Println("Logged in :", username)
conditionsMap["Username"] = username
conditionsMap["LoginError"] = false
// Create a new JSON Web Token
tokenString, err := createToken(username)
if err != nil {
log.Println(err) // Quick handle
os.Exit(1)
}
// Create the cookie for client - browser
expirationTime := time.Now().Add(1 * time.Hour) // Set cookie expired after 1 hour
cookie := &http.Cookie{
Name: "token",
Value: tokenString,
Expires: expirationTime,
}
http.SetCookie(w, cookie)
http.Redirect(w, r, "/payment", http.StatusFound) // Redirect to payment page if login successful
}
}
if err := templ.ExecuteTemplate(w, "login", &Page{Title: "Login"}); err != nil { // Stay at login page if login failed
log.Println(err)
}
}
func pay(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: Pay Logic")
// Check if user already logged in
username, _ := ExtractTokenUsername(r)
if username == "" { // User already logged in
w.Write([]byte("Please login first."))
return
}
var mid int
var amt int
var uid int
// Payment logic
if r.FormValue("merchant") != "" && r.FormValue("amount") != "" {
// Convert values to int
merchantID := r.FormValue("merchant")
if i, err := strconv.Atoi(merchantID); err == nil {
mid = i
}
amount := r.FormValue("amount")
if i, err := strconv.Atoi(amount); err == nil {
amt = i
}
// Decrease balance for designated user according to the amount input
for i, user := range Users {
if user.Name == username {
if user.Balance < amt {
fmt.Fprintf(w, "Insufficient balance!")
return
}
uid = user.Id
user.Balance = user.Balance - amt
Users[i] = user
}
}
// Increase balance for designated user according to the amount input
for i, merchant := range Merchants {
if merchant.Id == mid {
merchant.Balance = merchant.Balance + amt
Merchants[i] = merchant
}
}
// Insert new transaction
transaction := Transaction{Id: tr_count, From: uid, To: mid, Amount: amt, CreatedAt: time.Now().Format(time.RFC3339)}
Transactions = append(Transactions, transaction)
tr_count++ // Increase tr_count for insert (id_ purposes
json.NewEncoder(w).Encode(transaction) // Returns the payment detail
}
}
// Remove the "token" cookie for logout
func logout(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: Logout")
c := http.Cookie{
Name: "token",
MaxAge: -1}
http.SetCookie(w, &c)
w.Write([]byte("Cookie deleted. Logged out!\n"))
}
// Display the payment template
func payment(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: Payment Page")
conditionsMap := map[string]interface{}{}
// Check if user already logged in
username, _ := ExtractTokenUsername(r)
if username != "" {
conditionsMap["Username"] = username
} else {
http.Redirect(w, r, "/login", http.StatusFound) // Redirect to login page if not yet logged in
}
// Execute payment template for logged in user
if err := templ.ExecuteTemplate(w, "payment", conditionsMap); err != nil {
log.Println(err)
}
}
// Setup code for executing template
var templ = func() *template.Template {
t := template.New("")
err := filepath.Walk("./template/", func(path string, info os.FileInfo, err error) error {
if strings.Contains(path, ".html") {
fmt.Println(path)
_, err = t.ParseFiles(path)
if err != nil {
fmt.Println(err)
}
}
return err
})
if err != nil {
panic(err)
}
return t
}()
func createToken(username string) (string, error) {
claims := jwt.MapClaims{}
claims["authorized"] = true
claims["username"] = username // Embed username inside the token string
claims["expired"] = time.Now().Add(time.Hour * 1).Unix() // Token expires after 1 hour
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(jwtTokenSecret))
}
func generateToken(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: Generate Token API")
vars := mux.Vars(r)
username := vars["username"]
tokenString, err := createToken(username)
if err != nil {
log.Println(err) // Quick handle
os.Exit(1)
}
json.NewEncoder(w).Encode(tokenString)
}
func ExtractTokenUsername(r *http.Request) (string, error) {
// Get our token string from Cookie
biscuit, err := r.Cookie("token")
var tokenString string
if err != nil {
tokenString = ""
} else {
tokenString = biscuit.Value
}
// Abort
if tokenString == "" {
return "", nil
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(jwtTokenSecret), nil
})
if err != nil {
return "", err
}
claims, ok := token.Claims.(jwt.MapClaims)
if ok && token.Valid {
username := fmt.Sprintf("%s", claims["username"]) // Convert to string
if err != nil {
return "", err
}
return username, nil
}
return "", nil
}
// Routes with Mux
func handleRequests() {
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/", payment)
myRouter.HandleFunc("/login", login)
myRouter.HandleFunc("/logout", logout)
myRouter.HandleFunc("/payment", payment)
myRouter.HandleFunc("/pay", pay).Methods("POST")
myRouter.HandleFunc("/users", returnUsers)
myRouter.HandleFunc("/merchants", returnMerchants)
myRouter.HandleFunc("/transactions", returnTransactions)
myRouter.HandleFunc("/user/{id}", returnUser)
myRouter.HandleFunc("/token", generateToken).Methods("POST")
log.Fatal(http.ListenAndServe(":10000", myRouter))
}
func main() {
fmt.Println("Payments API in Go with Mux Routers")
// Users initial data, Password: pass123
Users = []User{
User{Id: 1, Name: "kervin", Password: "$2b$10$knfJ.4rmOOdY5vDcOQZYeOPOsjO2Mwpl5KnPwN8j.4RIY5hekklSO", Balance: 100},
User{Id: 2, Name: "admin", Password: "$2b$10$knfJ.4rmOOdY5vDcOQZYeOPOsjO2Mwpl5KnPwN8j.4RIY5hekklSO", Balance: 5000},
}
// Merchants initial data
Merchants = []Merchant{
Merchant{Id: 1, Name: "Merchant A", Balance: 1000},
Merchant{Id: 2, Name: "Merchant B", Balance: 500},
Merchant{Id: 3, Name: "Merchant C", Balance: 200},
}
// Transactions initial data
Transactions = []Transaction{
Transaction{Id: 1, From: 1, To: 1, Amount: 1000, CreatedAt: "2021-10-21 00:00:00"},
}
handleRequests()
}