app.go 1.0 KB

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