|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package shared |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/modules/log" |
| 11 | + "code.gitea.io/gitea/modules/setting" |
| 12 | + "code.gitea.io/gitea/modules/web/middleware" |
| 13 | + "code.gitea.io/gitea/routers/common" |
| 14 | + "code.gitea.io/gitea/services/context" |
| 15 | +) |
| 16 | + |
| 17 | +func SitemapEnabled(ctx *context.Context) { |
| 18 | + if !setting.Other.EnableSitemap { |
| 19 | + ctx.HTTPError(http.StatusNotFound) |
| 20 | + return |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// verifyAuthWithOptions checks authentication according to options |
| 25 | +func verifyAuthWithOptions(options *common.VerifyOptions) func(ctx *context.Context) { |
| 26 | + return func(ctx *context.Context) { |
| 27 | + // Check prohibit login users. |
| 28 | + if ctx.IsSigned { |
| 29 | + if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm { |
| 30 | + ctx.Data["Title"] = ctx.Tr("auth.active_your_account") |
| 31 | + ctx.HTML(http.StatusOK, "user/auth/activate") |
| 32 | + return |
| 33 | + } |
| 34 | + if !ctx.Doer.IsActive || ctx.Doer.ProhibitLogin { |
| 35 | + log.Info("Failed authentication attempt for %s from %s", ctx.Doer.Name, ctx.RemoteAddr()) |
| 36 | + ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") |
| 37 | + ctx.HTML(http.StatusOK, "user/auth/prohibit_login") |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if ctx.Doer.MustChangePassword { |
| 42 | + if ctx.Req.URL.Path != "/user/settings/change_password" { |
| 43 | + if strings.HasPrefix(ctx.Req.UserAgent(), "git") { |
| 44 | + ctx.HTTPError(http.StatusUnauthorized, ctx.Locale.TrString("auth.must_change_password")) |
| 45 | + return |
| 46 | + } |
| 47 | + ctx.Data["Title"] = ctx.Tr("auth.must_change_password") |
| 48 | + ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password" |
| 49 | + if ctx.Req.URL.Path != "/user/events" { |
| 50 | + middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI()) |
| 51 | + } |
| 52 | + ctx.Redirect(setting.AppSubURL + "/user/settings/change_password") |
| 53 | + return |
| 54 | + } |
| 55 | + } else if ctx.Req.URL.Path == "/user/settings/change_password" { |
| 56 | + // make sure that the form cannot be accessed by users who don't need this |
| 57 | + ctx.Redirect(setting.AppSubURL + "/") |
| 58 | + return |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Redirect to dashboard (or alternate location) if user tries to visit any non-login page. |
| 63 | + if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" { |
| 64 | + ctx.RedirectToCurrentSite(ctx.FormString("redirect_to")) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == http.MethodPost { |
| 69 | + ctx.Csrf.Validate(ctx) |
| 70 | + if ctx.Written() { |
| 71 | + return |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if options.SignInRequired { |
| 76 | + if !ctx.IsSigned { |
| 77 | + if ctx.Req.URL.Path != "/user/events" { |
| 78 | + middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI()) |
| 79 | + } |
| 80 | + ctx.Redirect(setting.AppSubURL + "/user/login") |
| 81 | + return |
| 82 | + } else if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm { |
| 83 | + ctx.Data["Title"] = ctx.Tr("auth.active_your_account") |
| 84 | + ctx.HTML(http.StatusOK, "user/auth/activate") |
| 85 | + return |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Redirect to log in page if auto-signin info is provided and has not signed in. |
| 90 | + if !options.SignOutRequired && !ctx.IsSigned && |
| 91 | + ctx.GetSiteCookie(setting.CookieRememberName) != "" { |
| 92 | + if ctx.Req.URL.Path != "/user/events" { |
| 93 | + middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI()) |
| 94 | + } |
| 95 | + ctx.Redirect(setting.AppSubURL + "/user/login") |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + if options.AdminRequired { |
| 100 | + if !ctx.Doer.IsAdmin { |
| 101 | + ctx.HTTPError(http.StatusForbidden) |
| 102 | + return |
| 103 | + } |
| 104 | + ctx.Data["PageIsAdmin"] = true |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +var ( |
| 110 | + OptSignInIgnoreCsrf = verifyAuthWithOptions(&common.VerifyOptions{DisableCSRF: true}) |
| 111 | + |
| 112 | + // required to be signed in or signed out |
| 113 | + ReqSignIn = verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: true}) |
| 114 | + ReqSignOut = verifyAuthWithOptions(&common.VerifyOptions{SignOutRequired: true}) |
| 115 | + // optional sign in (if signed in, use the user as doer, if not, no doer) |
| 116 | + OptSignIn = verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInViewStrict}) |
| 117 | + OptExploreSignIn = verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInViewStrict || setting.Service.Explore.RequireSigninView}) |
| 118 | + |
| 119 | + AdminReq = verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: true, AdminRequired: true}) |
| 120 | +) |
| 121 | + |
| 122 | +func OpenIDSignUpEnabled(ctx *context.Context) { |
| 123 | + if !setting.Service.EnableOpenIDSignUp { |
| 124 | + ctx.HTTPError(http.StatusForbidden) |
| 125 | + return |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func OpenIDSignInEnabled(ctx *context.Context) { |
| 130 | + if !setting.Service.EnableOpenIDSignIn { |
| 131 | + ctx.HTTPError(http.StatusForbidden) |
| 132 | + return |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func LinkAccountEnabled(ctx *context.Context) { |
| 137 | + if !setting.Service.EnableOpenIDSignIn && !setting.Service.EnableOpenIDSignUp && !setting.OAuth2.Enabled { |
| 138 | + ctx.HTTPError(http.StatusForbidden) |
| 139 | + return |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func Oauth2Enabled(ctx *context.Context) { |
| 144 | + if !setting.OAuth2.Enabled { |
| 145 | + ctx.HTTPError(http.StatusForbidden) |
| 146 | + return |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func PackagesEnabled(ctx *context.Context) { |
| 151 | + if !setting.Packages.Enabled { |
| 152 | + ctx.HTTPError(http.StatusForbidden) |
| 153 | + return |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// WebhooksEnabled requires webhooks to be enabled by admin. |
| 158 | +func WebhooksEnabled(ctx *context.Context) { |
| 159 | + if setting.DisableWebhooks { |
| 160 | + ctx.HTTPError(http.StatusForbidden) |
| 161 | + return |
| 162 | + } |
| 163 | +} |
0 commit comments