darkID-prototype/clientApp/clientAppRESTFunctions.go
clientApp/clientAppRESTFunctions.goRaw
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7
8 "github.com/fatih/color"
9 "github.com/gorilla/mux"
10)
11
12//TODO use rsa library instead own rsa functions
13
14func Index(w http.ResponseWriter, r *http.Request) {
15 fmt.Fprintln(w, "clientApp")
16}
17
18func GetServer(w http.ResponseWriter, r *http.Request) {
19 color.Green(config.Server)
20 fmt.Fprintln(w, config.Server)
21}
22func GetIDs(w http.ResponseWriter, r *http.Request) {
23 keys := IDs()
24
25 jResp, err := json.Marshal(keys)
26 check(err)
27 fmt.Fprintln(w, string(jResp))
28}
29func GetID(w http.ResponseWriter, r *http.Request) {
30 vars := mux.Vars(r)
31 keyID := vars["keyid"]
32 key := getKeyByKeyID(keyID)
33
34 jResp, err := json.Marshal(key)
35 check(err)
36 fmt.Fprintln(w, string(jResp))
37}
38func GetNewID(w http.ResponseWriter, r *http.Request) {
39 key := NewID()
40
41 fmt.Println(key)
42
43 jResp, err := json.Marshal(key)
44 check(err)
45 fmt.Fprintln(w, string(jResp))
46}
47func GetBlindAndSendToSign(w http.ResponseWriter, r *http.Request) {
48 vars := mux.Vars(r)
49 idKey := vars["keyid"]
50 color.Green(idKey)
51
52 keys := BlindAndSendToSign(idKey)
53
54 jResp, err := json.Marshal(keys)
55 check(err)
56 fmt.Fprintln(w, string(jResp))
57}
58
59func GetVerify(w http.ResponseWriter, r *http.Request) {
60 vars := mux.Vars(r)
61 packPubK := vars["pubK"]
62 color.Green(packPubK)
63
64 //keys := Verify(packPubK)
65 keys := "a"
66
67 jResp, err := json.Marshal(keys)
68 check(err)
69 fmt.Fprintln(w, string(jResp))
70}
71
72func GetDelete(w http.ResponseWriter, r *http.Request) {
73 vars := mux.Vars(r)
74 keyID := vars["keyid"]
75
76 keys := Delete(keyID)
77
78 jResp, err := json.Marshal(keys)
79 check(err)
80 fmt.Fprintln(w, string(jResp))
81}
82
83type EncryptData struct {
84 M string `json:"m"`
85 C []byte `json:"c"`
86}
87
88func PostEncrypt(w http.ResponseWriter, r *http.Request) {
89 vars := mux.Vars(r)
90 keyID := vars["keyid"]
91
92 //get ciphertext from POST json
93 decoder := json.NewDecoder(r.Body)
94 var encryptData EncryptData
95 err := decoder.Decode(&encryptData)
96 if err != nil {
97 panic(err)
98 }
99 defer r.Body.Close()
100
101 encryptData = Encrypt(keyID, encryptData)
102
103 jResp, err := json.Marshal(encryptData)
104 check(err)
105 fmt.Fprintln(w, string(jResp))
106}
107func PostDecrypt(w http.ResponseWriter, r *http.Request) {
108 vars := mux.Vars(r)
109 keyID := vars["keyid"]
110
111 //get ciphertext from POST json
112 decoder := json.NewDecoder(r.Body)
113 var encryptData EncryptData
114 err := decoder.Decode(&encryptData)
115 if err != nil {
116 panic(err)
117 }
118 defer r.Body.Close()
119
120 encryptData = Decrypt(keyID, encryptData)
121
122 jResp, err := json.Marshal(encryptData)
123 check(err)
124 fmt.Fprintln(w, string(jResp))
125}