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.

gpg_key.go 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. "net/http"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/convert"
  10. api "code.gitea.io/gitea/modules/structs"
  11. )
  12. func listGPGKeys(ctx *context.APIContext, uid int64) {
  13. keys, err := models.ListGPGKeys(uid)
  14. if err != nil {
  15. ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
  16. return
  17. }
  18. apiKeys := make([]*api.GPGKey, len(keys))
  19. for i := range keys {
  20. apiKeys[i] = convert.ToGPGKey(keys[i])
  21. }
  22. ctx.JSON(http.StatusOK, &apiKeys)
  23. }
  24. //ListGPGKeys get the GPG key list of a user
  25. func ListGPGKeys(ctx *context.APIContext) {
  26. // swagger:operation GET /users/{username}/gpg_keys user userListGPGKeys
  27. // ---
  28. // summary: List the given user's GPG keys
  29. // produces:
  30. // - application/json
  31. // parameters:
  32. // - name: username
  33. // in: path
  34. // description: username of user
  35. // type: string
  36. // required: true
  37. // responses:
  38. // "200":
  39. // "$ref": "#/responses/GPGKeyList"
  40. user := GetUserByParams(ctx)
  41. if ctx.Written() {
  42. return
  43. }
  44. listGPGKeys(ctx, user.ID)
  45. }
  46. //ListMyGPGKeys get the GPG key list of the authenticated user
  47. func ListMyGPGKeys(ctx *context.APIContext) {
  48. // swagger:operation GET /user/gpg_keys user userCurrentListGPGKeys
  49. // ---
  50. // summary: List the authenticated user's GPG keys
  51. // produces:
  52. // - application/json
  53. // responses:
  54. // "200":
  55. // "$ref": "#/responses/GPGKeyList"
  56. listGPGKeys(ctx, ctx.User.ID)
  57. }
  58. //GetGPGKey get the GPG key based on a id
  59. func GetGPGKey(ctx *context.APIContext) {
  60. // swagger:operation GET /user/gpg_keys/{id} user userCurrentGetGPGKey
  61. // ---
  62. // summary: Get a GPG key
  63. // produces:
  64. // - application/json
  65. // parameters:
  66. // - name: id
  67. // in: path
  68. // description: id of key to get
  69. // type: integer
  70. // format: int64
  71. // required: true
  72. // responses:
  73. // "200":
  74. // "$ref": "#/responses/GPGKey"
  75. // "404":
  76. // "$ref": "#/responses/notFound"
  77. key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id"))
  78. if err != nil {
  79. if models.IsErrGPGKeyNotExist(err) {
  80. ctx.NotFound()
  81. } else {
  82. ctx.Error(http.StatusInternalServerError, "GetGPGKeyByID", err)
  83. }
  84. return
  85. }
  86. ctx.JSON(http.StatusOK, convert.ToGPGKey(key))
  87. }
  88. // CreateUserGPGKey creates new GPG key to given user by ID.
  89. func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
  90. key, err := models.AddGPGKey(uid, form.ArmoredKey)
  91. if err != nil {
  92. HandleAddGPGKeyError(ctx, err)
  93. return
  94. }
  95. ctx.JSON(http.StatusCreated, convert.ToGPGKey(key))
  96. }
  97. // swagger:parameters userCurrentPostGPGKey
  98. type swaggerUserCurrentPostGPGKey struct {
  99. // in:body
  100. Form api.CreateGPGKeyOption
  101. }
  102. //CreateGPGKey create a GPG key belonging to the authenticated user
  103. func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
  104. // swagger:operation POST /user/gpg_keys user userCurrentPostGPGKey
  105. // ---
  106. // summary: Create a GPG key
  107. // consumes:
  108. // - application/json
  109. // produces:
  110. // - application/json
  111. // responses:
  112. // "201":
  113. // "$ref": "#/responses/GPGKey"
  114. // "422":
  115. // "$ref": "#/responses/validationError"
  116. CreateUserGPGKey(ctx, form, ctx.User.ID)
  117. }
  118. //DeleteGPGKey remove a GPG key belonging to the authenticated user
  119. func DeleteGPGKey(ctx *context.APIContext) {
  120. // swagger:operation DELETE /user/gpg_keys/{id} user userCurrentDeleteGPGKey
  121. // ---
  122. // summary: Remove a GPG key
  123. // produces:
  124. // - application/json
  125. // parameters:
  126. // - name: id
  127. // in: path
  128. // description: id of key to delete
  129. // type: integer
  130. // format: int64
  131. // required: true
  132. // responses:
  133. // "204":
  134. // "$ref": "#/responses/empty"
  135. // "403":
  136. // "$ref": "#/responses/forbidden"
  137. if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  138. if models.IsErrGPGKeyAccessDenied(err) {
  139. ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
  140. } else {
  141. ctx.Error(http.StatusInternalServerError, "DeleteGPGKey", err)
  142. }
  143. return
  144. }
  145. ctx.Status(http.StatusNoContent)
  146. }
  147. // HandleAddGPGKeyError handle add GPGKey error
  148. func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
  149. switch {
  150. case models.IsErrGPGKeyAccessDenied(err):
  151. ctx.Error(http.StatusUnprocessableEntity, "", "You do not have access to this GPG key")
  152. case models.IsErrGPGKeyIDAlreadyUsed(err):
  153. ctx.Error(http.StatusUnprocessableEntity, "", "A key with the same id already exists")
  154. default:
  155. ctx.Error(http.StatusInternalServerError, "AddGPGKey", err)
  156. }
  157. }