user_app.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 v1
  5. import (
  6. api "github.com/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/middleware"
  9. )
  10. // GET /users/:username/tokens
  11. func ListAccessTokens(ctx *middleware.Context) {
  12. tokens, err := models.ListAccessTokens(ctx.User.Id)
  13. if err != nil {
  14. ctx.APIError(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. type CreateAccessTokenForm struct {
  24. Name string `json:"name" binding:"Required"`
  25. }
  26. // POST /users/:username/tokens
  27. func CreateAccessToken(ctx *middleware.Context, form CreateAccessTokenForm) {
  28. t := &models.AccessToken{
  29. UID: ctx.User.Id,
  30. Name: form.Name,
  31. }
  32. if err := models.NewAccessToken(t); err != nil {
  33. ctx.APIError(500, "NewAccessToken", err)
  34. return
  35. }
  36. ctx.JSON(201, &api.AccessToken{t.Name, t.Sha1})
  37. }