app.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package user
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/pkg/context"
  10. api "gitlab.com/gitote/go-gitote-client"
  11. )
  12. // ListAccessTokens list all the access tokens
  13. func ListAccessTokens(c *context.APIContext) {
  14. tokens, err := models.ListAccessTokens(c.User.ID)
  15. if err != nil {
  16. c.Error(500, "ListAccessTokens", err)
  17. return
  18. }
  19. apiTokens := make([]*api.AccessToken, len(tokens))
  20. for i := range tokens {
  21. apiTokens[i] = &api.AccessToken{tokens[i].Name, tokens[i].Sha1}
  22. }
  23. c.JSON(200, &apiTokens)
  24. }
  25. // CreateAccessToken create access tokens
  26. func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) {
  27. t := &models.AccessToken{
  28. UID: c.User.ID,
  29. Name: form.Name,
  30. }
  31. if err := models.NewAccessToken(t); err != nil {
  32. c.Error(500, "NewAccessToken", err)
  33. return
  34. }
  35. c.JSON(201, &api.AccessToken{t.Name, t.Sha1})
  36. }