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