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 4.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 my public keys
  47. func ListMyPublicKeys(ctx *context.APIContext) {
  48. // swagger:route GET /user/keys user userCurrentListKeys
  49. //
  50. // Produces:
  51. // - application/json
  52. //
  53. // Responses:
  54. // 200: PublicKeyList
  55. // 500: error
  56. listPublicKeys(ctx, ctx.User.ID)
  57. }
  58. // ListPublicKeys list all user's public keys
  59. func ListPublicKeys(ctx *context.APIContext) {
  60. // swagger:route GET /users/{username}/keys user userListKeys
  61. //
  62. // Produces:
  63. // - application/json
  64. //
  65. // Responses:
  66. // 200: PublicKeyList
  67. // 500: error
  68. user := GetUserByParams(ctx)
  69. if ctx.Written() {
  70. return
  71. }
  72. listPublicKeys(ctx, user.ID)
  73. }
  74. // GetPublicKey get one public key
  75. func GetPublicKey(ctx *context.APIContext) {
  76. // swagger:route GET /user/keys/{id} user userCurrentGetKey
  77. //
  78. // Produces:
  79. // - application/json
  80. //
  81. // Responses:
  82. // 200: PublicKey
  83. // 404: notFound
  84. // 500: error
  85. key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id"))
  86. if err != nil {
  87. if models.IsErrKeyNotExist(err) {
  88. ctx.Status(404)
  89. } else {
  90. ctx.Error(500, "GetPublicKeyByID", err)
  91. }
  92. return
  93. }
  94. apiLink := composePublicKeysAPILink()
  95. ctx.JSON(200, convert.ToPublicKey(apiLink, key))
  96. }
  97. // CreateUserPublicKey creates new public key to given user by ID.
  98. func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
  99. content, err := models.CheckPublicKeyString(form.Key)
  100. if err != nil {
  101. repo.HandleCheckKeyStringError(ctx, err)
  102. return
  103. }
  104. key, err := models.AddPublicKey(uid, form.Title, content)
  105. if err != nil {
  106. repo.HandleAddKeyError(ctx, err)
  107. return
  108. }
  109. apiLink := composePublicKeysAPILink()
  110. ctx.JSON(201, convert.ToPublicKey(apiLink, key))
  111. }
  112. // CreatePublicKey create one public key for me
  113. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  114. // swagger:route POST /user/keys user userCurrentPostKey
  115. //
  116. // Consumes:
  117. // - application/json
  118. //
  119. // Produces:
  120. // - application/json
  121. //
  122. // Responses:
  123. // 201: PublicKey
  124. // 422: validationError
  125. // 500: error
  126. CreateUserPublicKey(ctx, form, ctx.User.ID)
  127. }
  128. // DeletePublicKey delete one public key of mine
  129. func DeletePublicKey(ctx *context.APIContext) {
  130. // swagger:route DELETE /user/keys/{id} user userCurrentDeleteKey
  131. //
  132. // Produces:
  133. // - application/json
  134. //
  135. // Responses:
  136. // 204: empty
  137. // 403: forbidden
  138. // 500: error
  139. if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  140. if models.IsErrKeyAccessDenied(err) {
  141. ctx.Error(403, "", "You do not have access to this key")
  142. } else {
  143. ctx.Error(500, "DeletePublicKey", err)
  144. }
  145. return
  146. }
  147. ctx.Status(204)
  148. }