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.

64 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:operation GET /users/{username}/tokens user userGetTokens
  13. // ---
  14. // summary: List the authenticated user's access tokens
  15. // produces:
  16. // - application/json
  17. // responses:
  18. // "200":
  19. // "$ref": "#/responses/AccessTokenList"
  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:operation POST /users/{username}/tokens user userCreateToken
  37. // ---
  38. // summary: Create an access token
  39. // consumes:
  40. // - application/json
  41. // produces:
  42. // - application/json
  43. // responses:
  44. // "200":
  45. // "$ref": "#/responses/AccessToken"
  46. t := &models.AccessToken{
  47. UID: ctx.User.ID,
  48. Name: form.Name,
  49. }
  50. if err := models.NewAccessToken(t); err != nil {
  51. ctx.Error(500, "NewAccessToken", err)
  52. return
  53. }
  54. ctx.JSON(201, &api.AccessToken{
  55. Name: t.Name,
  56. Sha1: t.Sha1,
  57. })
  58. }