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.

128 lines
2.5 KiB

  1. package server
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/gorilla/mux"
  6. keys "github.com/tendermint/go-crypto/keys"
  7. "github.com/tendermint/go-crypto/keys/server/types"
  8. )
  9. type Keys struct {
  10. manager keys.Manager
  11. algo string
  12. }
  13. func New(manager keys.Manager, algo string) Keys {
  14. return Keys{
  15. manager: manager,
  16. algo: algo,
  17. }
  18. }
  19. func (k Keys) GenerateKey(w http.ResponseWriter, r *http.Request) {
  20. req := types.CreateKeyRequest{
  21. Algo: k.algo, // default key type from cli
  22. }
  23. err := readRequest(r, &req)
  24. if err != nil {
  25. writeError(w, err)
  26. return
  27. }
  28. key, seed, err := k.manager.Create(req.Name, req.Passphrase, req.Algo)
  29. if err != nil {
  30. writeError(w, err)
  31. return
  32. }
  33. res := types.CreateKeyResponse{key, seed}
  34. writeSuccess(w, &res)
  35. }
  36. func (k Keys) GetKey(w http.ResponseWriter, r *http.Request) {
  37. vars := mux.Vars(r)
  38. name := vars["name"]
  39. key, err := k.manager.Get(name)
  40. if err != nil {
  41. writeError(w, err)
  42. return
  43. }
  44. writeSuccess(w, &key)
  45. }
  46. func (k Keys) ListKeys(w http.ResponseWriter, r *http.Request) {
  47. keys, err := k.manager.List()
  48. if err != nil {
  49. writeError(w, err)
  50. return
  51. }
  52. writeSuccess(w, keys)
  53. }
  54. func (k Keys) UpdateKey(w http.ResponseWriter, r *http.Request) {
  55. req := types.UpdateKeyRequest{}
  56. err := readRequest(r, &req)
  57. if err != nil {
  58. writeError(w, err)
  59. return
  60. }
  61. vars := mux.Vars(r)
  62. name := vars["name"]
  63. if name != req.Name {
  64. writeError(w, errors.New("path and json key names don't match"))
  65. return
  66. }
  67. err = k.manager.Update(req.Name, req.OldPass, req.NewPass)
  68. if err != nil {
  69. writeError(w, err)
  70. return
  71. }
  72. key, err := k.manager.Get(req.Name)
  73. if err != nil {
  74. writeError(w, err)
  75. return
  76. }
  77. writeSuccess(w, &key)
  78. }
  79. func (k Keys) DeleteKey(w http.ResponseWriter, r *http.Request) {
  80. req := types.DeleteKeyRequest{}
  81. err := readRequest(r, &req)
  82. if err != nil {
  83. writeError(w, err)
  84. return
  85. }
  86. vars := mux.Vars(r)
  87. name := vars["name"]
  88. if name != req.Name {
  89. writeError(w, errors.New("path and json key names don't match"))
  90. return
  91. }
  92. err = k.manager.Delete(req.Name, req.Passphrase)
  93. if err != nil {
  94. writeError(w, err)
  95. return
  96. }
  97. // not really an error, but something generic
  98. resp := types.ErrorResponse{
  99. Success: true,
  100. }
  101. writeSuccess(w, &resp)
  102. }
  103. func (k Keys) Register(r *mux.Router) {
  104. r.HandleFunc("/", k.GenerateKey).Methods("POST")
  105. r.HandleFunc("/", k.ListKeys).Methods("GET")
  106. r.HandleFunc("/{name}", k.GetKey).Methods("GET")
  107. r.HandleFunc("/{name}", k.UpdateKey).Methods("POST", "PUT")
  108. r.HandleFunc("/{name}", k.DeleteKey).Methods("DELETE")
  109. }