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.

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