arnaucube
arnaucube/darkID-prototypePublic Self-hosted repository
darkID-prototype/serverIDsigner/userRESTFunctions.go
serverIDsigner/userRESTFunctions.goRaw
  1package main
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"net/http"
  7	"strconv"
  8	"strings"
  9
 10	"github.com/fatih/color"
 11	"gopkg.in/mgo.v2/bson"
 12
 13	ownrsa "./ownrsa"
 14)
 15
 16type User struct {
 17	Id       bson.ObjectId `json:"id" bson:"_id,omitempty"`
 18	Email    string        `json:"email"`
 19	Password string        `json:"password"`
 20	Token    string        `json:"token"`
 21}
 22
 23func Index(w http.ResponseWriter, r *http.Request) {
 24	//TODO return the public key, to allow others verifign signed strings by this server
 25
 26	jResp, err := json.Marshal(serverRSA.PubK)
 27	if err != nil {
 28		panic(err)
 29	}
 30	fmt.Fprintln(w, string(jResp))
 31}
 32
 33func Signup(w http.ResponseWriter, r *http.Request) {
 34
 35	decoder := json.NewDecoder(r.Body)
 36	var user User
 37	err := decoder.Decode(&user)
 38	if err != nil {
 39		panic(err)
 40	}
 41	defer r.Body.Close()
 42
 43	fmt.Print("user signup: ")
 44	fmt.Println(user)
 45
 46	//save the new project to mongodb
 47	rUser := User{}
 48	err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser)
 49	if err != nil {
 50		//user not exists
 51		err = userCollection.Insert(user) //TODO find a way to get the object result when inserting in one line, without need of the two mgo petitions
 52		err = userCollection.Find(bson.M{"email": user.Email}).One(&user)
 53	} else {
 54		//user exists
 55		fmt.Fprintln(w, "User already registered")
 56		return
 57	}
 58
 59	jResp, err := json.Marshal(user)
 60	if err != nil {
 61		panic(err)
 62	}
 63	fmt.Fprintln(w, string(jResp))
 64}
 65
 66func Login(w http.ResponseWriter, r *http.Request) {
 67
 68	decoder := json.NewDecoder(r.Body)
 69	var user User
 70	err := decoder.Decode(&user)
 71	if err != nil {
 72		panic(err)
 73	}
 74	defer r.Body.Close()
 75	//TODO check if the user password exists in the database
 76
 77	fmt.Print("user login: ")
 78	fmt.Println(user)
 79	token, err := newToken()
 80	check(err)
 81	user.Token = token
 82
 83	//save the new project to mongodb
 84	rUser := User{}
 85	err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser)
 86	if err != nil {
 87	} else {
 88		//user exists, update with the token
 89		err = userCollection.Update(bson.M{"_id": rUser.Id}, user)
 90		check(err)
 91	}
 92
 93	jResp, err := json.Marshal(user)
 94	if err != nil {
 95		panic(err)
 96	}
 97	fmt.Fprintln(w, string(jResp))
 98}
 99
100type Sign struct {
101	M string `json:"m"`
102	C string `json:"c"`
103}
104
105type AskBlindSign struct {
106	/*PubKString ownrsa.RSAPublicKeyString `json:"pubKstring"`
107	PubK       ownrsa.RSAPublicKey       `json:"pubK"`*/
108	M string `json:"m"`
109}
110
111func BlindSign(w http.ResponseWriter, r *http.Request) {
112	decoder := json.NewDecoder(r.Body)
113	var askBlindSign AskBlindSign
114	err := decoder.Decode(&askBlindSign)
115	if err != nil {
116		panic(err)
117	}
118	defer r.Body.Close()
119	color.Red(askBlindSign.M)
120	fmt.Println(askBlindSign)
121
122	/*fmt.Println(askBlindSign)
123	askBlindSign.PubK, err = ownrsa.PubKStringToBigInt(askBlindSign.PubKString)
124	if err != nil {
125		fmt.Fprintln(w, "error")
126		return
127	}*/
128
129	//convert msg to []int
130	/*var m []int
131	mBytes := []byte(askBlindSign.M)
132	for _, byte := range mBytes {
133		m = append(m, int(byte))
134	}*/
135
136	m := ownrsa.StringToArrayInt(askBlindSign.M, "_")
137
138	sigma := ownrsa.BlindSign(m, serverRSA.PrivK) //here the privK will be the CA privK, not the m emmiter's one. The pubK is the user's one
139	fmt.Print("Sigma': ")
140	fmt.Println(sigma)
141	sigmaString := ownrsa.ArrayIntToString(sigma, "_")
142	askBlindSign.M = sigmaString
143
144	jResp, err := json.Marshal(askBlindSign)
145	if err != nil {
146		panic(err)
147	}
148	fmt.Fprintln(w, string(jResp))
149}
150
151type PetitionVerifySign struct {
152	M       string `json:"m"`
153	MSigned string `json:"mSigned"`
154}
155
156func VerifySign(w http.ResponseWriter, r *http.Request) {
157	decoder := json.NewDecoder(r.Body)
158	var petitionVerifySign PetitionVerifySign
159	err := decoder.Decode(&petitionVerifySign)
160	if err != nil {
161		panic(err)
162	}
163	defer r.Body.Close()
164
165	fmt.Println(petitionVerifySign)
166
167	//convert M to []int
168	var mOriginal []int
169	mBytes := []byte(petitionVerifySign.M)
170	for _, byte := range mBytes {
171		mOriginal = append(mOriginal, int(byte))
172	}
173
174	//convert MSigned to []int
175	var mSignedInts []int
176	mSignedString := strings.Split(petitionVerifySign.MSigned, " ")
177	for _, s := range mSignedString {
178		i, err := strconv.Atoi(s)
179		check(err)
180		mSignedInts = append(mSignedInts, i)
181	}
182
183	verified := ownrsa.Verify(mOriginal, mSignedInts, serverRSA.PubK)
184
185	fmt.Fprintln(w, verified)
186}