key.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "fmt"
  9. "gitote/gitote/models"
  10. "gitote/gitote/pkg/context"
  11. "gitote/gitote/pkg/setting"
  12. "gitote/gitote/routes/api/v1/convert"
  13. api "gitlab.com/gitote/go-gitote-client"
  14. )
  15. func composeDeployKeysAPILink(repoPath string) string {
  16. return setting.AppURL + "api/v1/repos/" + repoPath + "/keys/"
  17. }
  18. func ListDeployKeys(c *context.APIContext) {
  19. keys, err := models.ListDeployKeys(c.Repo.Repository.ID)
  20. if err != nil {
  21. c.Error(500, "ListDeployKeys", err)
  22. return
  23. }
  24. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  25. apiKeys := make([]*api.DeployKey, len(keys))
  26. for i := range keys {
  27. if err = keys[i].GetContent(); err != nil {
  28. c.Error(500, "GetContent", err)
  29. return
  30. }
  31. apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
  32. }
  33. c.JSON(200, &apiKeys)
  34. }
  35. func GetDeployKey(c *context.APIContext) {
  36. key, err := models.GetDeployKeyByID(c.ParamsInt64(":id"))
  37. if err != nil {
  38. if models.IsErrDeployKeyNotExist(err) {
  39. c.Status(404)
  40. } else {
  41. c.Error(500, "GetDeployKeyByID", err)
  42. }
  43. return
  44. }
  45. if err = key.GetContent(); err != nil {
  46. c.Error(500, "GetContent", err)
  47. return
  48. }
  49. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  50. c.JSON(200, convert.ToDeployKey(apiLink, key))
  51. }
  52. func HandleCheckKeyStringError(c *context.APIContext, err error) {
  53. if models.IsErrKeyUnableVerify(err) {
  54. c.Error(422, "", "Unable to verify key content")
  55. } else {
  56. c.Error(422, "", fmt.Errorf("Invalid key content: %v", err))
  57. }
  58. }
  59. func HandleAddKeyError(c *context.APIContext, err error) {
  60. switch {
  61. case models.IsErrKeyAlreadyExist(err):
  62. c.Error(422, "", "Key content has been used as non-deploy key")
  63. case models.IsErrKeyNameAlreadyUsed(err):
  64. c.Error(422, "", "Key title has been used")
  65. default:
  66. c.Error(500, "AddKey", err)
  67. }
  68. }
  69. func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) {
  70. content, err := models.CheckPublicKeyString(form.Key)
  71. if err != nil {
  72. HandleCheckKeyStringError(c, err)
  73. return
  74. }
  75. key, err := models.AddDeployKey(c.Repo.Repository.ID, form.Title, content)
  76. if err != nil {
  77. HandleAddKeyError(c, err)
  78. return
  79. }
  80. key.Content = content
  81. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  82. c.JSON(201, convert.ToDeployKey(apiLink, key))
  83. }
  84. func DeleteDeploykey(c *context.APIContext) {
  85. if err := models.DeleteDeployKey(c.User, c.ParamsInt64(":id")); err != nil {
  86. if models.IsErrKeyAccessDenied(err) {
  87. c.Error(403, "", "You do not have access to this key")
  88. } else {
  89. c.Error(500, "DeleteDeployKey", err)
  90. }
  91. return
  92. }
  93. c.Status(204)
  94. }