key.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "github.com/pkg/errors"
  9. "gogs.io/gogs/internal/conf"
  10. "gogs.io/gogs/internal/context"
  11. "gogs.io/gogs/internal/db"
  12. "gogs.io/gogs/internal/route/api/v1/convert"
  13. "gogs.io/gogs/internal/route/api/v1/repo"
  14. )
  15. func GetUserByParamsName(c *context.APIContext, name string) *db.User {
  16. user, err := db.GetUserByName(c.Params(name))
  17. if err != nil {
  18. c.NotFoundOrError(err, "get user by name")
  19. return nil
  20. }
  21. return user
  22. }
  23. // GetUserByParams returns user whose name is presented in URL paramenter.
  24. func GetUserByParams(c *context.APIContext) *db.User {
  25. return GetUserByParamsName(c, ":username")
  26. }
  27. func composePublicKeysAPILink() string {
  28. return conf.Server.ExternalURL + "api/v1/user/keys/"
  29. }
  30. func listPublicKeys(c *context.APIContext, uid int64) {
  31. keys, err := db.ListPublicKeys(uid)
  32. if err != nil {
  33. c.Error(err, "list public keys")
  34. return
  35. }
  36. apiLink := composePublicKeysAPILink()
  37. apiKeys := make([]*api.PublicKey, len(keys))
  38. for i := range keys {
  39. apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
  40. }
  41. c.JSONSuccess(&apiKeys)
  42. }
  43. func ListMyPublicKeys(c *context.APIContext) {
  44. listPublicKeys(c, c.User.ID)
  45. }
  46. func ListPublicKeys(c *context.APIContext) {
  47. user := GetUserByParams(c)
  48. if c.Written() {
  49. return
  50. }
  51. listPublicKeys(c, user.ID)
  52. }
  53. func GetPublicKey(c *context.APIContext) {
  54. key, err := db.GetPublicKeyByID(c.ParamsInt64(":id"))
  55. if err != nil {
  56. c.NotFoundOrError(err, "get public key by ID")
  57. return
  58. }
  59. apiLink := composePublicKeysAPILink()
  60. c.JSONSuccess(convert.ToPublicKey(apiLink, key))
  61. }
  62. // CreateUserPublicKey creates new public key to given user by ID.
  63. func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
  64. content, err := db.CheckPublicKeyString(form.Key)
  65. if err != nil {
  66. repo.HandleCheckKeyStringError(c, err)
  67. return
  68. }
  69. key, err := db.AddPublicKey(uid, form.Title, content)
  70. if err != nil {
  71. repo.HandleAddKeyError(c, err)
  72. return
  73. }
  74. apiLink := composePublicKeysAPILink()
  75. c.JSON(http.StatusCreated, convert.ToPublicKey(apiLink, key))
  76. }
  77. func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
  78. CreateUserPublicKey(c, form, c.User.ID)
  79. }
  80. func DeletePublicKey(c *context.APIContext) {
  81. if err := db.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
  82. if db.IsErrKeyAccessDenied(err) {
  83. c.ErrorStatus(http.StatusForbidden, errors.New("You do not have access to this key."))
  84. } else {
  85. c.Error(err, "delete public key")
  86. }
  87. return
  88. }
  89. c.NoContent()
  90. }