app.go 967 B

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. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "github.com/gogs/gogs/models"
  9. "github.com/gogs/gogs/pkg/context"
  10. )
  11. func ListAccessTokens(c *context.APIContext) {
  12. tokens, err := models.ListAccessTokens(c.User.ID)
  13. if err != nil {
  14. c.ServerError("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.JSONSuccess(&apiTokens)
  22. }
  23. func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) {
  24. t := &models.AccessToken{
  25. UID: c.User.ID,
  26. Name: form.Name,
  27. }
  28. if err := models.NewAccessToken(t); err != nil {
  29. c.ServerError("NewAccessToken", err)
  30. return
  31. }
  32. c.JSON(http.StatusCreated, &api.AccessToken{t.Name, t.Sha1})
  33. }