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
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 jResp, err := json.Marshal("error login, email not found")
88 check(err)
89 fmt.Fprintln(w, string(jResp))
90 return
91 }
92 //user exists, check password
93 if user.Password != rUser.Password {
94 jResp, err := json.Marshal("error login, password not match")
95 check(err)
96 fmt.Fprintln(w, string(jResp))
97 return
98 }
99 //update with the token
100 err = userCollection.Update(bson.M{"_id": rUser.Id}, user)
101 check(err)
102
103 jResp, err := json.Marshal(user)
104 if err != nil {
105 panic(err)
106 }
107 fmt.Fprintln(w, string(jResp))
108}
109
110type Sign struct {
111 M string `json:"m"`
112 C string `json:"c"`
113}
114
115type AskBlindSign struct {
116 /*PubKString ownrsa.RSAPublicKeyString `json:"pubKstring"`
117 PubK ownrsa.RSAPublicKey `json:"pubK"`*/
118 M string `json:"m"`
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 color.Red(askBlindSign.M)
130 fmt.Println(askBlindSign)
131
132 /*fmt.Println(askBlindSign)
133 askBlindSign.PubK, err = ownrsa.PubKStringToBigInt(askBlindSign.PubKString)
134 if err != nil {
135 fmt.Fprintln(w, "error")
136 return
137 }*/
138
139 //convert msg to []int
140 /*var m []int
141 mBytes := []byte(askBlindSign.M)
142 for _, byte := range mBytes {
143 m = append(m, int(byte))
144 }*/
145
146 m := ownrsa.StringToArrayInt(askBlindSign.M, "_")
147
148 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
149 fmt.Print("Sigma': ")
150 fmt.Println(sigma)
151 sigmaString := ownrsa.ArrayIntToString(sigma, "_")
152 askBlindSign.M = sigmaString
153
154 jResp, err := json.Marshal(askBlindSign)
155 if err != nil {
156 panic(err)
157 }
158 fmt.Fprintln(w, string(jResp))
159}
160
161type PetitionVerifySign struct {
162 M string `json:"m"`
163 MSigned string `json:"mSigned"`
164}
165
166func VerifySign(w http.ResponseWriter, r *http.Request) {
167 decoder := json.NewDecoder(r.Body)
168 var petitionVerifySign PetitionVerifySign
169 err := decoder.Decode(&petitionVerifySign)
170 if err != nil {
171 panic(err)
172 }
173 defer r.Body.Close()
174
175 fmt.Println(petitionVerifySign)
176
177 //convert M to []int
178 var mOriginal []int
179 mBytes := []byte(petitionVerifySign.M)
180 for _, byte := range mBytes {
181 mOriginal = append(mOriginal, int(byte))
182 }
183
184 //convert MSigned to []int
185 var mSignedInts []int
186 mSignedString := strings.Split(petitionVerifySign.MSigned, " ")
187 for _, s := range mSignedString {
188 i, err := strconv.Atoi(s)
189 check(err)
190 mSignedInts = append(mSignedInts, i)
191 }
192
193 verified := ownrsa.Verify(mOriginal, mSignedInts, serverRSA.PubK)
194
195 fmt.Fprintln(w, verified)
196}