You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

key.go 3.3 kB

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