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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // required: true
  74. // responses:
  75. // "200":
  76. // "$ref": "#/responses/GPGKey"
  77. // "404":
  78. // "$ref": "#/responses/notFound"
  79. key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id"))
  80. if err != nil {
  81. if models.IsErrGPGKeyNotExist(err) {
  82. ctx.Status(404)
  83. } else {
  84. ctx.Error(500, "GetGPGKeyByID", err)
  85. }
  86. return
  87. }
  88. ctx.JSON(200, convert.ToGPGKey(key))
  89. }
  90. // CreateUserGPGKey creates new GPG key to given user by ID.
  91. func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
  92. key, err := models.AddGPGKey(uid, form.ArmoredKey)
  93. if err != nil {
  94. HandleAddGPGKeyError(ctx, err)
  95. return
  96. }
  97. ctx.JSON(201, convert.ToGPGKey(key))
  98. }
  99. // swagger:parameters userCurrentPostGPGKey
  100. type swaggerUserCurrentPostGPGKey struct {
  101. // in:body
  102. Form api.CreateGPGKeyOption
  103. }
  104. //CreateGPGKey create a GPG key belonging to the authenticated user
  105. func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
  106. // swagger:operation POST /user/gpg_keys user userCurrentPostGPGKey
  107. // ---
  108. // summary: Create a GPG key
  109. // consumes:
  110. // - application/json
  111. // produces:
  112. // - application/json
  113. // responses:
  114. // "201":
  115. // "$ref": "#/responses/GPGKey"
  116. // "422":
  117. // "$ref": "#/responses/validationError"
  118. CreateUserGPGKey(ctx, form, ctx.User.ID)
  119. }
  120. //DeleteGPGKey remove a GPG key belonging to the authenticated user
  121. func DeleteGPGKey(ctx *context.APIContext) {
  122. // swagger:operation DELETE /user/gpg_keys/{id} user userCurrentDeleteGPGKey
  123. // ---
  124. // summary: Remove a GPG key
  125. // produces:
  126. // - application/json
  127. // parameters:
  128. // - name: id
  129. // in: path
  130. // description: id of key to delete
  131. // type: integer
  132. // required: true
  133. // responses:
  134. // "204":
  135. // "$ref": "#/responses/empty"
  136. // "403":
  137. // "$ref": "#/responses/forbidden"
  138. if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  139. if models.IsErrGPGKeyAccessDenied(err) {
  140. ctx.Error(403, "", "You do not have access to this key")
  141. } else {
  142. ctx.Error(500, "DeleteGPGKey", err)
  143. }
  144. return
  145. }
  146. ctx.Status(204)
  147. }
  148. // HandleAddGPGKeyError handle add GPGKey error
  149. func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
  150. switch {
  151. case models.IsErrGPGKeyAccessDenied(err):
  152. ctx.Error(422, "", "You do not have access to this GPG key")
  153. case models.IsErrGPGKeyIDAlreadyUsed(err):
  154. ctx.Error(422, "", "A key with the same id already exists")
  155. default:
  156. ctx.Error(500, "AddGPGKey", err)
  157. }
  158. }