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.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // format: int64
  93. // required: true
  94. // responses:
  95. // "200":
  96. // "$ref": "#/responses/PublicKey"
  97. // "404":
  98. // "$ref": "#/responses/notFound"
  99. key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id"))
  100. if err != nil {
  101. if models.IsErrKeyNotExist(err) {
  102. ctx.Status(404)
  103. } else {
  104. ctx.Error(500, "GetPublicKeyByID", err)
  105. }
  106. return
  107. }
  108. apiLink := composePublicKeysAPILink()
  109. ctx.JSON(200, convert.ToPublicKey(apiLink, key))
  110. }
  111. // CreateUserPublicKey creates new public key to given user by ID.
  112. func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
  113. content, err := models.CheckPublicKeyString(form.Key)
  114. if err != nil {
  115. repo.HandleCheckKeyStringError(ctx, err)
  116. return
  117. }
  118. key, err := models.AddPublicKey(uid, form.Title, content, 0)
  119. if err != nil {
  120. repo.HandleAddKeyError(ctx, err)
  121. return
  122. }
  123. apiLink := composePublicKeysAPILink()
  124. ctx.JSON(201, convert.ToPublicKey(apiLink, key))
  125. }
  126. // CreatePublicKey create one public key for me
  127. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  128. // swagger:operation POST /user/keys user userCurrentPostKey
  129. // ---
  130. // summary: Create a public key
  131. // consumes:
  132. // - application/json
  133. // produces:
  134. // - application/json
  135. // parameters:
  136. // - name: body
  137. // in: body
  138. // schema:
  139. // "$ref": "#/definitions/CreateKeyOption"
  140. // responses:
  141. // "201":
  142. // "$ref": "#/responses/PublicKey"
  143. // "422":
  144. // "$ref": "#/responses/validationError"
  145. CreateUserPublicKey(ctx, form, ctx.User.ID)
  146. }
  147. // DeletePublicKey delete one public key
  148. func DeletePublicKey(ctx *context.APIContext) {
  149. // swagger:operation DELETE /user/keys/{id} user userCurrentDeleteKey
  150. // ---
  151. // summary: Delete a public key
  152. // produces:
  153. // - application/json
  154. // parameters:
  155. // - name: id
  156. // in: path
  157. // description: id of key to delete
  158. // type: integer
  159. // format: int64
  160. // required: true
  161. // responses:
  162. // "204":
  163. // "$ref": "#/responses/empty"
  164. // "403":
  165. // "$ref": "#/responses/forbidden"
  166. // "404":
  167. // "$ref": "#/responses/notFound"
  168. if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  169. if models.IsErrKeyNotExist(err) {
  170. ctx.Status(404)
  171. } else if models.IsErrKeyAccessDenied(err) {
  172. ctx.Error(403, "", "You do not have access to this key")
  173. } else {
  174. ctx.Error(500, "DeletePublicKey", err)
  175. }
  176. return
  177. }
  178. ctx.Status(204)
  179. }