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