You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package main
  2. import (
  3. "crypto/rsa"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "github.com/cryptoballot/rsablind"
  10. "gopkg.in/mgo.v2/bson"
  11. )
  12. type User struct {
  13. Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
  14. Email string `json:"email"`
  15. Password string `json:"password"`
  16. Token string `json:"token"`
  17. }
  18. func Index(w http.ResponseWriter, r *http.Request) {
  19. // return server public key, to allow others verifign signed strings by this server
  20. jResp, err := json.Marshal(serverKey.PublicKey)
  21. if err != nil {
  22. panic(err)
  23. }
  24. fmt.Fprintln(w, string(jResp))
  25. }
  26. func Signup(w http.ResponseWriter, r *http.Request) {
  27. decoder := json.NewDecoder(r.Body)
  28. var user User
  29. err := decoder.Decode(&user)
  30. if err != nil {
  31. panic(err)
  32. }
  33. defer r.Body.Close()
  34. fmt.Print("user signup: ")
  35. fmt.Println(user)
  36. //save the new project to mongodb
  37. rUser := User{}
  38. err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser)
  39. if err != nil {
  40. //user not exists
  41. 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
  42. err = userCollection.Find(bson.M{"email": user.Email}).One(&user)
  43. } else {
  44. //user exists
  45. fmt.Fprintln(w, "User already registered")
  46. return
  47. }
  48. jResp, err := json.Marshal(user)
  49. if err != nil {
  50. panic(err)
  51. }
  52. fmt.Fprintln(w, string(jResp))
  53. }
  54. func Login(w http.ResponseWriter, r *http.Request) {
  55. decoder := json.NewDecoder(r.Body)
  56. var user User
  57. err := decoder.Decode(&user)
  58. if err != nil {
  59. panic(err)
  60. }
  61. defer r.Body.Close()
  62. //TODO check if the user password exists in the database
  63. fmt.Print("user login: ")
  64. fmt.Println(user)
  65. token, err := newToken()
  66. check(err)
  67. user.Token = token
  68. //save the new project to mongodb
  69. rUser := User{}
  70. err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser)
  71. if err != nil {
  72. jResp, err := json.Marshal("error login, email not found")
  73. check(err)
  74. fmt.Fprintln(w, string(jResp))
  75. return
  76. }
  77. //user exists, check password
  78. if user.Password != rUser.Password {
  79. jResp, err := json.Marshal("error login, password not match")
  80. check(err)
  81. fmt.Fprintln(w, string(jResp))
  82. return
  83. }
  84. //update with the token
  85. err = userCollection.Update(bson.M{"_id": rUser.Id}, user)
  86. check(err)
  87. jResp, err := json.Marshal(user)
  88. if err != nil {
  89. panic(err)
  90. }
  91. fmt.Fprintln(w, string(jResp))
  92. }
  93. type Sign struct {
  94. M string `json:"m"`
  95. C string `json:"c"`
  96. }
  97. type AskBlindSign struct {
  98. M []byte `json:"m"`
  99. }
  100. type SignResponse struct {
  101. Sig []byte `json:"sig"`
  102. PubK rsa.PublicKey `json:"pubK"`
  103. }
  104. func BlindSign(w http.ResponseWriter, r *http.Request) {
  105. decoder := json.NewDecoder(r.Body)
  106. var askBlindSign AskBlindSign
  107. err := decoder.Decode(&askBlindSign)
  108. if err != nil {
  109. panic(err)
  110. }
  111. defer r.Body.Close()
  112. fmt.Println(askBlindSign)
  113. blinded := askBlindSign.M
  114. /*privK := openPEMKey(keysDir + "/server_private.pem")
  115. pubK := openPublicPEMKey(keysDir + "/server_public.pem")*/
  116. sig, err := rsablind.BlindSign(serverKey, blinded)
  117. check(err)
  118. var signResponse SignResponse
  119. signResponse.Sig = sig
  120. signResponse.PubK = serverKey.PublicKey
  121. jResp, err := json.Marshal(signResponse)
  122. if err != nil {
  123. panic(err)
  124. }
  125. fmt.Fprintln(w, string(jResp))
  126. }
  127. //TODO verifysign will not be necessary in this server
  128. type PetitionVerifySign struct {
  129. M string `json:"m"`
  130. MSigned string `json:"mSigned"`
  131. }
  132. func VerifySign(w http.ResponseWriter, r *http.Request) {
  133. decoder := json.NewDecoder(r.Body)
  134. var petitionVerifySign PetitionVerifySign
  135. err := decoder.Decode(&petitionVerifySign)
  136. if err != nil {
  137. panic(err)
  138. }
  139. defer r.Body.Close()
  140. fmt.Println(petitionVerifySign)
  141. //convert M to []int
  142. var mOriginal []int
  143. mBytes := []byte(petitionVerifySign.M)
  144. for _, byte := range mBytes {
  145. mOriginal = append(mOriginal, int(byte))
  146. }
  147. //convert MSigned to []int
  148. var mSignedInts []int
  149. mSignedString := strings.Split(petitionVerifySign.MSigned, " ")
  150. for _, s := range mSignedString {
  151. i, err := strconv.Atoi(s)
  152. check(err)
  153. mSignedInts = append(mSignedInts, i)
  154. }
  155. //verified := ownrsa.Verify(mOriginal, mSignedInts, serverRSA.PubK)
  156. verified := false
  157. fmt.Fprintln(w, verified)
  158. }