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