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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/routers/api/v1/utils"
  13. )
  14. // ListAccessTokens list all the access tokens
  15. func ListAccessTokens(ctx *context.APIContext) {
  16. // swagger:operation GET /users/{username}/tokens user userGetTokens
  17. // ---
  18. // summary: List the authenticated user's access tokens
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: username
  23. // in: path
  24. // description: username of user
  25. // type: string
  26. // required: true
  27. // - name: page
  28. // in: query
  29. // description: page number of results to return (1-based)
  30. // type: integer
  31. // - name: limit
  32. // in: query
  33. // description: page size of results, maximum page size is 50
  34. // type: integer
  35. // responses:
  36. // "200":
  37. // "$ref": "#/responses/AccessTokenList"
  38. tokens, err := models.ListAccessTokens(ctx.User.ID, utils.GetListOptions(ctx))
  39. if err != nil {
  40. ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err)
  41. return
  42. }
  43. apiTokens := make([]*api.AccessToken, len(tokens))
  44. for i := range tokens {
  45. apiTokens[i] = &api.AccessToken{
  46. ID: tokens[i].ID,
  47. Name: tokens[i].Name,
  48. TokenLastEight: tokens[i].TokenLastEight,
  49. }
  50. }
  51. ctx.JSON(http.StatusOK, &apiTokens)
  52. }
  53. // CreateAccessToken create access tokens
  54. func CreateAccessToken(ctx *context.APIContext, form api.CreateAccessTokenOption) {
  55. // swagger:operation POST /users/{username}/tokens user userCreateToken
  56. // ---
  57. // summary: Create an access token
  58. // consumes:
  59. // - application/json
  60. // produces:
  61. // - application/json
  62. // parameters:
  63. // - name: username
  64. // in: path
  65. // description: username of user
  66. // type: string
  67. // required: true
  68. // - name: accessToken
  69. // in: body
  70. // schema:
  71. // type: object
  72. // required:
  73. // - name
  74. // properties:
  75. // name:
  76. // type: string
  77. // responses:
  78. // "200":
  79. // "$ref": "#/responses/AccessToken"
  80. t := &models.AccessToken{
  81. UID: ctx.User.ID,
  82. Name: form.Name,
  83. }
  84. if err := models.NewAccessToken(t); err != nil {
  85. ctx.Error(http.StatusInternalServerError, "NewAccessToken", err)
  86. return
  87. }
  88. ctx.JSON(http.StatusCreated, &api.AccessToken{
  89. Name: t.Name,
  90. Token: t.Token,
  91. ID: t.ID,
  92. TokenLastEight: t.TokenLastEight,
  93. })
  94. }
  95. // DeleteAccessToken delete access tokens
  96. func DeleteAccessToken(ctx *context.APIContext) {
  97. // swagger:operation DELETE /users/{username}/tokens/{token} user userDeleteAccessToken
  98. // ---
  99. // summary: delete an access token
  100. // produces:
  101. // - application/json
  102. // parameters:
  103. // - name: username
  104. // in: path
  105. // description: username of user
  106. // type: string
  107. // required: true
  108. // - name: token
  109. // in: path
  110. // description: token to be deleted
  111. // type: integer
  112. // format: int64
  113. // required: true
  114. // responses:
  115. // "204":
  116. // "$ref": "#/responses/empty"
  117. tokenID := ctx.ParamsInt64(":id")
  118. if err := models.DeleteAccessTokenByID(tokenID, ctx.User.ID); err != nil {
  119. if models.IsErrAccessTokenNotExist(err) {
  120. ctx.NotFound()
  121. } else {
  122. ctx.Error(http.StatusInternalServerError, "DeleteAccessTokenByID", err)
  123. }
  124. return
  125. }
  126. ctx.Status(http.StatusNoContent)
  127. }
  128. // CreateOauth2Application is the handler to create a new OAuth2 Application for the authenticated user
  129. func CreateOauth2Application(ctx *context.APIContext, data api.CreateOAuth2ApplicationOptions) {
  130. // swagger:operation POST /user/applications/oauth2 user userCreateOAuth2Application
  131. // ---
  132. // summary: creates a new OAuth2 application
  133. // produces:
  134. // - application/json
  135. // parameters:
  136. // - name: body
  137. // in: body
  138. // required: true
  139. // schema:
  140. // "$ref": "#/definitions/CreateOAuth2ApplicationOptions"
  141. // responses:
  142. // "201":
  143. // "$ref": "#/responses/OAuth2Application"
  144. app, err := models.CreateOAuth2Application(models.CreateOAuth2ApplicationOptions{
  145. Name: data.Name,
  146. UserID: ctx.User.ID,
  147. RedirectURIs: data.RedirectURIs,
  148. })
  149. if err != nil {
  150. ctx.Error(http.StatusBadRequest, "", "error creating oauth2 application")
  151. return
  152. }
  153. secret, err := app.GenerateClientSecret()
  154. if err != nil {
  155. ctx.Error(http.StatusBadRequest, "", "error creating application secret")
  156. return
  157. }
  158. app.ClientSecret = secret
  159. ctx.JSON(http.StatusCreated, convert.ToOAuth2Application(app))
  160. }
  161. // ListOauth2Applications list all the Oauth2 application
  162. func ListOauth2Applications(ctx *context.APIContext) {
  163. // swagger:operation GET /user/applications/oauth2 user userGetOauth2Application
  164. // ---
  165. // summary: List the authenticated user's oauth2 applications
  166. // produces:
  167. // - application/json
  168. // parameters:
  169. // - name: page
  170. // in: query
  171. // description: page number of results to return (1-based)
  172. // type: integer
  173. // - name: limit
  174. // in: query
  175. // description: page size of results, maximum page size is 50
  176. // type: integer
  177. // responses:
  178. // "200":
  179. // "$ref": "#/responses/OAuth2ApplicationList"
  180. apps, err := models.ListOAuth2Applications(ctx.User.ID, utils.GetListOptions(ctx))
  181. if err != nil {
  182. ctx.Error(http.StatusInternalServerError, "ListOAuth2Applications", err)
  183. return
  184. }
  185. apiApps := make([]*api.OAuth2Application, len(apps))
  186. for i := range apps {
  187. apiApps[i] = convert.ToOAuth2Application(apps[i])
  188. apiApps[i].ClientSecret = "" // Hide secret on application list
  189. }
  190. ctx.JSON(http.StatusOK, &apiApps)
  191. }
  192. // DeleteOauth2Application delete OAuth2 Application
  193. func DeleteOauth2Application(ctx *context.APIContext) {
  194. // swagger:operation DELETE /user/applications/oauth2/{id} user userDeleteOAuth2Application
  195. // ---
  196. // summary: delete an OAuth2 Application
  197. // produces:
  198. // - application/json
  199. // parameters:
  200. // - name: id
  201. // in: path
  202. // description: token to be deleted
  203. // type: integer
  204. // format: int64
  205. // required: true
  206. // responses:
  207. // "204":
  208. // "$ref": "#/responses/empty"
  209. appID := ctx.ParamsInt64(":id")
  210. if err := models.DeleteOAuth2Application(appID, ctx.User.ID); err != nil {
  211. ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err)
  212. return
  213. }
  214. ctx.Status(http.StatusNoContent)
  215. }