app.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. "gogs.io/gogs/internal/context"
  9. "gogs.io/gogs/internal/db"
  10. "gogs.io/gogs/internal/db/errors"
  11. )
  12. func ListAccessTokens(c *context.APIContext) {
  13. tokens, err := db.ListAccessTokens(c.User.ID)
  14. if err != nil {
  15. c.Error(err, "list access tokens")
  16. return
  17. }
  18. apiTokens := make([]*api.AccessToken, len(tokens))
  19. for i := range tokens {
  20. apiTokens[i] = &api.AccessToken{Name: tokens[i].Name, Sha1: tokens[i].Sha1}
  21. }
  22. c.JSONSuccess(&apiTokens)
  23. }
  24. func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) {
  25. t := &db.AccessToken{
  26. UserID: c.User.ID,
  27. Name: form.Name,
  28. }
  29. if err := db.NewAccessToken(t); err != nil {
  30. if errors.IsAccessTokenNameAlreadyExist(err) {
  31. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  32. } else {
  33. c.Error(err, "new access token")
  34. }
  35. return
  36. }
  37. c.JSON(http.StatusCreated, &api.AccessToken{Name: t.Name, Sha1: t.Sha1})
  38. }