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.

155 lines
4.6 KiB

  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. var err error
  56. if ctx.Data["App"], err = models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{
  57. ID: ctx.ParamsInt64("id"),
  58. Name: form.Name,
  59. RedirectURIs: []string{form.RedirectURI},
  60. UserID: ctx.User.ID,
  61. }); err != nil {
  62. ctx.ServerError("UpdateOAuth2Application", err)
  63. return
  64. }
  65. ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
  66. ctx.HTML(200, tplSettingsOAuthApplications)
  67. }
  68. // OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
  69. func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
  70. ctx.Data["Title"] = ctx.Tr("settings")
  71. ctx.Data["PageIsSettingsApplications"] = true
  72. app, err := models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))
  73. if err != nil {
  74. if models.IsErrOAuthApplicationNotFound(err) {
  75. ctx.NotFound("Application not found", err)
  76. return
  77. }
  78. ctx.ServerError("GetOAuth2ApplicationByID", err)
  79. return
  80. }
  81. if app.UID != ctx.User.ID {
  82. ctx.NotFound("Application not found", nil)
  83. return
  84. }
  85. ctx.Data["App"] = app
  86. ctx.Data["ClientSecret"], err = app.GenerateClientSecret()
  87. if err != nil {
  88. ctx.ServerError("GenerateClientSecret", err)
  89. return
  90. }
  91. ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
  92. ctx.HTML(200, tplSettingsOAuthApplications)
  93. }
  94. // OAuth2ApplicationShow displays the given application
  95. func OAuth2ApplicationShow(ctx *context.Context) {
  96. app, err := models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))
  97. if err != nil {
  98. if models.IsErrOAuthApplicationNotFound(err) {
  99. ctx.NotFound("Application not found", err)
  100. return
  101. }
  102. ctx.ServerError("GetOAuth2ApplicationByID", err)
  103. return
  104. }
  105. if app.UID != ctx.User.ID {
  106. ctx.NotFound("Application not found", nil)
  107. return
  108. }
  109. ctx.Data["App"] = app
  110. ctx.HTML(200, tplSettingsOAuthApplications)
  111. }
  112. // DeleteOAuth2Application deletes the given oauth2 application
  113. func DeleteOAuth2Application(ctx *context.Context) {
  114. if err := models.DeleteOAuth2Application(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
  115. ctx.ServerError("DeleteOAuth2Application", err)
  116. return
  117. }
  118. log.Trace("OAuth2 Application deleted: %s", ctx.User.Name)
  119. ctx.Flash.Success(ctx.Tr("settings.remove_oauth2_application_success"))
  120. ctx.JSON(200, map[string]interface{}{
  121. "redirect": setting.AppSubURL + "/user/settings/applications",
  122. })
  123. }
  124. // RevokeOAuth2Grant revokes the grant with the given id
  125. func RevokeOAuth2Grant(ctx *context.Context) {
  126. if ctx.User.ID == 0 || ctx.QueryInt64("id") == 0 {
  127. ctx.ServerError("RevokeOAuth2Grant", fmt.Errorf("user id or grant id is zero"))
  128. return
  129. }
  130. if err := models.RevokeOAuth2Grant(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
  131. ctx.ServerError("RevokeOAuth2Grant", err)
  132. return
  133. }
  134. ctx.Flash.Success(ctx.Tr("settings.revoke_oauth2_grant_success"))
  135. ctx.JSON(200, map[string]interface{}{
  136. "redirect": setting.AppSubURL + "/user/settings/applications",
  137. })
  138. }