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.

key.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 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. "code.gitea.io/gitea/routers/api/v1/repo"
  12. )
  13. // GetUserByParamsName get user by name
  14. func GetUserByParamsName(ctx *context.APIContext, name string) *models.User {
  15. user, err := models.GetUserByName(ctx.Params(name))
  16. if err != nil {
  17. if models.IsErrUserNotExist(err) {
  18. ctx.Status(404)
  19. } else {
  20. ctx.Error(500, "GetUserByName", err)
  21. }
  22. return nil
  23. }
  24. return user
  25. }
  26. // GetUserByParams returns user whose name is presented in URL paramenter.
  27. func GetUserByParams(ctx *context.APIContext) *models.User {
  28. return GetUserByParamsName(ctx, ":username")
  29. }
  30. func composePublicKeysAPILink() string {
  31. return setting.AppURL + "api/v1/user/keys/"
  32. }
  33. func listPublicKeys(ctx *context.APIContext, uid int64) {
  34. keys, err := models.ListPublicKeys(uid)
  35. if err != nil {
  36. ctx.Error(500, "ListPublicKeys", err)
  37. return
  38. }
  39. apiLink := composePublicKeysAPILink()
  40. apiKeys := make([]*api.PublicKey, len(keys))
  41. for i := range keys {
  42. apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
  43. }
  44. ctx.JSON(200, &apiKeys)
  45. }
  46. // ListMyPublicKeys list all of the authenticated user's public keys
  47. func ListMyPublicKeys(ctx *context.APIContext) {
  48. // swagger:operation GET /user/keys user userCurrentListKeys
  49. // ---
  50. // summary: List the authenticated user's public keys
  51. // produces:
  52. // - application/json
  53. // responses:
  54. // "200":
  55. // "$ref": "#/responses/PublicKeyList"
  56. listPublicKeys(ctx, ctx.User.ID)
  57. }
  58. // ListPublicKeys list the given user's public keys
  59. func ListPublicKeys(ctx *context.APIContext) {
  60. // swagger:operation GET /users/{username}/keys user userListKeys
  61. // ---
  62. // summary: List the given user's public keys
  63. // produces:
  64. // - application/json
  65. // parameters:
  66. // - name: username
  67. // in: path
  68. // description: username of user
  69. // type: string
  70. // required: true
  71. // responses:
  72. // "200":
  73. // "$ref": "#/responses/PublicKeyList"
  74. user := GetUserByParams(ctx)
  75. if ctx.Written() {
  76. return
  77. }
  78. listPublicKeys(ctx, user.ID)
  79. }
  80. // GetPublicKey get a public key
  81. func GetPublicKey(ctx *context.APIContext) {
  82. // swagger:operation GET /user/keys/{id} user userCurrentGetKey
  83. // ---
  84. // summary: Get a public key
  85. // produces:
  86. // - application/json
  87. // parameters:
  88. // - name: id
  89. // in: path
  90. // description: id of key to get
  91. // type: integer
  92. // required: true
  93. // responses:
  94. // "200":
  95. // "$ref": "#/responses/PublicKey"
  96. // "404":
  97. // "$ref": "#/responses/notFound"
  98. key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id"))
  99. if err != nil {
  100. if models.IsErrKeyNotExist(err) {
  101. ctx.Status(404)
  102. } else {
  103. ctx.Error(500, "GetPublicKeyByID", err)
  104. }
  105. return
  106. }
  107. apiLink := composePublicKeysAPILink()
  108. ctx.JSON(200, convert.ToPublicKey(apiLink, key))
  109. }
  110. // CreateUserPublicKey creates new public key to given user by ID.
  111. func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
  112. content, err := models.CheckPublicKeyString(form.Key)
  113. if err != nil {
  114. repo.HandleCheckKeyStringError(ctx, err)
  115. return
  116. }
  117. key, err := models.AddPublicKey(uid, form.Title, content)
  118. if err != nil {
  119. repo.HandleAddKeyError(ctx, err)
  120. return
  121. }
  122. apiLink := composePublicKeysAPILink()
  123. ctx.JSON(201, convert.ToPublicKey(apiLink, key))
  124. }
  125. // CreatePublicKey create one public key for me
  126. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  127. // swagger:operation POST /user/keys user userCurrentPostKey
  128. // ---
  129. // summary: Create a public key
  130. // consumes:
  131. // - application/json
  132. // produces:
  133. // - application/json
  134. // parameters:
  135. // - name: body
  136. // in: body
  137. // schema:
  138. // "$ref": "#/definitions/CreateKeyOption"
  139. // responses:
  140. // "201":
  141. // "$ref": "#/responses/PublicKey"
  142. // "422":
  143. // "$ref": "#/responses/validationError"
  144. CreateUserPublicKey(ctx, form, ctx.User.ID)
  145. }
  146. // DeletePublicKey delete one public key
  147. func DeletePublicKey(ctx *context.APIContext) {
  148. // swagger:operation DELETE /user/keys/{id} user userCurrentDeleteKey
  149. // ---
  150. // summary: Delete a public key
  151. // produces:
  152. // - application/json
  153. // parameters:
  154. // - name: id
  155. // in: path
  156. // description: id of key to delete
  157. // type: integer
  158. // required: true
  159. // responses:
  160. // "204":
  161. // "$ref": "#/responses/empty"
  162. // "403":
  163. // "$ref": "#/responses/forbidden"
  164. // "404":
  165. // "$ref": "#/responses/notFound"
  166. if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  167. if models.IsErrKeyNotExist(err) {
  168. ctx.Status(404)
  169. } else if models.IsErrKeyAccessDenied(err) {
  170. ctx.Error(403, "", "You do not have access to this key")
  171. } else {
  172. ctx.Error(500, "DeletePublicKey", err)
  173. }
  174. return
  175. }
  176. ctx.Status(204)
  177. }