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.

173 lines
3.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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. } else {
  73. //user exists, update with the token
  74. err = userCollection.Update(bson.M{"_id": rUser.Id}, user)
  75. check(err)
  76. }
  77. jResp, err := json.Marshal(user)
  78. if err != nil {
  79. panic(err)
  80. }
  81. fmt.Fprintln(w, string(jResp))
  82. }
  83. type Sign struct {
  84. M string `json:"m"`
  85. C string `json:"c"`
  86. }
  87. type AskBlindSign struct {
  88. M []byte `json:"m"`
  89. }
  90. type SignResponse struct {
  91. Sig []byte `json:"sig"`
  92. PubK rsa.PublicKey `json:"pubK"`
  93. }
  94. func BlindSign(w http.ResponseWriter, r *http.Request) {
  95. decoder := json.NewDecoder(r.Body)
  96. var askBlindSign AskBlindSign
  97. err := decoder.Decode(&askBlindSign)
  98. if err != nil {
  99. panic(err)
  100. }
  101. defer r.Body.Close()
  102. fmt.Println(askBlindSign)
  103. blinded := askBlindSign.M
  104. /*privK := openPEMKey(keysDir + "/server_private.pem")
  105. pubK := openPublicPEMKey(keysDir + "/server_public.pem")*/
  106. sig, err := rsablind.BlindSign(serverKey, blinded)
  107. check(err)
  108. var signResponse SignResponse
  109. signResponse.Sig = sig
  110. signResponse.PubK = serverKey.PublicKey
  111. jResp, err := json.Marshal(signResponse)
  112. if err != nil {
  113. panic(err)
  114. }
  115. fmt.Fprintln(w, string(jResp))
  116. }
  117. //TODO verifysign will not be necessary in this server
  118. type PetitionVerifySign struct {
  119. M string `json:"m"`
  120. MSigned string `json:"mSigned"`
  121. }
  122. func VerifySign(w http.ResponseWriter, r *http.Request) {
  123. decoder := json.NewDecoder(r.Body)
  124. var petitionVerifySign PetitionVerifySign
  125. err := decoder.Decode(&petitionVerifySign)
  126. if err != nil {
  127. panic(err)
  128. }
  129. defer r.Body.Close()
  130. fmt.Println(petitionVerifySign)
  131. //convert M to []int
  132. var mOriginal []int
  133. mBytes := []byte(petitionVerifySign.M)
  134. for _, byte := range mBytes {
  135. mOriginal = append(mOriginal, int(byte))
  136. }
  137. //convert MSigned to []int
  138. var mSignedInts []int
  139. mSignedString := strings.Split(petitionVerifySign.MSigned, " ")
  140. for _, s := range mSignedString {
  141. i, err := strconv.Atoi(s)
  142. check(err)
  143. mSignedInts = append(mSignedInts, i)
  144. }
  145. //verified := ownrsa.Verify(mOriginal, mSignedInts, serverRSA.PubK)
  146. verified := false
  147. fmt.Fprintln(w, verified)
  148. }