key.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // ListDeployKeys list all the deploy keys of a repository
  19. func ListDeployKeys(c *context.APIContext) {
  20. keys, err := models.ListDeployKeys(c.Repo.Repository.ID)
  21. if err != nil {
  22. c.Error(500, "ListDeployKeys", err)
  23. return
  24. }
  25. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  26. apiKeys := make([]*api.DeployKey, len(keys))
  27. for i := range keys {
  28. if err = keys[i].GetContent(); err != nil {
  29. c.Error(500, "GetContent", err)
  30. return
  31. }
  32. apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
  33. }
  34. c.JSON(200, &apiKeys)
  35. }
  36. // GetDeployKey get a deploy key by id
  37. func GetDeployKey(c *context.APIContext) {
  38. key, err := models.GetDeployKeyByID(c.ParamsInt64(":id"))
  39. if err != nil {
  40. if models.IsErrDeployKeyNotExist(err) {
  41. c.Status(404)
  42. } else {
  43. c.Error(500, "GetDeployKeyByID", err)
  44. }
  45. return
  46. }
  47. if err = key.GetContent(); err != nil {
  48. c.Error(500, "GetContent", err)
  49. return
  50. }
  51. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  52. c.JSON(200, convert.ToDeployKey(apiLink, key))
  53. }
  54. // HandleCheckKeyStringError handle check key error
  55. func HandleCheckKeyStringError(c *context.APIContext, err error) {
  56. if models.IsErrKeyUnableVerify(err) {
  57. c.Error(422, "", "Unable to verify key content")
  58. } else {
  59. c.Error(422, "", fmt.Errorf("Invalid key content: %v", err))
  60. }
  61. }
  62. // HandleAddKeyError handle add key error
  63. func HandleAddKeyError(c *context.APIContext, err error) {
  64. switch {
  65. case models.IsErrKeyAlreadyExist(err):
  66. c.Error(422, "", "Key content has been used as non-deploy key")
  67. case models.IsErrKeyNameAlreadyUsed(err):
  68. c.Error(422, "", "Key title has been used")
  69. default:
  70. c.Error(500, "AddKey", err)
  71. }
  72. }
  73. // CreateDeployKey create deploy key for a repository
  74. func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) {
  75. content, err := models.CheckPublicKeyString(form.Key)
  76. if err != nil {
  77. HandleCheckKeyStringError(c, err)
  78. return
  79. }
  80. key, err := models.AddDeployKey(c.Repo.Repository.ID, form.Title, content)
  81. if err != nil {
  82. HandleAddKeyError(c, err)
  83. return
  84. }
  85. key.Content = content
  86. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  87. c.JSON(201, convert.ToDeployKey(apiLink, key))
  88. }
  89. // DeleteDeploykey delete deploy key for a repository
  90. func DeleteDeploykey(c *context.APIContext) {
  91. if err := models.DeleteDeployKey(c.User, c.ParamsInt64(":id")); err != nil {
  92. if models.IsErrKeyAccessDenied(err) {
  93. c.Error(403, "", "You do not have access to this key")
  94. } else {
  95. c.Error(500, "DeleteDeployKey", err)
  96. }
  97. return
  98. }
  99. c.Status(204)
  100. }