app.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. api "github.com/gogs/go-gogs-client"
  7. "github.com/gogs/gogs/models"
  8. "github.com/gogs/gogs/pkg/context"
  9. )
  10. // https://github.com/gogs/go-gogs-client/wiki/Users#list-access-tokens-for-a-user
  11. func ListAccessTokens(c *context.APIContext) {
  12. tokens, err := models.ListAccessTokens(c.User.ID)
  13. if err != nil {
  14. c.Error(500, "ListAccessTokens", err)
  15. return
  16. }
  17. apiTokens := make([]*api.AccessToken, len(tokens))
  18. for i := range tokens {
  19. apiTokens[i] = &api.AccessToken{tokens[i].Name, tokens[i].Sha1}
  20. }
  21. c.JSON(200, &apiTokens)
  22. }
  23. // https://github.com/gogs/go-gogs-client/wiki/Users#create-a-access-token
  24. func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) {
  25. t := &models.AccessToken{
  26. UID: c.User.ID,
  27. Name: form.Name,
  28. }
  29. if err := models.NewAccessToken(t); err != nil {
  30. c.Error(500, "NewAccessToken", err)
  31. return
  32. }
  33. c.JSON(201, &api.AccessToken{t.Name, t.Sha1})
  34. }