You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

oauth2.go 4.3 kB

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