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.

196 lines
4.4 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "github.com/fatih/color"
  9. "gopkg.in/mgo.v2/bson"
  10. ownrsa "./ownrsa"
  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. //TODO return the public key, to allow others verifign signed strings by this server
  20. jResp, err := json.Marshal(serverRSA.PubK)
  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. /*PubKString ownrsa.RSAPublicKeyString `json:"pubKstring"`
  99. PubK ownrsa.RSAPublicKey `json:"pubK"`*/
  100. M string `json:"m"`
  101. }
  102. func BlindSign(w http.ResponseWriter, r *http.Request) {
  103. decoder := json.NewDecoder(r.Body)
  104. var askBlindSign AskBlindSign
  105. err := decoder.Decode(&askBlindSign)
  106. if err != nil {
  107. panic(err)
  108. }
  109. defer r.Body.Close()
  110. color.Red(askBlindSign.M)
  111. fmt.Println(askBlindSign)
  112. /*fmt.Println(askBlindSign)
  113. askBlindSign.PubK, err = ownrsa.PubKStringToBigInt(askBlindSign.PubKString)
  114. if err != nil {
  115. fmt.Fprintln(w, "error")
  116. return
  117. }*/
  118. //convert msg to []int
  119. /*var m []int
  120. mBytes := []byte(askBlindSign.M)
  121. for _, byte := range mBytes {
  122. m = append(m, int(byte))
  123. }*/
  124. m := ownrsa.StringToArrayInt(askBlindSign.M, "_")
  125. 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
  126. fmt.Print("Sigma': ")
  127. fmt.Println(sigma)
  128. sigmaString := ownrsa.ArrayIntToString(sigma, "_")
  129. askBlindSign.M = sigmaString
  130. jResp, err := json.Marshal(askBlindSign)
  131. if err != nil {
  132. panic(err)
  133. }
  134. fmt.Fprintln(w, string(jResp))
  135. }
  136. type PetitionVerifySign struct {
  137. M string `json:"m"`
  138. MSigned string `json:"mSigned"`
  139. }
  140. func 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. fmt.Println(petitionVerifySign)
  149. //convert M to []int
  150. var mOriginal []int
  151. mBytes := []byte(petitionVerifySign.M)
  152. for _, byte := range mBytes {
  153. mOriginal = append(mOriginal, int(byte))
  154. }
  155. //convert MSigned to []int
  156. var mSignedInts []int
  157. mSignedString := strings.Split(petitionVerifySign.MSigned, " ")
  158. for _, s := range mSignedString {
  159. i, err := strconv.Atoi(s)
  160. check(err)
  161. mSignedInts = append(mSignedInts, i)
  162. }
  163. verified := ownrsa.Verify(mOriginal, mSignedInts, serverRSA.PubK)
  164. fmt.Fprintln(w, verified)
  165. }