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.

113 lines
2.7 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
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package user
  6. import (
  7. api "code.gitea.io/sdk/gitea"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. )
  11. // ListAccessTokens list all the access tokens
  12. func ListAccessTokens(ctx *context.APIContext) {
  13. // swagger:operation GET /users/{username}/tokens user userGetTokens
  14. // ---
  15. // summary: List the authenticated user's access tokens
  16. // produces:
  17. // - application/json
  18. // parameters:
  19. // - name: username
  20. // in: path
  21. // description: username of user
  22. // type: string
  23. // required: true
  24. // responses:
  25. // "200":
  26. // "$ref": "#/responses/AccessTokenList"
  27. tokens, err := models.ListAccessTokens(ctx.User.ID)
  28. if err != nil {
  29. ctx.Error(500, "ListAccessTokens", err)
  30. return
  31. }
  32. apiTokens := make([]*api.AccessToken, len(tokens))
  33. for i := range tokens {
  34. apiTokens[i] = &api.AccessToken{
  35. ID: tokens[i].ID,
  36. Name: tokens[i].Name,
  37. Sha1: tokens[i].Sha1,
  38. }
  39. }
  40. ctx.JSON(200, &apiTokens)
  41. }
  42. // CreateAccessToken create access tokens
  43. func CreateAccessToken(ctx *context.APIContext, form api.CreateAccessTokenOption) {
  44. // swagger:operation POST /users/{username}/tokens user userCreateToken
  45. // ---
  46. // summary: Create an access token
  47. // consumes:
  48. // - application/json
  49. // produces:
  50. // - application/json
  51. // parameters:
  52. // - name: username
  53. // in: path
  54. // description: username of user
  55. // type: string
  56. // required: true
  57. // responses:
  58. // "200":
  59. // "$ref": "#/responses/AccessToken"
  60. t := &models.AccessToken{
  61. UID: ctx.User.ID,
  62. Name: form.Name,
  63. }
  64. if err := models.NewAccessToken(t); err != nil {
  65. ctx.Error(500, "NewAccessToken", err)
  66. return
  67. }
  68. ctx.JSON(201, &api.AccessToken{
  69. Name: t.Name,
  70. Sha1: t.Sha1,
  71. ID: t.ID,
  72. })
  73. }
  74. // DeleteAccessToken delete access tokens
  75. func DeleteAccessToken(ctx *context.APIContext) {
  76. // swagger:operation DELETE /users/{username}/tokens/{token} user userDeleteAccessToken
  77. // ---
  78. // summary: delete an access token
  79. // produces:
  80. // - application/json
  81. // parameters:
  82. // - name: username
  83. // in: path
  84. // description: username of user
  85. // type: string
  86. // required: true
  87. // - name: token
  88. // in: path
  89. // description: token to be deleted
  90. // type: integer
  91. // required: true
  92. // responses:
  93. // "204":
  94. // "$ref": "#/responses/empty"
  95. tokenID := ctx.ParamsInt64(":id")
  96. if err := models.DeleteAccessTokenByID(tokenID, ctx.User.ID); err != nil {
  97. if models.IsErrAccessTokenNotExist(err) {
  98. ctx.Status(404)
  99. } else {
  100. ctx.Error(500, "DeleteAccessTokenByID", err)
  101. }
  102. return
  103. }
  104. ctx.Status(204)
  105. }