Files
Gogs/internal/context/auth.go

113 lines
2.9 KiB
Go
Raw Normal View History

// 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.
2016-03-11 11:56:52 -05:00
package context
import (
"net/http"
2014-03-22 17:59:22 -04:00
"net/url"
"strings"
2014-03-22 17:59:22 -04:00
2015-10-15 21:28:12 -04:00
"github.com/go-macaron/csrf"
"gopkg.in/macaron.v1"
2014-03-19 12:50:44 -04:00
"gogs.io/gogs/internal/auth"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/tool"
)
2014-03-23 01:44:02 +08:00
type ToggleOptions struct {
2016-03-11 11:56:52 -05:00
SignInRequired bool
SignOutRequired bool
AdminRequired bool
DisableCSRF bool
}
2014-07-26 00:24:27 -04:00
func Toggle(options *ToggleOptions) macaron.Handler {
2017-06-03 07:26:09 -04:00
return func(c *Context) {
2014-05-05 13:08:01 -04:00
// Cannot view any page before installation.
2020-02-22 20:46:16 +08:00
if !conf.Security.InstallLock {
c.RedirectSubpath("/install")
2014-03-30 11:58:21 -04:00
return
}
2016-07-16 10:22:16 +08:00
// Check prohibit login users.
2017-06-03 07:26:09 -04:00
if c.IsLogged && c.User.ProhibitLogin {
c.Data["Title"] = c.Tr("auth.prohibit_login")
c.Success( "user/auth/prohibit_login")
2016-07-16 10:22:16 +08:00
return
}
// Check non-logged users landing page.
if !c.IsLogged && c.Req.RequestURI == "/" && conf.Server.LandingURL != "/" {
c.RedirectSubpath(conf.Server.LandingURL)
2014-11-24 18:47:59 -05:00
return
}
2014-05-05 13:08:01 -04:00
// Redirect to dashboard if user tries to visit any non-login page.
2017-06-03 07:26:09 -04:00
if options.SignOutRequired && c.IsLogged && c.Req.RequestURI != "/" {
c.RedirectSubpath("/")
2014-03-20 07:50:26 -04:00
return
}
2017-06-03 07:26:09 -04:00
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
}
2014-03-23 01:44:02 +08:00
}
2016-03-11 11:56:52 -05:00
if options.SignInRequired {
2017-06-03 07:26:09 -04:00
if !c.IsLogged {
// Restrict API calls with error message.
2017-06-03 07:26:09 -04:00
if auth.IsAPIPath(c.Req.URL.Path) {
c.JSON(http.StatusForbidden, map[string]string{
"message": "Only authenticated user is allowed to call APIs.",
})
return
}
c.SetCookie("redirect_to", url.QueryEscape(conf.Server.Subpath+c.Req.RequestURI), 0, conf.Server.Subpath)
c.RedirectSubpath("/user/login")
2014-03-23 01:44:02 +08:00
return
} else if !c.User.IsActive && conf.Auth.RequireEmailConfirmation {
c.Title("auth.active_your_account")
c.Success("user/auth/activate")
2014-03-23 01:44:02 +08:00
return
}
}
2016-03-11 11:56:52 -05:00
// Redirect to log in page if auto-signin info is provided and has not signed in.
2017-06-03 07:26:09 -04:00
if !options.SignOutRequired && !c.IsLogged && !auth.IsAPIPath(c.Req.URL.Path) &&
2020-02-22 20:46:16 +08:00
len(c.GetCookie(conf.Security.CookieUsername)) > 0 {
c.SetCookie("redirect_to", url.QueryEscape(conf.Server.Subpath+c.Req.RequestURI), 0, conf.Server.Subpath)
c.RedirectSubpath("/user/login")
return
2015-11-18 23:52:09 -05:00
}
2016-03-11 11:56:52 -05:00
if options.AdminRequired {
2017-06-03 07:26:09 -04:00
if !c.User.IsAdmin {
c.Status(http.StatusForbidden)
2014-03-23 01:44:02 +08:00
return
}
c.PageIs("Admin")
}
}
}
// 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
}
}