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.

gpg_key.go 4.4KB

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