basepath fixes

This commit is contained in:
Nazar Kanaev 2021-03-17 16:25:16 +00:00
parent 5e453e3227
commit f3c55ba5f2
4 changed files with 25 additions and 29 deletions

View File

@ -23,30 +23,21 @@ func IsAuthenticated(req *http.Request, username, password string) bool {
} }
func Authenticate(rw http.ResponseWriter, username, password, basepath string) { func Authenticate(rw http.ResponseWriter, username, password, basepath string) {
expires := time.Now().Add(time.Hour * 24 * 7) // 1 week http.SetCookie(rw, &http.Cookie{
var cookiePath string
if basepath != "" {
cookiePath = basepath
} else {
cookiePath = "/"
}
cookie := http.Cookie{
Name: "auth", Name: "auth",
Value: username + ":" + secret(username, password), Value: username + ":" + secret(username, password),
Expires: expires, Expires: time.Now().Add(time.Hour * 24 * 7), // 1 week,
Path: cookiePath, Path: basepath,
} })
http.SetCookie(rw, &cookie)
} }
func Logout(rw http.ResponseWriter) { func Logout(rw http.ResponseWriter, basepath string) {
cookie := http.Cookie{ http.SetCookie(rw, &http.Cookie{
Name: "auth", Name: "auth",
Value: "", Value: "",
MaxAge: -1, MaxAge: -1,
} Path: basepath,
http.SetCookie(rw, &cookie) })
} }
func StringsEqual(p1, p2 string) bool { func StringsEqual(p1, p2 string) bool {

View File

@ -16,8 +16,12 @@ type authMiddleware struct {
public string public string
} }
func unsafeMethod(method string) bool {
return method == "POST" || method == "PUT" || method == "DELETE"
}
func (m *authMiddleware) handler(c *router.Context) { func (m *authMiddleware) handler(c *router.Context) {
if strings.HasPrefix(c.Req.URL.Path, m.public) { if strings.HasPrefix(c.Req.URL.Path, m.basepath + m.public) {
c.Next() c.Next()
return return
} }
@ -26,9 +30,14 @@ func (m *authMiddleware) handler(c *router.Context) {
return return
} }
if c.Req.URL.Path != m.basepath { rootUrl := m.basepath + "/"
// TODO: check ajax
c.Out.WriteHeader(http.StatusForbidden) if c.Req.URL.Path != rootUrl {
if unsafeMethod(c.Req.Method) && c.Req.Header.Get("X-Requested-By") != "yarr" {
c.Out.WriteHeader(http.StatusUnauthorized)
return
}
c.Redirect(rootUrl)
return return
} }
@ -37,10 +46,9 @@ func (m *authMiddleware) handler(c *router.Context) {
password := c.Req.FormValue("password") password := c.Req.FormValue("password")
if auth.StringsEqual(username, m.username) && auth.StringsEqual(password, m.password) { if auth.StringsEqual(username, m.username) && auth.StringsEqual(password, m.password) {
auth.Authenticate(c.Out, m.username, m.password, m.basepath) auth.Authenticate(c.Out, m.username, m.password, m.basepath)
c.Redirect(m.basepath) c.Redirect(rootUrl)
return return
} else { } else {
// TODO: show error
c.HTML(http.StatusOK, assets.Template("login.html"), map[string]string{ c.HTML(http.StatusOK, assets.Template("login.html"), map[string]string{
"username": username, "username": username,
"error": "Invalid username/password", "error": "Invalid username/password",

View File

@ -21,10 +21,10 @@ func (s *Server) handler() http.Handler {
// TODO: auth, base, security // TODO: auth, base, security
if s.Username != "" && s.Password != "" { if s.Username != "" && s.Password != "" {
a := &authMiddleware{ a := &authMiddleware{
basepath: BasePath,
username: s.Username, username: s.Username,
password: s.Password, password: s.Password,
basepath: BasePath + "/", public: "/static",
public: BasePath + "/static",
} }
r.Use(a.handler) r.Use(a.handler)
} }
@ -401,6 +401,6 @@ func (s *Server) handlePageCrawl(c *router.Context) {
} }
func (s *Server) handleLogout(c *router.Context) { func (s *Server) handleLogout(c *router.Context) {
auth.Logout(c.Out) auth.Logout(c.Out, BasePath)
c.Out.WriteHeader(http.StatusNoContent) c.Out.WriteHeader(http.StatusNoContent)
} }

View File

@ -54,9 +54,6 @@ func (s *Server) Start() {
} }
} }
func unsafeMethod(method string) bool {
return method == "POST" || method == "PUT" || method == "DELETE"
}
/* /*
func (h Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (h Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {