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