本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

160 lines
4.8KB

  1. // Copyright 2019 The Gitea 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 setting
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. const (
  15. tplSettingsOAuthApplications base.TplName = "user/settings/applications_oauth2_edit"
  16. )
  17. // OAuthApplicationsPost response for adding a oauth2 application
  18. func OAuthApplicationsPost(ctx *context.Context, form auth.EditOAuth2ApplicationForm) {
  19. ctx.Data["Title"] = ctx.Tr("settings")
  20. ctx.Data["PageIsSettingsApplications"] = true
  21. if ctx.HasError() {
  22. loadApplicationsData(ctx)
  23. ctx.HTML(200, tplSettingsApplications)
  24. return
  25. }
  26. // TODO validate redirect URI
  27. app, err := models.CreateOAuth2Application(models.CreateOAuth2ApplicationOptions{
  28. Name: form.Name,
  29. RedirectURIs: []string{form.RedirectURI},
  30. UserID: ctx.User.ID,
  31. })
  32. if err != nil {
  33. ctx.ServerError("CreateOAuth2Application", err)
  34. return
  35. }
  36. ctx.Flash.Success(ctx.Tr("settings.create_oauth2_application_success"))
  37. ctx.Data["App"] = app
  38. ctx.Data["ClientSecret"], err = app.GenerateClientSecret()
  39. if err != nil {
  40. ctx.ServerError("GenerateClientSecret", err)
  41. return
  42. }
  43. ctx.HTML(200, tplSettingsOAuthApplications)
  44. }
  45. // OAuthApplicationsEdit response for editing oauth2 application
  46. func OAuthApplicationsEdit(ctx *context.Context, form auth.EditOAuth2ApplicationForm) {
  47. ctx.Data["Title"] = ctx.Tr("settings")
  48. ctx.Data["PageIsSettingsApplications"] = true
  49. if ctx.HasError() {
  50. loadApplicationsData(ctx)
  51. ctx.HTML(200, tplSettingsApplications)
  52. return
  53. }
  54. // TODO validate redirect URI
  55. if err := models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{
  56. ID: ctx.ParamsInt64("id"),
  57. Name: form.Name,
  58. RedirectURIs: []string{form.RedirectURI},
  59. UserID: ctx.User.ID,
  60. }); err != nil {
  61. ctx.ServerError("UpdateOAuth2Application", err)
  62. return
  63. }
  64. var err error
  65. if ctx.Data["App"], err = models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id")); err != nil {
  66. ctx.ServerError("GetOAuth2ApplicationByID", err)
  67. return
  68. }
  69. ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
  70. ctx.HTML(200, tplSettingsOAuthApplications)
  71. }
  72. // OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
  73. func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
  74. ctx.Data["Title"] = ctx.Tr("settings")
  75. ctx.Data["PageIsSettingsApplications"] = true
  76. app, err := models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))
  77. if err != nil {
  78. if models.IsErrOAuthApplicationNotFound(err) {
  79. ctx.NotFound("Application not found", err)
  80. return
  81. }
  82. ctx.ServerError("GetOAuth2ApplicationByID", err)
  83. return
  84. }
  85. if app.UID != ctx.User.ID {
  86. ctx.NotFound("Application not found", nil)
  87. return
  88. }
  89. ctx.Data["App"] = app
  90. ctx.Data["ClientSecret"], err = app.GenerateClientSecret()
  91. if err != nil {
  92. ctx.ServerError("GenerateClientSecret", err)
  93. return
  94. }
  95. ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
  96. ctx.HTML(200, tplSettingsOAuthApplications)
  97. }
  98. // OAuth2ApplicationShow displays the given application
  99. func OAuth2ApplicationShow(ctx *context.Context) {
  100. app, err := models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))
  101. if err != nil {
  102. if models.IsErrOAuthApplicationNotFound(err) {
  103. ctx.NotFound("Application not found", err)
  104. return
  105. }
  106. ctx.ServerError("GetOAuth2ApplicationByID", err)
  107. return
  108. }
  109. if app.UID != ctx.User.ID {
  110. ctx.NotFound("Application not found", nil)
  111. return
  112. }
  113. ctx.Data["App"] = app
  114. ctx.HTML(200, tplSettingsOAuthApplications)
  115. }
  116. // DeleteOAuth2Application deletes the given oauth2 application
  117. func DeleteOAuth2Application(ctx *context.Context) {
  118. if err := models.DeleteOAuth2Application(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
  119. ctx.ServerError("DeleteOAuth2Application", err)
  120. return
  121. }
  122. log.Trace("OAuth2 Application deleted: %s", ctx.User.Name)
  123. ctx.Flash.Success(ctx.Tr("settings.remove_oauth2_application_success"))
  124. ctx.JSON(200, map[string]interface{}{
  125. "redirect": setting.AppSubURL + "/user/settings/applications",
  126. })
  127. }
  128. // RevokeOAuth2Grant revokes the grant with the given id
  129. func RevokeOAuth2Grant(ctx *context.Context) {
  130. if ctx.User.ID == 0 || ctx.QueryInt64("id") == 0 {
  131. ctx.ServerError("RevokeOAuth2Grant", fmt.Errorf("user id or grant id is zero"))
  132. return
  133. }
  134. if err := models.RevokeOAuth2Grant(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
  135. ctx.ServerError("RevokeOAuth2Grant", err)
  136. return
  137. }
  138. ctx.Flash.Success(ctx.Tr("settings.revoke_oauth2_grant_success"))
  139. ctx.JSON(200, map[string]interface{}{
  140. "redirect": setting.AppSubURL + "/user/settings/applications",
  141. })
  142. }
上海开阖软件有限公司 沪ICP备12045867号-1