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 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. user_setting "code.gitea.io/gitea/routers/web/user/setting"
  12. )
  13. const (
  14. tplSettingsApplications base.TplName = "org/settings/applications"
  15. tplSettingsOAuthApplicationEdit base.TplName = "org/settings/applications_oauth2_edit"
  16. )
  17. func newOAuth2CommonHandlers(org *context.Organization) *user_setting.OAuth2CommonHandlers {
  18. return &user_setting.OAuth2CommonHandlers{
  19. OwnerID: org.Organization.ID,
  20. BasePathList: fmt.Sprintf("%s/org/%s/settings/applications", setting.AppSubURL, org.Organization.Name),
  21. BasePathEditPrefix: fmt.Sprintf("%s/org/%s/settings/applications/oauth2", setting.AppSubURL, org.Organization.Name),
  22. TplAppEdit: tplSettingsOAuthApplicationEdit,
  23. }
  24. }
  25. // Applications render org applications page (for org, at the moment, there are only OAuth2 applications)
  26. func Applications(ctx *context.Context) {
  27. ctx.Data["Title"] = ctx.Tr("settings.applications")
  28. ctx.Data["PageIsOrgSettings"] = true
  29. ctx.Data["PageIsSettingsApplications"] = true
  30. apps, err := auth.GetOAuth2ApplicationsByUserID(ctx, ctx.Org.Organization.ID)
  31. if err != nil {
  32. ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
  33. return
  34. }
  35. ctx.Data["Applications"] = apps
  36. ctx.HTML(http.StatusOK, tplSettingsApplications)
  37. }
  38. // OAuthApplicationsPost response for adding an oauth2 application
  39. func OAuthApplicationsPost(ctx *context.Context) {
  40. ctx.Data["Title"] = ctx.Tr("settings.applications")
  41. ctx.Data["PageIsOrgSettings"] = true
  42. ctx.Data["PageIsSettingsApplications"] = true
  43. oa := newOAuth2CommonHandlers(ctx.Org)
  44. oa.AddApp(ctx)
  45. }
  46. // OAuth2ApplicationShow displays the given application
  47. func OAuth2ApplicationShow(ctx *context.Context) {
  48. ctx.Data["PageIsOrgSettings"] = true
  49. ctx.Data["PageIsSettingsApplications"] = true
  50. oa := newOAuth2CommonHandlers(ctx.Org)
  51. oa.EditShow(ctx)
  52. }
  53. // OAuth2ApplicationEdit response for editing oauth2 application
  54. func OAuth2ApplicationEdit(ctx *context.Context) {
  55. ctx.Data["Title"] = ctx.Tr("settings.applications")
  56. ctx.Data["PageIsOrgSettings"] = true
  57. ctx.Data["PageIsSettingsApplications"] = true
  58. oa := newOAuth2CommonHandlers(ctx.Org)
  59. oa.EditSave(ctx)
  60. }
  61. // OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
  62. func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
  63. ctx.Data["Title"] = ctx.Tr("settings")
  64. ctx.Data["PageIsOrgSettings"] = true
  65. ctx.Data["PageIsSettingsApplications"] = true
  66. oa := newOAuth2CommonHandlers(ctx.Org)
  67. oa.RegenerateSecret(ctx)
  68. }
  69. // DeleteOAuth2Application deletes the given oauth2 application
  70. func DeleteOAuth2Application(ctx *context.Context) {
  71. oa := newOAuth2CommonHandlers(ctx.Org)
  72. oa.DeleteApp(ctx)
  73. }
  74. // TODO: revokes the grant with the given id