2014-03-15 16:31:50 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-04-19 14:29:26 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2014-03-15 16:31:50 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-03-11 22:26:52 +05:30
|
|
|
package context
|
2014-03-15 16:31:50 +05:30
|
|
|
|
|
|
|
import (
|
2019-04-19 14:29:26 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
2019-02-19 12:49:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-03-07 13:42:43 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2014-03-15 16:31:50 +05:30
|
|
|
)
|
|
|
|
|
2016-11-25 12:21:01 +05:30
|
|
|
// ToggleOptions contains required or check options
|
2014-03-22 23:14:02 +05:30
|
|
|
type ToggleOptions struct {
|
2016-03-11 22:26:52 +05:30
|
|
|
SignInRequired bool
|
|
|
|
SignOutRequired bool
|
|
|
|
AdminRequired bool
|
|
|
|
DisableCSRF bool
|
2015-08-14 00:13:40 +05:30
|
|
|
}
|
|
|
|
|
2016-11-25 12:21:01 +05:30
|
|
|
// Toggle returns toggle options as middleware
|
2021-01-26 21:06:53 +05:30
|
|
|
func Toggle(options *ToggleOptions) func(ctx *Context) {
|
2014-03-15 16:31:50 +05:30
|
|
|
return func(ctx *Context) {
|
2016-07-16 07:52:16 +05:30
|
|
|
// Check prohibit login users.
|
2018-09-13 17:34:25 +05:30
|
|
|
if ctx.IsSigned {
|
2019-02-19 12:49:28 +05:30
|
|
|
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
|
|
|
ctx.HTML(200, "user/auth/activate")
|
|
|
|
return
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
if !ctx.User.IsActive || ctx.User.ProhibitLogin {
|
2019-02-19 12:49:28 +05:30
|
|
|
log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
|
2018-09-13 17:34:25 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
|
|
|
|
ctx.HTML(200, "user/auth/prohibit_login")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.User.MustChangePassword {
|
2019-02-28 13:31:42 +05:30
|
|
|
if ctx.Req.URL.Path != "/user/settings/change_password" {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
|
|
|
|
ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
|
2020-05-27 04:09:39 +05:30
|
|
|
if ctx.Req.URL.Path != "/user/events" {
|
2021-03-07 13:42:43 +05:30
|
|
|
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
|
2020-05-27 04:09:39 +05:30
|
|
|
}
|
2019-02-28 13:31:42 +05:30
|
|
|
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 17:34:25 +05:30
|
|
|
return
|
|
|
|
}
|
2016-07-16 07:52:16 +05:30
|
|
|
}
|
|
|
|
|
2014-05-05 22:38:01 +05:30
|
|
|
// Redirect to dashboard if user tries to visit any non-login page.
|
2019-12-24 05:41:12 +05:30
|
|
|
if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
|
2016-11-27 15:44:25 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/")
|
2014-03-20 17:20:26 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" {
|
|
|
|
Validate(ctx, ctx.csrf)
|
2014-08-01 02:55:34 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2014-03-22 23:14:02 +05:30
|
|
|
}
|
|
|
|
|
2016-03-11 22:26:52 +05:30
|
|
|
if options.SignInRequired {
|
2014-03-22 23:14:02 +05:30
|
|
|
if !ctx.IsSigned {
|
2020-08-09 04:09:40 +05:30
|
|
|
if ctx.Req.URL.Path != "/user/events" {
|
2021-03-07 13:42:43 +05:30
|
|
|
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
|
2020-08-09 04:09:40 +05:30
|
|
|
}
|
2016-11-27 15:44:25 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login")
|
2014-03-22 23:14:02 +05:30
|
|
|
return
|
2014-05-26 05:41:25 +05:30
|
|
|
} else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
|
2014-08-10 09:32:00 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
2014-09-02 22:31:02 +05:30
|
|
|
ctx.HTML(200, "user/auth/activate")
|
2014-03-22 23:14:02 +05:30
|
|
|
return
|
|
|
|
}
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to log in page if auto-signin info is provided and has not signed in.
|
|
|
|
if !options.SignOutRequired && !ctx.IsSigned &&
|
|
|
|
len(ctx.GetCookie(setting.CookieUserName)) > 0 {
|
|
|
|
if ctx.Req.URL.Path != "/user/events" {
|
2021-03-07 13:42:43 +05:30
|
|
|
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.AdminRequired {
|
|
|
|
if !ctx.User.IsAdmin {
|
|
|
|
ctx.Error(403)
|
|
|
|
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 {
|
|
|
|
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
|
|
|
ctx.JSON(403, map[string]string{
|
|
|
|
"message": "This account is not activated.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ctx.User.IsActive || ctx.User.ProhibitLogin {
|
|
|
|
log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
|
|
|
|
ctx.JSON(403, map[string]string{
|
|
|
|
"message": "This account is prohibited from signing in, please contact your site administrator.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.User.MustChangePassword {
|
|
|
|
ctx.JSON(403, map[string]string{
|
|
|
|
"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.
|
|
|
|
ctx.JSON(403, map[string]string{
|
|
|
|
"message": "Only signed in user is allowed to call APIs.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
} else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
|
|
|
ctx.HTML(200, "user/auth/activate")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ctx.IsSigned && ctx.IsBasicAuth {
|
2019-04-19 14:29:26 +05:30
|
|
|
twofa, err := models.GetTwoFactorByUID(ctx.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrTwoFactorNotEnrolled(err) {
|
|
|
|
return // No 2FA enrollment for this user
|
|
|
|
}
|
2021-01-26 21:06:53 +05:30
|
|
|
ctx.InternalServerError(err)
|
2019-04-19 14:29:26 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
|
|
|
|
ok, err := twofa.ValidateTOTP(otpHeader)
|
|
|
|
if err != nil {
|
2021-01-26 21:06:53 +05:30
|
|
|
ctx.InternalServerError(err)
|
2019-04-19 14:29:26 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
ctx.JSON(403, map[string]string{
|
|
|
|
"message": "Only signed in user is allowed to call APIs.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 23:14:02 +05:30
|
|
|
}
|
|
|
|
|
2016-03-11 22:26:52 +05:30
|
|
|
if options.AdminRequired {
|
2014-03-22 23:14:02 +05:30
|
|
|
if !ctx.User.IsAdmin {
|
2021-01-26 21:06:53 +05:30
|
|
|
ctx.JSON(403, map[string]string{
|
|
|
|
"message": "You have no permission to request for this.",
|
|
|
|
})
|
2014-03-22 23:14:02 +05:30
|
|
|
return
|
|
|
|
}
|
2014-03-22 23:57:03 +05:30
|
|
|
ctx.Data["PageIsAdmin"] = true
|
2014-03-15 16:31:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|