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