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.

67 lines
1.5 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. func ListAccessTokens(ctx *context.APIContext) {
  12. // swagger:route GET /users/{username}/tokens userGetTokens
  13. //
  14. // Produces:
  15. // - application/json
  16. //
  17. // Responses:
  18. // 200: AccessTokenList
  19. // 500: error
  20. tokens, err := models.ListAccessTokens(ctx.User.ID)
  21. if err != nil {
  22. ctx.Error(500, "ListAccessTokens", err)
  23. return
  24. }
  25. apiTokens := make([]*api.AccessToken, len(tokens))
  26. for i := range tokens {
  27. apiTokens[i] = &api.AccessToken{
  28. Name: tokens[i].Name,
  29. Sha1: tokens[i].Sha1,
  30. }
  31. }
  32. ctx.JSON(200, &apiTokens)
  33. }
  34. // CreateAccessToken create access tokens
  35. func CreateAccessToken(ctx *context.APIContext, form api.CreateAccessTokenOption) {
  36. // swagger:route POST /users/{username} /tokens userCreateToken
  37. //
  38. // Consumes:
  39. // - application/json
  40. //
  41. // Produces:
  42. // - application/json
  43. //
  44. // Responses:
  45. // 200: AccessToken
  46. // 500: error
  47. t := &models.AccessToken{
  48. UID: ctx.User.ID,
  49. Name: form.Name,
  50. }
  51. if err := models.NewAccessToken(t); err != nil {
  52. ctx.Error(500, "NewAccessToken", err)
  53. return
  54. }
  55. ctx.JSON(201, &api.AccessToken{
  56. Name: t.Name,
  57. Sha1: t.Sha1,
  58. })
  59. }