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