gitea源码

oauth2_common.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "fmt"
  6. "net/http"
  7. "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/modules/templates"
  9. "code.gitea.io/gitea/modules/util"
  10. "code.gitea.io/gitea/modules/web"
  11. shared_user "code.gitea.io/gitea/routers/web/shared/user"
  12. "code.gitea.io/gitea/services/context"
  13. "code.gitea.io/gitea/services/forms"
  14. )
  15. type OAuth2CommonHandlers struct {
  16. OwnerID int64 // 0 for instance-wide, otherwise OrgID or UserID
  17. BasePathList string // the base URL for the application list page, eg: "/user/setting/applications"
  18. BasePathEditPrefix string // the base URL for the application edit page, will be appended with app id, eg: "/user/setting/applications/oauth2"
  19. TplAppEdit templates.TplName // the template for the application edit page
  20. }
  21. func (oa *OAuth2CommonHandlers) renderEditPage(ctx *context.Context) {
  22. app := ctx.Data["App"].(*auth.OAuth2Application)
  23. ctx.Data["FormActionPath"] = fmt.Sprintf("%s/%d", oa.BasePathEditPrefix, app.ID)
  24. if ctx.ContextUser != nil && ctx.ContextUser.IsOrganization() {
  25. if _, err := shared_user.RenderUserOrgHeader(ctx); err != nil {
  26. ctx.ServerError("RenderUserOrgHeader", err)
  27. return
  28. }
  29. }
  30. ctx.HTML(http.StatusOK, oa.TplAppEdit)
  31. }
  32. // AddApp adds an oauth2 application
  33. func (oa *OAuth2CommonHandlers) AddApp(ctx *context.Context) {
  34. form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
  35. if ctx.HasError() {
  36. ctx.Flash.Error(ctx.GetErrMsg())
  37. // go to the application list page
  38. ctx.Redirect(oa.BasePathList)
  39. return
  40. }
  41. app, err := auth.CreateOAuth2Application(ctx, auth.CreateOAuth2ApplicationOptions{
  42. Name: form.Name,
  43. RedirectURIs: util.SplitTrimSpace(form.RedirectURIs, "\n"),
  44. UserID: oa.OwnerID,
  45. ConfidentialClient: form.ConfidentialClient,
  46. SkipSecondaryAuthorization: form.SkipSecondaryAuthorization,
  47. })
  48. if err != nil {
  49. ctx.ServerError("CreateOAuth2Application", err)
  50. return
  51. }
  52. // render the edit page with secret
  53. ctx.Flash.Success(ctx.Tr("settings.create_oauth2_application_success"), true)
  54. ctx.Data["App"] = app
  55. ctx.Data["ClientSecret"], err = app.GenerateClientSecret(ctx)
  56. if err != nil {
  57. ctx.ServerError("GenerateClientSecret", err)
  58. return
  59. }
  60. oa.renderEditPage(ctx)
  61. }
  62. // EditShow displays the given application
  63. func (oa *OAuth2CommonHandlers) EditShow(ctx *context.Context) {
  64. app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.PathParamInt64("id"))
  65. if err != nil {
  66. if auth.IsErrOAuthApplicationNotFound(err) {
  67. ctx.NotFound(err)
  68. return
  69. }
  70. ctx.ServerError("GetOAuth2ApplicationByID", err)
  71. return
  72. }
  73. if app.UID != oa.OwnerID {
  74. ctx.NotFound(nil)
  75. return
  76. }
  77. ctx.Data["App"] = app
  78. oa.renderEditPage(ctx)
  79. }
  80. // EditSave saves the oauth2 application
  81. func (oa *OAuth2CommonHandlers) EditSave(ctx *context.Context) {
  82. form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
  83. if ctx.HasError() {
  84. app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.PathParamInt64("id"))
  85. if err != nil {
  86. if auth.IsErrOAuthApplicationNotFound(err) {
  87. ctx.NotFound(err)
  88. return
  89. }
  90. ctx.ServerError("GetOAuth2ApplicationByID", err)
  91. return
  92. }
  93. if app.UID != oa.OwnerID {
  94. ctx.NotFound(nil)
  95. return
  96. }
  97. ctx.Data["App"] = app
  98. oa.renderEditPage(ctx)
  99. return
  100. }
  101. var err error
  102. if ctx.Data["App"], err = auth.UpdateOAuth2Application(ctx, auth.UpdateOAuth2ApplicationOptions{
  103. ID: ctx.PathParamInt64("id"),
  104. Name: form.Name,
  105. RedirectURIs: util.SplitTrimSpace(form.RedirectURIs, "\n"),
  106. UserID: oa.OwnerID,
  107. ConfidentialClient: form.ConfidentialClient,
  108. SkipSecondaryAuthorization: form.SkipSecondaryAuthorization,
  109. }); err != nil {
  110. ctx.ServerError("UpdateOAuth2Application", err)
  111. return
  112. }
  113. ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
  114. ctx.Redirect(oa.BasePathList)
  115. }
  116. // RegenerateSecret regenerates the secret
  117. func (oa *OAuth2CommonHandlers) RegenerateSecret(ctx *context.Context) {
  118. app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.PathParamInt64("id"))
  119. if err != nil {
  120. if auth.IsErrOAuthApplicationNotFound(err) {
  121. ctx.NotFound(err)
  122. return
  123. }
  124. ctx.ServerError("GetOAuth2ApplicationByID", err)
  125. return
  126. }
  127. if app.UID != oa.OwnerID {
  128. ctx.NotFound(nil)
  129. return
  130. }
  131. ctx.Data["App"] = app
  132. ctx.Data["ClientSecret"], err = app.GenerateClientSecret(ctx)
  133. if err != nil {
  134. ctx.ServerError("GenerateClientSecret", err)
  135. return
  136. }
  137. ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"), true)
  138. oa.renderEditPage(ctx)
  139. }
  140. // DeleteApp deletes the given oauth2 application
  141. func (oa *OAuth2CommonHandlers) DeleteApp(ctx *context.Context) {
  142. if err := auth.DeleteOAuth2Application(ctx, ctx.PathParamInt64("id"), oa.OwnerID); err != nil {
  143. ctx.ServerError("DeleteOAuth2Application", err)
  144. return
  145. }
  146. ctx.Flash.Success(ctx.Tr("settings.remove_oauth2_application_success"))
  147. ctx.JSONRedirect(oa.BasePathList)
  148. }
  149. // RevokeGrant revokes the grant
  150. func (oa *OAuth2CommonHandlers) RevokeGrant(ctx *context.Context) {
  151. if err := auth.RevokeOAuth2Grant(ctx, ctx.PathParamInt64("grantId"), oa.OwnerID); err != nil {
  152. ctx.ServerError("RevokeOAuth2Grant", err)
  153. return
  154. }
  155. ctx.Flash.Success(ctx.Tr("settings.revoke_oauth2_grant_success"))
  156. ctx.JSONRedirect(oa.BasePathList)
  157. }