mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +00:00
refactor authentication service
This commit is contained in:
parent
34ce10ff86
commit
abfbe1ff1b
4 changed files with 60 additions and 36 deletions
|
|
@ -1,35 +1,25 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/labstack/echo-contrib/session"
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
|
||||||
"github.com/rjNemo/rentease/internal/constant"
|
"github.com/rjNemo/rentease/internal/constant"
|
||||||
|
"github.com/rjNemo/rentease/internal/service/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MakeAuthMiddleware() echo.MiddlewareFunc {
|
func MakeAuthMiddleware(as *auth.Service) echo.MiddlewareFunc {
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
if c.Request().RequestURI == constant.RouteLogin {
|
if c.Request().RequestURI == constant.RouteLogin {
|
||||||
return next(c)
|
return next(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := readSession(c)
|
if !as.Authenticated(c) {
|
||||||
if s != "bar" || err != nil {
|
|
||||||
return c.Redirect(http.StatusSeeOther, constant.RouteLogin)
|
return c.Redirect(http.StatusSeeOther, constant.RouteLogin)
|
||||||
}
|
}
|
||||||
return next(c)
|
return next(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func readSession(c echo.Context) (string, error) {
|
|
||||||
sess, err := session.Get(sessionName, c)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s", sess.Values["foo"]), nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@ package server
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
|
||||||
"github.com/labstack/echo-contrib/session"
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
|
||||||
"github.com/rjNemo/rentease/internal/constant"
|
"github.com/rjNemo/rentease/internal/constant"
|
||||||
|
|
@ -12,11 +10,6 @@ import (
|
||||||
"github.com/rjNemo/rentease/internal/view"
|
"github.com/rjNemo/rentease/internal/view"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
sessionName = "rentease"
|
|
||||||
sessionAge = 86400 * 7 // 7 days
|
|
||||||
)
|
|
||||||
|
|
||||||
func handleLoginPage() echo.HandlerFunc {
|
func handleLoginPage() echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
return renderTempl(c, http.StatusOK, view.Login(view.LoginFormViewModel{}))
|
return renderTempl(c, http.StatusOK, view.Login(view.LoginFormViewModel{}))
|
||||||
|
|
@ -25,19 +18,10 @@ func handleLoginPage() echo.HandlerFunc {
|
||||||
|
|
||||||
func handleLogin(as *auth.Service) echo.HandlerFunc {
|
func handleLogin(as *auth.Service) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
sess, err := session.Get(sessionName, c)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sess.Options = &sessions.Options{
|
|
||||||
Path: constant.RouteLogin,
|
|
||||||
MaxAge: sessionAge,
|
|
||||||
HttpOnly: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
email := c.FormValue("email")
|
email := c.FormValue("email")
|
||||||
password := c.FormValue("password")
|
password := c.FormValue("password")
|
||||||
if !as.Authenticate(email, password) {
|
|
||||||
|
if !as.ValidCredentials(email, password) {
|
||||||
lfvm := view.LoginFormViewModel{
|
lfvm := view.LoginFormViewModel{
|
||||||
Email: email,
|
Email: email,
|
||||||
Password: password,
|
Password: password,
|
||||||
|
|
@ -47,10 +31,11 @@ func handleLogin(as *auth.Service) echo.HandlerFunc {
|
||||||
return renderTempl(c, http.StatusUnauthorized, view.LoginForm(lfvm))
|
return renderTempl(c, http.StatusUnauthorized, view.LoginForm(lfvm))
|
||||||
}
|
}
|
||||||
|
|
||||||
sess.Values["foo"] = "bar"
|
err := as.Authenticate(c, "foo")
|
||||||
if err := sess.Save(c.Request(), c.Response()); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return hxRedirect(c, http.StatusOK, constant.RouteBooking)
|
return hxRedirect(c, http.StatusOK, constant.RouteBooking)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ func (s Server) MountHandlers() {
|
||||||
api.POST("/sync", handleSync(s.bs))
|
api.POST("/sync", handleSync(s.bs))
|
||||||
|
|
||||||
private := s.Router.Group("")
|
private := s.Router.Group("")
|
||||||
private.Use(MakeAuthMiddleware())
|
private.Use(MakeAuthMiddleware(s.as))
|
||||||
private.GET("/bookings", handleBookingListPage(s.bs, s.hc))
|
private.GET("/bookings", handleBookingListPage(s.bs, s.hc))
|
||||||
private.GET("/bookings/new", handleBookingCreatePage(s.hc))
|
private.GET("/bookings/new", handleBookingCreatePage(s.hc))
|
||||||
private.POST("/bookings/new", handleBookingCreate(s.bs))
|
private.POST("/bookings/new", handleBookingCreate(s.bs))
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,19 @@
|
||||||
package auth
|
package auth
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
|
"github.com/labstack/echo-contrib/session"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
|
||||||
|
"github.com/rjNemo/rentease/internal/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sessionName = "rentease"
|
||||||
|
sessionAge = 86400 * 7 // 7 days
|
||||||
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
secret string
|
secret string
|
||||||
|
|
@ -18,6 +31,7 @@ func NewService(secret, admin, adminSecret, apiKey string) (*Service, error) {
|
||||||
if secret == "" || admin == "" || adminSecret == "" || apiKey == "" {
|
if secret == "" || admin == "" || adminSecret == "" || apiKey == "" {
|
||||||
return nil, errors.New("error building Auth service. Verify your env variables")
|
return nil, errors.New("error building Auth service. Verify your env variables")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Service{
|
return &Service{
|
||||||
secret,
|
secret,
|
||||||
admin,
|
admin,
|
||||||
|
|
@ -26,10 +40,45 @@ func NewService(secret, admin, adminSecret, apiKey string) (*Service, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *Service) Authenticate(email, password string) bool {
|
func (as *Service) ValidCredentials(email, password string) bool {
|
||||||
return email == as.admin && password == as.adminSecret
|
return email == as.admin && password == as.adminSecret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *Service) ValidateApiKey(key string) bool {
|
func (as *Service) ValidateApiKey(key string) bool {
|
||||||
return key == as.apiKey
|
return key == as.apiKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (as *Service) getSession(c echo.Context) (*sessions.Session, error) {
|
||||||
|
sess, err := session.Get(sessionName, c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sess.Options = &sessions.Options{
|
||||||
|
Path: constant.RouteLogin,
|
||||||
|
MaxAge: sessionAge,
|
||||||
|
HttpOnly: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
return sess, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *Service) Authenticate(c echo.Context, key string) error {
|
||||||
|
sess, err := as.getSession(c)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sess.Values["user"] = key
|
||||||
|
return sess.Save(c.Request(), c.Response())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *Service) Authenticated(c echo.Context) bool {
|
||||||
|
sess, err := as.getSession(c)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
user, ok := sess.Values["user"]
|
||||||
|
return ok && user == "foo"
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue