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.

153 lines
3.6 KiB

  1. // Copyright 2017 The Gitea 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 user
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/routers/api/v1/convert"
  11. )
  12. func composePublicGPGKeysAPILink() string {
  13. return setting.AppURL + "api/v1/user/gpg_keys/"
  14. }
  15. func listGPGKeys(ctx *context.APIContext, uid int64) {
  16. keys, err := models.ListGPGKeys(uid)
  17. if err != nil {
  18. ctx.Error(500, "ListGPGKeys", err)
  19. return
  20. }
  21. apiKeys := make([]*api.GPGKey, len(keys))
  22. for i := range keys {
  23. apiKeys[i] = convert.ToGPGKey(keys[i])
  24. }
  25. ctx.JSON(200, &apiKeys)
  26. }
  27. //ListGPGKeys get the GPG key list of a user
  28. func ListGPGKeys(ctx *context.APIContext) {
  29. // swagger:route GET /users/{username}/gpg_keys userListGPGKeys
  30. //
  31. // Produces:
  32. // - application/json
  33. //
  34. // Responses:
  35. // 200: GPGKeyList
  36. // 500: error
  37. user := GetUserByParams(ctx)
  38. if ctx.Written() {
  39. return
  40. }
  41. listGPGKeys(ctx, user.ID)
  42. }
  43. //ListMyGPGKeys get the GPG key list of the logged user
  44. func ListMyGPGKeys(ctx *context.APIContext) {
  45. // swagger:route GET /user/gpg_keys userCurrentListGPGKeys
  46. //
  47. // Produces:
  48. // - application/json
  49. //
  50. // Responses:
  51. // 200: GPGKeyList
  52. // 500: error
  53. listGPGKeys(ctx, ctx.User.ID)
  54. }
  55. //GetGPGKey get the GPG key based on a id
  56. func GetGPGKey(ctx *context.APIContext) {
  57. // swagger:route GET /user/gpg_keys/{id} userCurrentGetGPGKey
  58. //
  59. // Produces:
  60. // - application/json
  61. //
  62. // Responses:
  63. // 200: GPGKey
  64. // 404: notFound
  65. // 500: error
  66. key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id"))
  67. if err != nil {
  68. if models.IsErrGPGKeyNotExist(err) {
  69. ctx.Status(404)
  70. } else {
  71. ctx.Error(500, "GetGPGKeyByID", err)
  72. }
  73. return
  74. }
  75. ctx.JSON(200, convert.ToGPGKey(key))
  76. }
  77. // CreateUserGPGKey creates new GPG key to given user by ID.
  78. func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
  79. key, err := models.AddGPGKey(uid, form.ArmoredKey)
  80. if err != nil {
  81. HandleAddGPGKeyError(ctx, err)
  82. return
  83. }
  84. ctx.JSON(201, convert.ToGPGKey(key))
  85. }
  86. //CreateGPGKey associate a GPG key to the current user
  87. func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
  88. // swagger:route POST /user/gpg_keys userCurrentPostGPGKey
  89. //
  90. // Consumes:
  91. // - application/json
  92. //
  93. // Produces:
  94. // - application/json
  95. //
  96. // Responses:
  97. // 201: GPGKey
  98. // 422: validationError
  99. // 500: error
  100. CreateUserGPGKey(ctx, form, ctx.User.ID)
  101. }
  102. //DeleteGPGKey remove a GPG key associated to the current user
  103. func DeleteGPGKey(ctx *context.APIContext) {
  104. // swagger:route DELETE /user/gpg_keys/{id} userCurrentDeleteGPGKey
  105. //
  106. // Produces:
  107. // - application/json
  108. //
  109. // Responses:
  110. // 204: empty
  111. // 403: forbidden
  112. // 500: error
  113. if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  114. if models.IsErrGPGKeyAccessDenied(err) {
  115. ctx.Error(403, "", "You do not have access to this key")
  116. } else {
  117. ctx.Error(500, "DeleteGPGKey", err)
  118. }
  119. return
  120. }
  121. ctx.Status(204)
  122. }
  123. // HandleAddGPGKeyError handle add GPGKey error
  124. func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
  125. switch {
  126. case models.IsErrGPGKeyAccessDenied(err):
  127. ctx.Error(422, "", "You do not have access to this gpg key")
  128. case models.IsErrGPGKeyIDAlreadyUsed(err):
  129. ctx.Error(422, "", "A key with the same keyid is already in database")
  130. default:
  131. ctx.Error(500, "AddGPGKey", err)
  132. }
  133. }