Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

oauth2.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "net/http"
  8. "code.gitea.io/gitea/models"
  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. "code.gitea.io/gitea/modules/web"
  14. "code.gitea.io/gitea/services/forms"
  15. )
  16. const (
  17. tplSettingsOAuthApplications base.TplName = "user/settings/applications_oauth2_edit"
  18. )
  19. // OAuthApplicationsPost response for adding a oauth2 application
  20. func OAuthApplicationsPost(ctx *context.Context) {
  21. form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
  22. ctx.Data["Title"] = ctx.Tr("settings")
  23. ctx.Data["PageIsSettingsApplications"] = true
  24. if ctx.HasError() {
  25. loadApplicationsData(ctx)
  26. ctx.HTML(http.StatusOK, tplSettingsApplications)
  27. return
  28. }
  29. // TODO validate redirect URI
  30. app, err := models.CreateOAuth2Application(models.CreateOAuth2ApplicationOptions{
  31. Name: form.Name,
  32. RedirectURIs: []string{form.RedirectURI},
  33. UserID: ctx.User.ID,
  34. })
  35. if err != nil {
  36. ctx.ServerError("CreateOAuth2Application", err)
  37. return
  38. }
  39. ctx.Flash.Success(ctx.Tr("settings.create_oauth2_application_success"))
  40. ctx.Data["App"] = app
  41. ctx.Data["ClientSecret"], err = app.GenerateClientSecret()
  42. if err != nil {
  43. ctx.ServerError("GenerateClientSecret", err)
  44. return
  45. }
  46. ctx.HTML(http.StatusOK, tplSettingsOAuthApplications)
  47. }
  48. // OAuthApplicationsEdit response for editing oauth2 application
  49. func OAuthApplicationsEdit(ctx *context.Context) {
  50. form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
  51. ctx.Data["Title"] = ctx.Tr("settings")
  52. ctx.Data["PageIsSettingsApplications"] = true
  53. if ctx.HasError() {
  54. loadApplicationsData(ctx)
  55. ctx.HTML(http.StatusOK, tplSettingsApplications)
  56. return
  57. }
  58. // TODO validate redirect URI
  59. var err error
  60. if ctx.Data["App"], err = models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{
  61. ID: ctx.ParamsInt64("id"),
  62. Name: form.Name,
  63. RedirectURIs: []string{form.RedirectURI},
  64. UserID: ctx.User.ID,
  65. }); err != nil {
  66. ctx.ServerError("UpdateOAuth2Application", err)
  67. return
  68. }
  69. ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
  70. ctx.HTML(http.StatusOK, 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(http.StatusOK, 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(http.StatusOK, 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(http.StatusOK, 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(http.StatusOK, map[string]interface{}{
  140. "redirect": setting.AppSubURL + "/user/settings/applications",
  141. })
  142. }