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
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 /*PubKString ownrsa.RSAPublicKeyString `json:"pubKstring"`
106 PubK ownrsa.RSAPublicKey `json:"pubK"`*/
107 M string `json:"m"`
108}
109
110func BlindSign(w http.ResponseWriter, r *http.Request) {
111 decoder := json.NewDecoder(r.Body)
112 var askBlindSign AskBlindSign
113 err := decoder.Decode(&askBlindSign)
114 if err != nil {
115 panic(err)
116 }
117 defer r.Body.Close()
118 color.Red(askBlindSign.M)
119 fmt.Println(askBlindSign)
120
121 /*fmt.Println(askBlindSign)
122 askBlindSign.PubK, err = ownrsa.PubKStringToBigInt(askBlindSign.PubKString)
123 if err != nil {
124 fmt.Fprintln(w, "error")
125 return
126 }*/
127
128 //convert msg to []int
129 /*var m []int
130 mBytes := []byte(askBlindSign.M)
131 for _, byte := range mBytes {
132 m = append(m, int(byte))
133 }*/
134
135 m := ownrsa.StringToArrayInt(askBlindSign.M, "_")
136
137 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
138 fmt.Print("Sigma': ")
139 fmt.Println(sigma)
140 sigmaString := ownrsa.ArrayIntToString(sigma, "_")
141 askBlindSign.M = sigmaString
142
143 jResp, err := json.Marshal(askBlindSign)
144 if err != nil {
145 panic(err)
146 }
147 fmt.Fprintln(w, string(jResp))
148}
149
150type PetitionVerifySign struct {
151 M string `json:"m"`
152 MSigned string `json:"mSigned"`
153}
154
155func VerifySign(w http.ResponseWriter, r *http.Request) {
156 decoder := json.NewDecoder(r.Body)
157 var petitionVerifySign PetitionVerifySign
158 err := decoder.Decode(&petitionVerifySign)
159 if err != nil {
160 panic(err)
161 }
162 defer r.Body.Close()
163
164 fmt.Println(petitionVerifySign)
165
166 //convert M to []int
167 var mOriginal []int
168 mBytes := []byte(petitionVerifySign.M)
169 for _, byte := range mBytes {
170 mOriginal = append(mOriginal, int(byte))
171 }
172
173 //convert MSigned to []int
174 var mSignedInts []int
175 mSignedString := strings.Split(petitionVerifySign.MSigned, " ")
176 for _, s := range mSignedString {
177 i, err := strconv.Atoi(s)
178 check(err)
179 mSignedInts = append(mSignedInts, i)
180 }
181
182 verified := ownrsa.Verify(mOriginal, mSignedInts, serverRSA.PubK)
183
184 fmt.Fprintln(w, verified)
185}