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.

48 lines
1.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
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 user
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. )
  10. // ListAccessTokens list all the access tokens
  11. // see https://github.com/gogits/go-gogs-client/wiki/Users#list-access-tokens-for-a-user
  12. func ListAccessTokens(ctx *context.APIContext) {
  13. tokens, err := models.ListAccessTokens(ctx.User.ID)
  14. if err != nil {
  15. ctx.Error(500, "ListAccessTokens", err)
  16. return
  17. }
  18. apiTokens := make([]*api.AccessToken, len(tokens))
  19. for i := range tokens {
  20. apiTokens[i] = &api.AccessToken{
  21. Name: tokens[i].Name,
  22. Sha1: tokens[i].Sha1,
  23. }
  24. }
  25. ctx.JSON(200, &apiTokens)
  26. }
  27. // CreateAccessToken create access tokens
  28. // see https://github.com/gogits/go-gogs-client/wiki/Users#create-a-access-token
  29. func CreateAccessToken(ctx *context.APIContext, form api.CreateAccessTokenOption) {
  30. t := &models.AccessToken{
  31. UID: ctx.User.ID,
  32. Name: form.Name,
  33. }
  34. if err := models.NewAccessToken(t); err != nil {
  35. ctx.Error(500, "NewAccessToken", err)
  36. return
  37. }
  38. ctx.JSON(201, &api.AccessToken{
  39. Name: t.Name,
  40. Sha1: t.Sha1,
  41. })
  42. }