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 5.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. "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. api "code.gitea.io/sdk/gitea"
  12. )
  13. func composeDeployKeysAPILink(repoPath string) string {
  14. return setting.AppURL + "api/v1/repos/" + repoPath + "/keys/"
  15. }
  16. // ListDeployKeys list all the deploy keys of a repository
  17. func ListDeployKeys(ctx *context.APIContext) {
  18. // swagger:operation GET /repos/{owner}/{repo}/keys repository repoListKeys
  19. // ---
  20. // summary: List a repository's keys
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: owner
  25. // in: path
  26. // description: owner of the repo
  27. // type: string
  28. // required: true
  29. // - name: repo
  30. // in: path
  31. // description: name of the repo
  32. // type: string
  33. // required: true
  34. // responses:
  35. // "200":
  36. // "$ref": "#/responses/DeployKeyList"
  37. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  38. if err != nil {
  39. ctx.Error(500, "ListDeployKeys", err)
  40. return
  41. }
  42. apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
  43. apiKeys := make([]*api.DeployKey, len(keys))
  44. for i := range keys {
  45. if err = keys[i].GetContent(); err != nil {
  46. ctx.Error(500, "GetContent", err)
  47. return
  48. }
  49. apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
  50. }
  51. ctx.JSON(200, &apiKeys)
  52. }
  53. // GetDeployKey get a deploy key by id
  54. func GetDeployKey(ctx *context.APIContext) {
  55. // swagger:operation GET /repos/{owner}/{repo}/keys/{id} repository repoGetKey
  56. // ---
  57. // summary: Get a repository's key by id
  58. // produces:
  59. // - application/json
  60. // parameters:
  61. // - name: owner
  62. // in: path
  63. // description: owner of the repo
  64. // type: string
  65. // required: true
  66. // - name: repo
  67. // in: path
  68. // description: name of the repo
  69. // type: string
  70. // required: true
  71. // - name: id
  72. // in: path
  73. // description: id of the key to get
  74. // type: integer
  75. // required: true
  76. // responses:
  77. // "200":
  78. // "$ref": "#/responses/DeployKey"
  79. key, err := models.GetDeployKeyByID(ctx.ParamsInt64(":id"))
  80. if err != nil {
  81. if models.IsErrDeployKeyNotExist(err) {
  82. ctx.Status(404)
  83. } else {
  84. ctx.Error(500, "GetDeployKeyByID", err)
  85. }
  86. return
  87. }
  88. if err = key.GetContent(); err != nil {
  89. ctx.Error(500, "GetContent", err)
  90. return
  91. }
  92. apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
  93. ctx.JSON(200, convert.ToDeployKey(apiLink, key))
  94. }
  95. // HandleCheckKeyStringError handle check key error
  96. func HandleCheckKeyStringError(ctx *context.APIContext, err error) {
  97. if models.IsErrSSHDisabled(err) {
  98. ctx.Error(422, "", "SSH is disabled")
  99. } else if models.IsErrKeyUnableVerify(err) {
  100. ctx.Error(422, "", "Unable to verify key content")
  101. } else {
  102. ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err))
  103. }
  104. }
  105. // HandleAddKeyError handle add key error
  106. func HandleAddKeyError(ctx *context.APIContext, err error) {
  107. switch {
  108. case models.IsErrKeyAlreadyExist(err):
  109. ctx.Error(422, "", "Key content has been used as non-deploy key")
  110. case models.IsErrKeyNameAlreadyUsed(err):
  111. ctx.Error(422, "", "Key title has been used")
  112. default:
  113. ctx.Error(500, "AddKey", err)
  114. }
  115. }
  116. // CreateDeployKey create deploy key for a repository
  117. func CreateDeployKey(ctx *context.APIContext, form api.CreateKeyOption) {
  118. // swagger:operation POST /repos/{owner}/{repo}/keys repository repoCreateKey
  119. // ---
  120. // summary: Add a key to a repository
  121. // consumes:
  122. // - application/json
  123. // produces:
  124. // - application/json
  125. // parameters:
  126. // - name: owner
  127. // in: path
  128. // description: owner of the repo
  129. // type: string
  130. // required: true
  131. // - name: repo
  132. // in: path
  133. // description: name of the repo
  134. // type: string
  135. // required: true
  136. // - name: body
  137. // in: body
  138. // schema:
  139. // "$ref": "#/definitions/CreateKeyOption"
  140. // responses:
  141. // "201":
  142. // "$ref": "#/responses/DeployKey"
  143. content, err := models.CheckPublicKeyString(form.Key)
  144. if err != nil {
  145. HandleCheckKeyStringError(ctx, err)
  146. return
  147. }
  148. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content, form.ReadOnly)
  149. if err != nil {
  150. HandleAddKeyError(ctx, err)
  151. return
  152. }
  153. key.Content = content
  154. apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
  155. ctx.JSON(201, convert.ToDeployKey(apiLink, key))
  156. }
  157. // DeleteDeploykey delete deploy key for a repository
  158. func DeleteDeploykey(ctx *context.APIContext) {
  159. // swagger:operation DELETE /repos/{owner}/{repo}/keys/{id} repository repoDeleteKey
  160. // ---
  161. // summary: Delete a key from a repository
  162. // parameters:
  163. // - name: owner
  164. // in: path
  165. // description: owner of the repo
  166. // type: string
  167. // required: true
  168. // - name: repo
  169. // in: path
  170. // description: name of the repo
  171. // type: string
  172. // required: true
  173. // - name: id
  174. // in: path
  175. // description: id of the key to delete
  176. // type: integer
  177. // required: true
  178. // responses:
  179. // "204":
  180. // "$ref": "#/responses/empty"
  181. if err := models.DeleteDeployKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  182. if models.IsErrKeyAccessDenied(err) {
  183. ctx.Error(403, "", "You do not have access to this key")
  184. } else {
  185. ctx.Error(500, "DeleteDeployKey", err)
  186. }
  187. return
  188. }
  189. ctx.Status(204)
  190. }