miniwol/web/auth.go
adro 2485c7ab7a Features, Polish, Improvements
- Added logout, add/remove devices
- Page structure w/ dark theme
- Template files now use target extension
- Accessibility improvements
- Semantic improvements
2022-02-21 15:16:06 +01:00

76 lines
1.6 KiB
Go

package web
import (
"errors"
"net/http"
"time"
"git.ulra.eu/adro/miniwol/config"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
)
var sessions map[string]time.Time
func init() {
sessions = make(map[string]time.Time)
}
func checkAuth(token string) error {
for sToken, expiree := range sessions {
// Expire old sessions
if time.Now().After(expiree) {
delete(sessions, sToken)
continue
}
// Check for valid session of token
if token == sToken {
return nil
}
}
return errors.New("this token is not associated with a valid session")
}
func withAuth(handler echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
session, err := c.Cookie("session")
if err != nil || checkAuth(session.Value) != nil {
return c.Redirect(http.StatusSeeOther, "/")
}
return handler(c)
}
}
// Handlers
func auth(c echo.Context) error {
password := c.FormValue("Password")
if bcrypt.CompareHashAndPassword([]byte(config.Config.PassHash), []byte(password)) != nil {
return c.String(401, "Wrong Password")
}
token := uuid.New().String()
sessions[token] = time.Now().Add(time.Second * time.Duration(config.Config.SessionTTL*60))
c.SetCookie(&http.Cookie{
Name: "session",
Value: token,
Path: "/",
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Expires: sessions[token],
})
return c.Redirect(http.StatusSeeOther, "/")
}
func deauth(c echo.Context) error {
session, err := c.Cookie("session")
if err != nil {
return err
}
delete(sessions, session.Value)
return c.Redirect(http.StatusSeeOther, "/")
}