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.

185 lines
4.1 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. fmt.Print("user login: ")
  63. fmt.Println(user)
  64. token, err := newToken()
  65. check(err)
  66. user.Token = token
  67. //save the new project to mongodb
  68. rUser := User{}
  69. err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser)
  70. if err != nil {
  71. } else {
  72. //user exists, update with the token
  73. err = userCollection.Update(bson.M{"_id": rUser.Id}, user)
  74. check(err)
  75. }
  76. jResp, err := json.Marshal(user)
  77. if err != nil {
  78. panic(err)
  79. }
  80. fmt.Fprintln(w, string(jResp))
  81. }
  82. type Sign struct {
  83. M string `json:"m"`
  84. C string `json:"c"`
  85. }
  86. type AskBlindSign struct {
  87. /*PubKString ownrsa.RSAPublicKeyString `json:"pubKstring"`
  88. PubK ownrsa.RSAPublicKey `json:"pubK"`*/
  89. M string `json:"m"`
  90. }
  91. func BlindSign(w http.ResponseWriter, r *http.Request) {
  92. decoder := json.NewDecoder(r.Body)
  93. var askBlindSign AskBlindSign
  94. err := decoder.Decode(&askBlindSign)
  95. if err != nil {
  96. panic(err)
  97. }
  98. defer r.Body.Close()
  99. color.Red(askBlindSign.M)
  100. fmt.Println(askBlindSign)
  101. /*fmt.Println(askBlindSign)
  102. askBlindSign.PubK, err = ownrsa.PubKStringToBigInt(askBlindSign.PubKString)
  103. if err != nil {
  104. fmt.Fprintln(w, "error")
  105. return
  106. }*/
  107. //convert msg to []int
  108. /*var m []int
  109. mBytes := []byte(askBlindSign.M)
  110. for _, byte := range mBytes {
  111. m = append(m, int(byte))
  112. }*/
  113. m := ownrsa.StringToArrayInt(askBlindSign.M, "_")
  114. 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
  115. fmt.Print("Sigma': ")
  116. fmt.Println(sigma)
  117. sigmaString := ownrsa.ArrayIntToString(sigma, "_")
  118. askBlindSign.M = sigmaString
  119. jResp, err := json.Marshal(askBlindSign)
  120. if err != nil {
  121. panic(err)
  122. }
  123. fmt.Fprintln(w, string(jResp))
  124. }
  125. type PetitionVerifySign struct {
  126. M string `json:"m"`
  127. MSigned string `json:"mSigned"`
  128. }
  129. func VerifySign(w http.ResponseWriter, r *http.Request) {
  130. decoder := json.NewDecoder(r.Body)
  131. var petitionVerifySign PetitionVerifySign
  132. err := decoder.Decode(&petitionVerifySign)
  133. if err != nil {
  134. panic(err)
  135. }
  136. defer r.Body.Close()
  137. fmt.Println(petitionVerifySign)
  138. //convert M to []int
  139. var mOriginal []int
  140. mBytes := []byte(petitionVerifySign.M)
  141. for _, byte := range mBytes {
  142. mOriginal = append(mOriginal, int(byte))
  143. }
  144. //convert MSigned to []int
  145. var mSignedInts []int
  146. mSignedString := strings.Split(petitionVerifySign.MSigned, " ")
  147. for _, s := range mSignedString {
  148. i, err := strconv.Atoi(s)
  149. check(err)
  150. mSignedInts = append(mSignedInts, i)
  151. }
  152. verified := ownrsa.Verify(mOriginal, mSignedInts, serverRSA.PubK)
  153. fmt.Fprintln(w, verified)
  154. }