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.

115 lines
2.6 KiB

  1. package main
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/sha1"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. mrand "math/rand"
  10. "net/http"
  11. "strings"
  12. "github.com/fatih/color"
  13. "gopkg.in/mgo.v2/bson"
  14. )
  15. type User struct {
  16. Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
  17. Email string `json:"email"`
  18. Password string `json:"password"`
  19. Token string `json:"token"`
  20. }
  21. func Index(w http.ResponseWriter, r *http.Request) {
  22. fmt.Fprintln(w, "clientApp")
  23. }
  24. type Proof struct {
  25. PublicKey string `json:"publicKey"`
  26. Clear string `json:"clear"`
  27. Question []byte `json:"question"`
  28. Answer string `json:"answer"`
  29. }
  30. var proofs []Proof
  31. func GetProof(w http.ResponseWriter, r *http.Request) {
  32. decoder := json.NewDecoder(r.Body)
  33. var receivedProof Proof
  34. err := decoder.Decode(&receivedProof)
  35. if err != nil {
  36. panic(err)
  37. }
  38. defer r.Body.Close()
  39. //TODO check if the user password exists in the database
  40. stringPublicKey := strings.Replace(receivedProof.PublicKey, " ", "\n", -1)
  41. stringPublicKey = strings.Replace(stringPublicKey, "-----BEGIN\n", "-----BEGIN ", -1)
  42. stringPublicKey = strings.Replace(stringPublicKey, "-----END\n", "-----END ", -1)
  43. stringPublicKey = strings.Replace(stringPublicKey, "PUBLIC\n", "PUBLIC ", -1)
  44. color.Green(stringPublicKey)
  45. publicKey, err := ParseRsaPublicKeyFromPemStr(stringPublicKey)
  46. check(err)
  47. var proof Proof
  48. proof.Clear = RandStringRunes(40)
  49. out, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, &publicKey, []byte(proof.Clear), []byte("orders"))
  50. check(err)
  51. proof.Question = out
  52. proofs = append(proofs, proof)
  53. proof.Clear = ""
  54. jResp, err := json.Marshal(proof)
  55. if err != nil {
  56. panic(err)
  57. }
  58. fmt.Fprintln(w, string(jResp))
  59. }
  60. func AnswerProof(w http.ResponseWriter, r *http.Request) {
  61. decoder := json.NewDecoder(r.Body)
  62. var ansProof Proof
  63. err := decoder.Decode(&ansProof)
  64. if err != nil {
  65. panic(err)
  66. }
  67. defer r.Body.Close()
  68. proof, err := getProofFromStorage(ansProof.PublicKey)
  69. if err != nil {
  70. }
  71. if ansProof.Answer == proof.Clear {
  72. token, err := newToken()
  73. check(err)
  74. fmt.Fprintln(w, string(token))
  75. }
  76. fmt.Fprintln(w, string("fail"))
  77. }
  78. func getProofFromStorage(publicKey string) (Proof, error) {
  79. var voidProof Proof
  80. for _, proof := range proofs {
  81. if proof.PublicKey == publicKey {
  82. return proof, nil
  83. }
  84. }
  85. return voidProof, errors.New("proof not exist in storage")
  86. }
  87. //function to generate random string of fixed length
  88. var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  89. func RandStringRunes(n int) string {
  90. b := make([]rune, n)
  91. for i := range b {
  92. b[i] = letterRunes[mrand.Intn(len(letterRunes))]
  93. }
  94. return string(b)
  95. }