-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (103 loc) · 2.36 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
var (
token_map = make(map[string]string)
)
var creds_map = map[string]string{
"user1": "user1",
"user2": "user2",
}
var role_map = map[string]string{
"user1": "1",
"user2": "2",
"user0": "0",
}
type input_struct struct {
Username string `json:"username"`
Password string `json:"password"`
}
type token_struct struct {
Headers Hd `json:"headers"`
}
type Hd struct {
Token string `json:"token"`
}
func createToken(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
panic(err)
}
log.Println(string(body))
var info input_struct
err = json.Unmarshal(body, &info)
if err != nil {
panic(err)
}
token := uuid.New().String()
username := info.Username
pwd := info.Password
if creds_map[username] != pwd {
log.Fatalln("ERROR CREDENTIALS")
}
token_map[token] = string(username)
log.Println(token)
output_map := map[string]string{
"token": token,
}
token_dict, _ := json.Marshal(output_map)
output_body := fmt.Sprintf("\n%s\n", string(token_dict))
w.Write([]byte(output_body))
}
func getHasuraVariables(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
log.Println(string(body))
token_temp := &token_struct{}
err = json.Unmarshal([]byte(body), token_temp)
if err != nil {
log.Fatalln(err)
}
token_val := token_temp.Headers.Token
log.Println(string(token_val))
username, ok := token_map[token_val]
if !ok {
username = "user0"
}
dict := map[string]string{
"X-Hasura-Role": username,
"X-Hasura-User-Id": role_map[username],
}
json_object, err := json.Marshal(dict)
if err != nil {
log.Fatalln(err)
}
token_map_dict, _ := json.Marshal(token_map)
fmt.Fprintf(w, string(json_object))
log.Println(string(json_object))
log.Println(string(token_map_dict))
}
func main() {
router := mux.NewRouter()
srv := &http.Server{
Handler: router,
Addr: ":" + "8000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
router.HandleFunc("/v1/login", createToken).Methods("POST")
router.HandleFunc("/v1/verify", getHasuraVariables).Methods("POST")
log.Println("Server started and listening on http://127.0.0.1:8000")
log.Fatal(srv.ListenAndServe())
}