access_tokens.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2014 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. gocontext "context"
  7. "net/http"
  8. api "github.com/gogs/go-gogs-client"
  9. "gopkg.in/macaron.v1"
  10. "gogs.io/gogs/internal/context"
  11. "gogs.io/gogs/internal/database"
  12. )
  13. // AccessTokensHandler is the handler for users access tokens API endpoints.
  14. type AccessTokensHandler struct {
  15. store AccessTokensStore
  16. }
  17. // NewAccessTokensHandler returns a new AccessTokensHandler for users access
  18. // tokens API endpoints.
  19. func NewAccessTokensHandler(s AccessTokensStore) *AccessTokensHandler {
  20. return &AccessTokensHandler{
  21. store: s,
  22. }
  23. }
  24. func (h *AccessTokensHandler) List() macaron.Handler {
  25. return func(c *context.APIContext) {
  26. tokens, err := h.store.ListAccessTokens(c.Req.Context(), c.User.ID)
  27. if err != nil {
  28. c.Error(err, "list access tokens")
  29. return
  30. }
  31. apiTokens := make([]*api.AccessToken, len(tokens))
  32. for i := range tokens {
  33. apiTokens[i] = &api.AccessToken{Name: tokens[i].Name, Sha1: tokens[i].Sha1}
  34. }
  35. c.JSONSuccess(&apiTokens)
  36. }
  37. }
  38. func (h *AccessTokensHandler) Create() macaron.Handler {
  39. return func(c *context.APIContext, form api.CreateAccessTokenOption) {
  40. t, err := h.store.CreateAccessToken(c.Req.Context(), c.User.ID, form.Name)
  41. if err != nil {
  42. if database.IsErrAccessTokenAlreadyExist(err) {
  43. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  44. } else {
  45. c.Error(err, "new access token")
  46. }
  47. return
  48. }
  49. c.JSON(http.StatusCreated, &api.AccessToken{Name: t.Name, Sha1: t.Sha1})
  50. }
  51. }
  52. // AccessTokensStore is the data layer carrier for user access tokens API
  53. // endpoints. This interface is meant to abstract away and limit the exposure of
  54. // the underlying data layer to the handler through a thin-wrapper.
  55. type AccessTokensStore interface {
  56. // CreateAccessToken creates a new access token and persist to database. It
  57. // returns database.ErrAccessTokenAlreadyExist when an access token with same
  58. // name already exists for the user.
  59. CreateAccessToken(ctx gocontext.Context, userID int64, name string) (*database.AccessToken, error)
  60. // ListAccessTokens returns all access tokens belongs to given user.
  61. ListAccessTokens(ctx gocontext.Context, userID int64) ([]*database.AccessToken, error)
  62. }
  63. type accessTokensStore struct{}
  64. // NewAccessTokensStore returns a new AccessTokensStore using the global
  65. // database handle.
  66. func NewAccessTokensStore() AccessTokensStore {
  67. return &accessTokensStore{}
  68. }
  69. func (*accessTokensStore) CreateAccessToken(ctx gocontext.Context, userID int64, name string) (*database.AccessToken, error) {
  70. return database.Handle.AccessTokens().Create(ctx, userID, name)
  71. }
  72. func (*accessTokensStore) ListAccessTokens(ctx gocontext.Context, userID int64) ([]*database.AccessToken, error) {
  73. return database.Handle.AccessTokens().List(ctx, userID)
  74. }