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.

setting_oauth2.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package org
  4. import (
  5. "fmt"
  6. "net/http"
  7. "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/setting"
  11. shared_user "code.gitea.io/gitea/routers/web/shared/user"
  12. user_setting "code.gitea.io/gitea/routers/web/user/setting"
  13. )
  14. const (
  15. tplSettingsApplications base.TplName = "org/settings/applications"
  16. tplSettingsOAuthApplicationEdit base.TplName = "org/settings/applications_oauth2_edit"
  17. )
  18. func newOAuth2CommonHandlers(org *context.Organization) *user_setting.OAuth2CommonHandlers {
  19. return &user_setting.OAuth2CommonHandlers{
  20. OwnerID: org.Organization.ID,
  21. BasePathList: fmt.Sprintf("%s/org/%s/settings/applications", setting.AppSubURL, org.Organization.Name),
  22. BasePathEditPrefix: fmt.Sprintf("%s/org/%s/settings/applications/oauth2", setting.AppSubURL, org.Organization.Name),
  23. TplAppEdit: tplSettingsOAuthApplicationEdit,
  24. }
  25. }
  26. // Applications render org applications page (for org, at the moment, there are only OAuth2 applications)
  27. func Applications(ctx *context.Context) {
  28. ctx.Data["Title"] = ctx.Tr("settings.applications")
  29. ctx.Data["PageIsOrgSettings"] = true
  30. ctx.Data["PageIsSettingsApplications"] = true
  31. apps, err := auth.GetOAuth2ApplicationsByUserID(ctx, ctx.Org.Organization.ID)
  32. if err != nil {
  33. ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
  34. return
  35. }
  36. ctx.Data["Applications"] = apps
  37. err = shared_user.LoadHeaderCount(ctx)
  38. if err != nil {
  39. ctx.ServerError("LoadHeaderCount", err)
  40. return
  41. }
  42. ctx.HTML(http.StatusOK, tplSettingsApplications)
  43. }
  44. // OAuthApplicationsPost response for adding an oauth2 application
  45. func OAuthApplicationsPost(ctx *context.Context) {
  46. ctx.Data["Title"] = ctx.Tr("settings.applications")
  47. ctx.Data["PageIsOrgSettings"] = true
  48. ctx.Data["PageIsSettingsApplications"] = true
  49. oa := newOAuth2CommonHandlers(ctx.Org)
  50. oa.AddApp(ctx)
  51. }
  52. // OAuth2ApplicationShow displays the given application
  53. func OAuth2ApplicationShow(ctx *context.Context) {
  54. ctx.Data["PageIsOrgSettings"] = true
  55. ctx.Data["PageIsSettingsApplications"] = true
  56. oa := newOAuth2CommonHandlers(ctx.Org)
  57. oa.EditShow(ctx)
  58. }
  59. // OAuth2ApplicationEdit response for editing oauth2 application
  60. func OAuth2ApplicationEdit(ctx *context.Context) {
  61. ctx.Data["Title"] = ctx.Tr("settings.applications")
  62. ctx.Data["PageIsOrgSettings"] = true
  63. ctx.Data["PageIsSettingsApplications"] = true
  64. oa := newOAuth2CommonHandlers(ctx.Org)
  65. oa.EditSave(ctx)
  66. }
  67. // OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
  68. func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
  69. ctx.Data["Title"] = ctx.Tr("settings")
  70. ctx.Data["PageIsOrgSettings"] = true
  71. ctx.Data["PageIsSettingsApplications"] = true
  72. oa := newOAuth2CommonHandlers(ctx.Org)
  73. oa.RegenerateSecret(ctx)
  74. }
  75. // DeleteOAuth2Application deletes the given oauth2 application
  76. func DeleteOAuth2Application(ctx *context.Context) {
  77. oa := newOAuth2CommonHandlers(ctx.Org)
  78. oa.DeleteApp(ctx)
  79. }
  80. // TODO: revokes the grant with the given id