diff options
author | Unknwon <u@gogs.io> | 2019-10-24 01:51:46 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-24 01:51:46 -0700 |
commit | 01c8df01ec0608f1f25b2f1444adabb98fa5ee8a (patch) | |
tree | f8a7e5dd8d2a8c51e1ce2cabb9d33571a93314dd /pkg/context/auth.go | |
parent | 613139e7bef81d3573e7988a47eb6765f3de347a (diff) |
internal: move packages under this directory (#5836)
* Rename pkg -> internal
* Rename routes -> route
* Move route -> internal/route
* Rename models -> db
* Move db -> internal/db
* Fix route2 -> route
* Move cmd -> internal/cmd
* Bump version
Diffstat (limited to 'pkg/context/auth.go')
-rw-r--r-- | pkg/context/auth.go | 112 |
1 files changed, 0 insertions, 112 deletions
diff --git a/pkg/context/auth.go b/pkg/context/auth.go deleted file mode 100644 index 92ea7142..00000000 --- a/pkg/context/auth.go +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package context - -import ( - "net/http" - "net/url" - "strings" - - "github.com/go-macaron/csrf" - "gopkg.in/macaron.v1" - - "gogs.io/gogs/pkg/auth" - "gogs.io/gogs/pkg/setting" - "gogs.io/gogs/pkg/tool" -) - -type ToggleOptions struct { - SignInRequired bool - SignOutRequired bool - AdminRequired bool - DisableCSRF bool -} - -func Toggle(options *ToggleOptions) macaron.Handler { - return func(c *Context) { - // Cannot view any page before installation. - if !setting.InstallLock { - c.Redirect(setting.AppSubURL + "/install") - return - } - - // Check prohibit login users. - if c.IsLogged && c.User.ProhibitLogin { - c.Data["Title"] = c.Tr("auth.prohibit_login") - c.HTML(200, "user/auth/prohibit_login") - return - } - - // Check non-logged users landing page. - if !c.IsLogged && c.Req.RequestURI == "/" && setting.LandingPageURL != setting.LANDING_PAGE_HOME { - c.Redirect(setting.AppSubURL + string(setting.LandingPageURL)) - return - } - - // Redirect to dashboard if user tries to visit any non-login page. - if options.SignOutRequired && c.IsLogged && c.Req.RequestURI != "/" { - c.Redirect(setting.AppSubURL + "/") - return - } - - if !options.SignOutRequired && !options.DisableCSRF && c.Req.Method == "POST" && !auth.IsAPIPath(c.Req.URL.Path) { - csrf.Validate(c.Context, c.csrf) - if c.Written() { - return - } - } - - if options.SignInRequired { - if !c.IsLogged { - // Restrict API calls with error message. - if auth.IsAPIPath(c.Req.URL.Path) { - c.JSON(403, map[string]string{ - "message": "Only signed in user is allowed to call APIs.", - }) - return - } - - c.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+c.Req.RequestURI), 0, setting.AppSubURL) - c.Redirect(setting.AppSubURL + "/user/login") - return - } else if !c.User.IsActive && setting.Service.RegisterEmailConfirm { - c.Data["Title"] = c.Tr("auth.active_your_account") - c.HTML(200, "user/auth/activate") - return - } - } - - // Redirect to log in page if auto-signin info is provided and has not signed in. - if !options.SignOutRequired && !c.IsLogged && !auth.IsAPIPath(c.Req.URL.Path) && - len(c.GetCookie(setting.CookieUserName)) > 0 { - c.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+c.Req.RequestURI), 0, setting.AppSubURL) - c.Redirect(setting.AppSubURL + "/user/login") - return - } - - if options.AdminRequired { - if !c.User.IsAdmin { - c.Error(403) - return - } - c.Data["PageIsAdmin"] = true - } - } -} - -// RequireBasicAuth verifies HTTP Basic Authentication header with given credentials -func (c *Context) RequireBasicAuth(username, password string) { - fields := strings.Fields(c.Req.Header.Get("Authorization")) - if len(fields) != 2 || fields[0] != "Basic" { - c.Status(http.StatusUnauthorized) - return - } - - uname, passwd, _ := tool.BasicAuthDecode(fields[1]) - if uname != username || passwd != password { - c.Status(http.StatusForbidden) - return - } -} |