mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"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(c echo.Context) error {
|
||||
if c.Request().RequestURI == constant.RouteLogin {
|
||||
return next(c)
|
||||
}
|
||||
|
||||
s, err := readSession(c)
|
||||
if s != "bar" || err != nil {
|
||||
if !as.Authenticated(c) {
|
||||
return c.Redirect(http.StatusSeeOther, constant.RouteLogin)
|
||||
}
|
||||
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 (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"github.com/rjNemo/rentease/internal/constant"
|
||||
|
|
@ -12,11 +10,6 @@ import (
|
|||
"github.com/rjNemo/rentease/internal/view"
|
||||
)
|
||||
|
||||
const (
|
||||
sessionName = "rentease"
|
||||
sessionAge = 86400 * 7 // 7 days
|
||||
)
|
||||
|
||||
func handleLoginPage() echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return renderTempl(c, http.StatusOK, view.Login(view.LoginFormViewModel{}))
|
||||
|
|
@ -25,19 +18,10 @@ func handleLoginPage() echo.HandlerFunc {
|
|||
|
||||
func handleLogin(as *auth.Service) echo.HandlerFunc {
|
||||
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")
|
||||
password := c.FormValue("password")
|
||||
if !as.Authenticate(email, password) {
|
||||
|
||||
if !as.ValidCredentials(email, password) {
|
||||
lfvm := view.LoginFormViewModel{
|
||||
Email: email,
|
||||
Password: password,
|
||||
|
|
@ -47,10 +31,11 @@ func handleLogin(as *auth.Service) echo.HandlerFunc {
|
|||
return renderTempl(c, http.StatusUnauthorized, view.LoginForm(lfvm))
|
||||
}
|
||||
|
||||
sess.Values["foo"] = "bar"
|
||||
if err := sess.Save(c.Request(), c.Response()); err != nil {
|
||||
err := as.Authenticate(c, "foo")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return hxRedirect(c, http.StatusOK, constant.RouteBooking)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ func (s Server) MountHandlers() {
|
|||
api.POST("/sync", handleSync(s.bs))
|
||||
|
||||
private := s.Router.Group("")
|
||||
private.Use(MakeAuthMiddleware())
|
||||
private.Use(MakeAuthMiddleware(s.as))
|
||||
private.GET("/bookings", handleBookingListPage(s.bs, s.hc))
|
||||
private.GET("/bookings/new", handleBookingCreatePage(s.hc))
|
||||
private.POST("/bookings/new", handleBookingCreate(s.bs))
|
||||
|
|
|
|||
|
|
@ -1,6 +1,19 @@
|
|||
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 {
|
||||
secret string
|
||||
|
|
@ -18,6 +31,7 @@ func NewService(secret, admin, adminSecret, apiKey string) (*Service, error) {
|
|||
if secret == "" || admin == "" || adminSecret == "" || apiKey == "" {
|
||||
return nil, errors.New("error building Auth service. Verify your env variables")
|
||||
}
|
||||
|
||||
return &Service{
|
||||
secret,
|
||||
admin,
|
||||
|
|
@ -26,10 +40,45 @@ func NewService(secret, admin, adminSecret, apiKey string) (*Service, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (as *Service) Authenticate(email, password string) bool {
|
||||
func (as *Service) ValidCredentials(email, password string) bool {
|
||||
return email == as.admin && password == as.adminSecret
|
||||
}
|
||||
|
||||
func (as *Service) ValidateApiKey(key string) bool {
|
||||
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