You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

10 years ago
  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/base"
  9. "github.com/gogits/gogs/modules/middleware"
  10. )
  11. // GET /users/:username/tokens
  12. func ListAccessTokens(ctx *middleware.Context) {
  13. tokens, err := models.ListAccessTokens(ctx.User.Id)
  14. if err != nil {
  15. ctx.JSON(500, &base.ApiJsonErr{"ListAccessTokens: " + err.Error(), base.DOC_URL})
  16. return
  17. }
  18. apiTokens := make([]*api.AccessToken, len(tokens))
  19. for i := range tokens {
  20. apiTokens[i] = &api.AccessToken{tokens[i].Name, tokens[i].Sha1}
  21. }
  22. ctx.JSON(200, &apiTokens)
  23. }
  24. type CreateAccessTokenForm struct {
  25. Name string `json:"name" binding:"Required"`
  26. }
  27. // POST /users/:username/tokens
  28. func CreateAccessToken(ctx *middleware.Context, form CreateAccessTokenForm) {
  29. t := &models.AccessToken{
  30. Uid: ctx.User.Id,
  31. Name: form.Name,
  32. }
  33. if err := models.NewAccessToken(t); err != nil {
  34. ctx.JSON(500, &base.ApiJsonErr{"NewAccessToken: " + err.Error(), base.DOC_URL})
  35. return
  36. }
  37. ctx.JSON(201, &api.AccessToken{t.Name, t.Sha1})
  38. }