2014-03-15 06:01:50 -05:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-04-19 03:59:26 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2014-03-15 06:01:50 -05:00
|
|
|
|
2016-03-11 11:56:52 -05:00
|
|
|
package context
|
2014-03-15 06:01:50 -05:00
|
|
|
|
|
|
|
import (
|
2021-04-05 10:30:52 -05:00
|
|
|
"net/http"
|
2022-06-19 14:23:00 -05:00
|
|
|
"strings"
|
2021-04-05 10:30:52 -05:00
|
|
|
|
2022-01-02 08:12:35 -05:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2019-02-19 02:19:28 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2016-11-10 11:24:48 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-03-07 03:12:43 -05:00
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2014-03-15 06:01:50 -05:00
|
|
|
)
|
|
|
|
|
2016-11-25 01:51:01 -05:00
|
|
|
// ToggleOptions contains required or check options
|
2014-03-22 12:44:02 -05:00
|
|
|
type ToggleOptions struct {
|
2016-03-11 11:56:52 -05:00
|
|
|
SignInRequired bool
|
|
|
|
SignOutRequired bool
|
|
|
|
AdminRequired bool
|
|
|
|
DisableCSRF bool
|
2015-08-13 13:43:40 -05:00
|
|
|
}
|
|
|
|
|
2016-11-25 01:51:01 -05:00
|
|
|
// Toggle returns toggle options as middleware
|
2021-01-26 10:36:53 -05:00
|
|
|
func Toggle(options *ToggleOptions) func(ctx *Context) {
|
2014-03-15 06:01:50 -05:00
|
|
|
return func(ctx *Context) {
|
2016-07-15 21:22:16 -05:00
|
|
|
// Check prohibit login users.
|
2018-09-13 07:04:25 -05:00
|
|
|
if ctx.IsSigned {
|
2022-03-22 02:03:22 -05:00
|
|
|
if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
|
2019-02-19 02:19:28 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.HTML(http.StatusOK, "user/auth/activate")
|
2019-02-19 02:19:28 -05:00
|
|
|
return
|
2021-01-26 10:36:53 -05:00
|
|
|
}
|
2022-03-22 02:03:22 -05:00
|
|
|
if !ctx.Doer.IsActive || ctx.Doer.ProhibitLogin {
|
|
|
|
log.Info("Failed authentication attempt for %s from %s", ctx.Doer.Name, ctx.RemoteAddr())
|
2018-09-13 07:04:25 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
|
2018-09-13 07:04:25 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 02:03:22 -05:00
|
|
|
if ctx.Doer.MustChangePassword {
|
2019-02-28 03:01:42 -05:00
|
|
|
if ctx.Req.URL.Path != "/user/settings/change_password" {
|
2022-06-19 14:23:00 -05:00
|
|
|
if strings.HasPrefix(ctx.Req.UserAgent(), "git") {
|
|
|
|
ctx.Error(http.StatusUnauthorized, ctx.Tr("auth.must_change_password"))
|
|
|
|
return
|
|
|
|
}
|
2019-02-28 03:01:42 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
|
|
|
|
ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
|
2020-05-26 17:39:39 -05:00
|
|
|
if ctx.Req.URL.Path != "/user/events" {
|
2021-03-07 03:12:43 -05:00
|
|
|
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
|
2020-05-26 17:39:39 -05:00
|
|
|
}
|
2019-02-28 03:01:42 -05:00
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/change_password")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if ctx.Req.URL.Path == "/user/settings/change_password" {
|
|
|
|
// make sure that the form cannot be accessed by users who don't need this
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/")
|
2018-09-13 07:04:25 -05:00
|
|
|
return
|
|
|
|
}
|
2016-07-15 21:22:16 -05:00
|
|
|
}
|
|
|
|
|
2014-05-05 12:08:01 -05:00
|
|
|
// Redirect to dashboard if user tries to visit any non-login page.
|
2019-12-23 19:11:12 -05:00
|
|
|
if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
|
2016-11-27 05:14:25 -05:00
|
|
|
ctx.Redirect(setting.AppSubURL + "/")
|
2014-03-20 06:50:26 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:36:53 -05:00
|
|
|
if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" {
|
2023-04-13 14:45:33 -05:00
|
|
|
ctx.Csrf.Validate(ctx)
|
2014-07-31 16:25:34 -05:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2014-03-22 12:44:02 -05:00
|
|
|
}
|
|
|
|
|
2016-03-11 11:56:52 -05:00
|
|
|
if options.SignInRequired {
|
2014-03-22 12:44:02 -05:00
|
|
|
if !ctx.IsSigned {
|
2020-08-08 17:39:40 -05:00
|
|
|
if ctx.Req.URL.Path != "/user/events" {
|
2021-03-07 03:12:43 -05:00
|
|
|
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
|
2020-08-08 17:39:40 -05:00
|
|
|
}
|
2016-11-27 05:14:25 -05:00
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login")
|
2014-03-22 12:44:02 -05:00
|
|
|
return
|
2022-03-22 02:03:22 -05:00
|
|
|
} else if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
|
2014-08-09 23:02:00 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.HTML(http.StatusOK, "user/auth/activate")
|
2014-03-22 12:44:02 -05:00
|
|
|
return
|
|
|
|
}
|
2021-01-26 10:36:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to log in page if auto-signin info is provided and has not signed in.
|
|
|
|
if !options.SignOutRequired && !ctx.IsSigned &&
|
2023-04-13 14:45:33 -05:00
|
|
|
len(ctx.GetSiteCookie(setting.CookieUserName)) > 0 {
|
2021-01-26 10:36:53 -05:00
|
|
|
if ctx.Req.URL.Path != "/user/events" {
|
2021-03-07 03:12:43 -05:00
|
|
|
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
|
2021-01-26 10:36:53 -05:00
|
|
|
}
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.AdminRequired {
|
2022-03-22 02:03:22 -05:00
|
|
|
if !ctx.Doer.IsAdmin {
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.Error(http.StatusForbidden)
|
2021-01-26 10:36:53 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["PageIsAdmin"] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToggleAPI returns toggle options as middleware
|
|
|
|
func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
|
|
|
|
return func(ctx *APIContext) {
|
|
|
|
// Check prohibit login users.
|
|
|
|
if ctx.IsSigned {
|
2022-03-22 02:03:22 -05:00
|
|
|
if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
|
2021-01-26 10:36:53 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2021-01-26 10:36:53 -05:00
|
|
|
"message": "This account is not activated.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 02:03:22 -05:00
|
|
|
if !ctx.Doer.IsActive || ctx.Doer.ProhibitLogin {
|
|
|
|
log.Info("Failed authentication attempt for %s from %s", ctx.Doer.Name, ctx.RemoteAddr())
|
2021-01-26 10:36:53 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2021-01-26 10:36:53 -05:00
|
|
|
"message": "This account is prohibited from signing in, please contact your site administrator.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 02:03:22 -05:00
|
|
|
if ctx.Doer.MustChangePassword {
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2021-01-26 10:36:53 -05:00
|
|
|
"message": "You must change your password. Change it at: " + setting.AppURL + "/user/change_password",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to dashboard if user tries to visit any non-login page.
|
|
|
|
if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.SignInRequired {
|
|
|
|
if !ctx.IsSigned {
|
|
|
|
// Restrict API calls with error message.
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2021-01-26 10:36:53 -05:00
|
|
|
"message": "Only signed in user is allowed to call APIs.",
|
|
|
|
})
|
|
|
|
return
|
2022-03-22 02:03:22 -05:00
|
|
|
} else if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
|
2021-01-26 10:36:53 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.HTML(http.StatusOK, "user/auth/activate")
|
2021-01-26 10:36:53 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if ctx.IsSigned && ctx.IsBasicAuth {
|
2021-09-17 06:43:47 -05:00
|
|
|
if skip, ok := ctx.Data["SkipLocalTwoFA"]; ok && skip.(bool) {
|
|
|
|
return // Skip 2FA
|
|
|
|
}
|
2022-03-22 02:03:22 -05:00
|
|
|
twofa, err := auth.GetTwoFactorByUID(ctx.Doer.ID)
|
2019-04-19 03:59:26 -05:00
|
|
|
if err != nil {
|
2022-01-02 08:12:35 -05:00
|
|
|
if auth.IsErrTwoFactorNotEnrolled(err) {
|
2019-04-19 03:59:26 -05:00
|
|
|
return // No 2FA enrollment for this user
|
|
|
|
}
|
2021-01-26 10:36:53 -05:00
|
|
|
ctx.InternalServerError(err)
|
2019-04-19 03:59:26 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
|
|
|
|
ok, err := twofa.ValidateTOTP(otpHeader)
|
|
|
|
if err != nil {
|
2021-01-26 10:36:53 -05:00
|
|
|
ctx.InternalServerError(err)
|
2019-04-19 03:59:26 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2019-04-19 03:59:26 -05:00
|
|
|
"message": "Only signed in user is allowed to call APIs.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 12:44:02 -05:00
|
|
|
}
|
|
|
|
|
2016-03-11 11:56:52 -05:00
|
|
|
if options.AdminRequired {
|
2022-03-22 02:03:22 -05:00
|
|
|
if !ctx.Doer.IsAdmin {
|
2021-04-05 10:30:52 -05:00
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2021-01-26 10:36:53 -05:00
|
|
|
"message": "You have no permission to request for this.",
|
|
|
|
})
|
2014-03-22 12:44:02 -05:00
|
|
|
return
|
|
|
|
}
|
2014-03-22 13:27:03 -05:00
|
|
|
ctx.Data["PageIsAdmin"] = true
|
2014-03-15 06:01:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|