mirror of
https://github.com/nkanaev/yarr.git
synced 2025-07-09 16:20:09 +00:00
move authmiddleware to auth package
This commit is contained in:
parent
0b1c90718d
commit
eb0ad7f22e
@ -1,36 +1,35 @@
|
|||||||
package server
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/nkanaev/yarr/src/assets"
|
"github.com/nkanaev/yarr/src/assets"
|
||||||
"github.com/nkanaev/yarr/src/auth"
|
|
||||||
"github.com/nkanaev/yarr/src/router"
|
"github.com/nkanaev/yarr/src/router"
|
||||||
)
|
)
|
||||||
|
|
||||||
type authMiddleware struct {
|
type Middleware struct {
|
||||||
username string
|
Username string
|
||||||
password string
|
Password string
|
||||||
basepath string
|
BasePath string
|
||||||
public string
|
Public string
|
||||||
}
|
}
|
||||||
|
|
||||||
func unsafeMethod(method string) bool {
|
func unsafeMethod(method string) bool {
|
||||||
return method == "POST" || method == "PUT" || method == "DELETE"
|
return method == "POST" || method == "PUT" || method == "DELETE"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *authMiddleware) handler(c *router.Context) {
|
func (m *Middleware) Handler(c *router.Context) {
|
||||||
if strings.HasPrefix(c.Req.URL.Path, m.basepath + m.public) {
|
if strings.HasPrefix(c.Req.URL.Path, m.BasePath + m.Public) {
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if auth.IsAuthenticated(c.Req, m.username, m.password) {
|
if IsAuthenticated(c.Req, m.Username, m.Password) {
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rootUrl := m.basepath + "/"
|
rootUrl := m.BasePath + "/"
|
||||||
|
|
||||||
if c.Req.URL.Path != rootUrl {
|
if c.Req.URL.Path != rootUrl {
|
||||||
c.Out.WriteHeader(http.StatusUnauthorized)
|
c.Out.WriteHeader(http.StatusUnauthorized)
|
||||||
@ -40,8 +39,8 @@ func (m *authMiddleware) handler(c *router.Context) {
|
|||||||
if c.Req.Method == "POST" {
|
if c.Req.Method == "POST" {
|
||||||
username := c.Req.FormValue("username")
|
username := c.Req.FormValue("username")
|
||||||
password := c.Req.FormValue("password")
|
password := c.Req.FormValue("password")
|
||||||
if auth.StringsEqual(username, m.username) && auth.StringsEqual(password, m.password) {
|
if StringsEqual(username, m.Username) && StringsEqual(password, m.Password) {
|
||||||
auth.Authenticate(c.Out, m.username, m.password, m.basepath)
|
Authenticate(c.Out, m.Username, m.Password, m.BasePath)
|
||||||
c.Redirect(rootUrl)
|
c.Redirect(rootUrl)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
@ -18,15 +18,14 @@ import (
|
|||||||
func (s *Server) handler() http.Handler {
|
func (s *Server) handler() http.Handler {
|
||||||
r := router.NewRouter(BasePath)
|
r := router.NewRouter(BasePath)
|
||||||
|
|
||||||
// TODO: auth, base, security
|
|
||||||
if s.Username != "" && s.Password != "" {
|
if s.Username != "" && s.Password != "" {
|
||||||
a := &authMiddleware{
|
a := &auth.Middleware{
|
||||||
basepath: BasePath,
|
BasePath: BasePath,
|
||||||
username: s.Username,
|
Username: s.Username,
|
||||||
password: s.Password,
|
Password: s.Password,
|
||||||
public: "/static",
|
Public: "/static",
|
||||||
}
|
}
|
||||||
r.Use(a.handler)
|
r.Use(a.Handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.For("/", s.handleIndex)
|
r.For("/", s.handleIndex)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user