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.

127 lines
2.4 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, err := k.manager.Create(req.Name, req.Passphrase, req.Algo)
  29. if err != nil {
  30. writeError(w, err)
  31. return
  32. }
  33. writeSuccess(w, &key)
  34. }
  35. func (k Keys) GetKey(w http.ResponseWriter, r *http.Request) {
  36. vars := mux.Vars(r)
  37. name := vars["name"]
  38. key, err := k.manager.Get(name)
  39. if err != nil {
  40. writeError(w, err)
  41. return
  42. }
  43. writeSuccess(w, &key)
  44. }
  45. func (k Keys) ListKeys(w http.ResponseWriter, r *http.Request) {
  46. keys, err := k.manager.List()
  47. if err != nil {
  48. writeError(w, err)
  49. return
  50. }
  51. writeSuccess(w, keys)
  52. }
  53. func (k Keys) UpdateKey(w http.ResponseWriter, r *http.Request) {
  54. req := types.UpdateKeyRequest{}
  55. err := readRequest(r, &req)
  56. if err != nil {
  57. writeError(w, err)
  58. return
  59. }
  60. vars := mux.Vars(r)
  61. name := vars["name"]
  62. if name != req.Name {
  63. writeError(w, errors.New("path and json key names don't match"))
  64. return
  65. }
  66. err = k.manager.Update(req.Name, req.OldPass, req.NewPass)
  67. if err != nil {
  68. writeError(w, err)
  69. return
  70. }
  71. key, err := k.manager.Get(req.Name)
  72. if err != nil {
  73. writeError(w, err)
  74. return
  75. }
  76. writeSuccess(w, &key)
  77. }
  78. func (k Keys) DeleteKey(w http.ResponseWriter, r *http.Request) {
  79. req := types.DeleteKeyRequest{}
  80. err := readRequest(r, &req)
  81. if err != nil {
  82. writeError(w, err)
  83. return
  84. }
  85. vars := mux.Vars(r)
  86. name := vars["name"]
  87. if name != req.Name {
  88. writeError(w, errors.New("path and json key names don't match"))
  89. return
  90. }
  91. err = k.manager.Delete(req.Name, req.Passphrase)
  92. if err != nil {
  93. writeError(w, err)
  94. return
  95. }
  96. // not really an error, but something generic
  97. resp := types.ErrorResponse{
  98. Success: true,
  99. }
  100. writeSuccess(w, &resp)
  101. }
  102. func (k Keys) Register(r *mux.Router) {
  103. r.HandleFunc("/", k.GenerateKey).Methods("POST")
  104. r.HandleFunc("/", k.ListKeys).Methods("GET")
  105. r.HandleFunc("/{name}", k.GetKey).Methods("GET")
  106. r.HandleFunc("/{name}", k.UpdateKey).Methods("POST", "PUT")
  107. r.HandleFunc("/{name}", k.DeleteKey).Methods("DELETE")
  108. }