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