diff --git a/controller/controller.go b/controller/controller.go index 8de30f5..e2937ff 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -12,10 +12,10 @@ import ( "sync" "goweb/config" - "goweb/container" "goweb/funcmap" "goweb/middleware" "goweb/msg" + "goweb/services" "github.com/go-playground/validator/v10" @@ -37,10 +37,10 @@ var ( ) type Controller struct { - Container *container.Container + Container *services.Container } -func NewController(c *container.Container) Controller { +func NewController(c *services.Container) Controller { return Controller{ Container: c, } diff --git a/main.go b/main.go index bb0d850..b3cab62 100644 --- a/main.go +++ b/main.go @@ -8,12 +8,12 @@ import ( "os/signal" "time" - "goweb/container" "goweb/routes" + "goweb/services" ) func main() { - c := container.NewContainer() + c := services.NewContainer() // Build the router routes.BuildRouter(c) diff --git a/middleware/auth.go b/middleware/auth.go index 7bf8cf3..c17adfd 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -3,22 +3,22 @@ package middleware import ( "net/http" - "goweb/container" "goweb/context" "goweb/ent" "goweb/msg" + "goweb/services" "github.com/labstack/echo/v4" ) -func LoadAuthenticatedUser(authClient *container.AuthClient) echo.MiddlewareFunc { +func LoadAuthenticatedUser(authClient *services.AuthClient) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { u, err := authClient.GetAuthenticatedUser(c) switch err.(type) { case *ent.NotFoundError: c.Logger().Debug("auth user not found") - case container.NotAuthenticatedError: + case services.NotAuthenticatedError: case nil: c.Set(context.AuthenticatedUserKey, u) c.Logger().Info("auth user loaded in to context: %d", u.ID) @@ -31,7 +31,7 @@ func LoadAuthenticatedUser(authClient *container.AuthClient) echo.MiddlewareFunc } } -func LoadValidPasswordToken(authClient *container.AuthClient) echo.MiddlewareFunc { +func LoadValidPasswordToken(authClient *services.AuthClient) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { var usr *ent.User @@ -46,7 +46,7 @@ func LoadValidPasswordToken(authClient *container.AuthClient) echo.MiddlewareFun switch err.(type) { case nil: - case container.InvalidTokenError: + case services.InvalidPasswordTokenError: msg.Warning(c, "The link is either invalid or has expired. Please request a new one.") return c.Redirect(http.StatusFound, c.Echo().Reverse("forgot_password")) default: diff --git a/routes/router.go b/routes/router.go index 825523f..a68ea7f 100644 --- a/routes/router.go +++ b/routes/router.go @@ -6,6 +6,7 @@ import ( "goweb/config" "goweb/controller" "goweb/middleware" + "goweb/services" "github.com/go-playground/validator/v10" @@ -14,8 +15,6 @@ import ( "github.com/labstack/echo/v4" echomw "github.com/labstack/echo/v4/middleware" - - "goweb/container" ) type Validator struct { @@ -31,7 +30,7 @@ func (v *Validator) Validate(i interface{}) error { // TODO: This is doing more than building the router -func BuildRouter(c *container.Container) { +func BuildRouter(c *services.Container) { // Static files with proper cache control // funcmap.File() should be used in templates to append a cache key to the URL in order to break cache // after each server restart @@ -75,7 +74,7 @@ func BuildRouter(c *container.Container) { userRoutes(c, g, ctr) } -func navRoutes(c *container.Container, g *echo.Group, ctr controller.Controller) { +func navRoutes(c *services.Container, g *echo.Group, ctr controller.Controller) { home := Home{Controller: ctr} g.GET("/", home.Get).Name = "home" @@ -87,7 +86,7 @@ func navRoutes(c *container.Container, g *echo.Group, ctr controller.Controller) g.POST("/contact", contact.Post).Name = "contact.post" } -func userRoutes(c *container.Container, g *echo.Group, ctr controller.Controller) { +func userRoutes(c *services.Container, g *echo.Group, ctr controller.Controller) { logout := Logout{Controller: ctr} g.GET("/logout", logout.Get, middleware.RequireAuthentication()).Name = "logout" diff --git a/routes/routes_test.go b/routes/routes_test.go index 7dfcbbe..5405fcc 100644 --- a/routes/routes_test.go +++ b/routes/routes_test.go @@ -8,7 +8,7 @@ import ( "testing" "goweb/config" - "goweb/container" + "goweb/services" "github.com/PuerkitoBio/goquery" "github.com/stretchr/testify/assert" @@ -18,7 +18,7 @@ import ( var ( srv *httptest.Server - c *container.Container + c *services.Container ) func TestMain(m *testing.M) { @@ -26,7 +26,7 @@ func TestMain(m *testing.M) { config.SwitchEnvironment(config.EnvTest) // Start a test HTTP server - c = container.NewContainer() + c = services.NewContainer() BuildRouter(c) srv = httptest.NewServer(c.Web) diff --git a/container/auth.go b/services/auth.go similarity index 81% rename from container/auth.go rename to services/auth.go index 7853d68..330cacb 100644 --- a/container/auth.go +++ b/services/auth.go @@ -1,4 +1,4 @@ -package container +package services import ( "crypto/rand" @@ -16,14 +16,14 @@ import ( ) const ( - // sessionName stores the name of the session which contains authentication data - sessionName = "ua" + // authSessionName stores the name of the session which contains authentication data + authSessionName = "ua" - // sessionKeyUserID stores the key used to store the user ID in the session - sessionKeyUserID = "user_id" + // authSessionKeyUserID stores the key used to store the user ID in the session + authSessionKeyUserID = "user_id" - // sessionKeyAuthenticated stores the key used to store the authentication status in the session - sessionKeyAuthenticated = "authenticated" + // authSessionKeyAuthenticated stores the key used to store the authentication status in the session + authSessionKeyAuthenticated = "authenticated" ) // NotAuthenticatedError is an error returned when a user is not authenticated @@ -34,21 +34,21 @@ func (e NotAuthenticatedError) Error() string { return "user not authenticated" } -// InvalidTokenError is an error returned when an invalid token is provided -type InvalidTokenError struct{} +// InvalidPasswordTokenError is an error returned when an invalid token is provided +type InvalidPasswordTokenError struct{} // Error implements the error interface. -func (e InvalidTokenError) Error() string { - return "invalid token" +func (e InvalidPasswordTokenError) Error() string { + return "invalid password token" } -// AuthClient is the AuthClient that handles authentication requests +// AuthClient is the client that handles authentication requests type AuthClient struct { config *config.Config orm *ent.Client } -// NewAuthClient creates a new authentication AuthClient +// NewAuthClient creates a new authentication client func NewAuthClient(cfg *config.Config, orm *ent.Client) *AuthClient { return &AuthClient{ config: cfg, @@ -58,34 +58,34 @@ func NewAuthClient(cfg *config.Config, orm *ent.Client) *AuthClient { // Login logs in a user of a given ID func (c *AuthClient) Login(ctx echo.Context, userID int) error { - sess, err := session.Get(sessionName, ctx) + sess, err := session.Get(authSessionName, ctx) if err != nil { return err } - sess.Values[sessionKeyUserID] = userID - sess.Values[sessionKeyAuthenticated] = true + sess.Values[authSessionKeyUserID] = userID + sess.Values[authSessionKeyAuthenticated] = true return sess.Save(ctx.Request(), ctx.Response()) } // Logout logs the requesting user out func (c *AuthClient) Logout(ctx echo.Context) error { - sess, err := session.Get(sessionName, ctx) + sess, err := session.Get(authSessionName, ctx) if err != nil { return err } - sess.Values[sessionKeyAuthenticated] = false + sess.Values[authSessionKeyAuthenticated] = false return sess.Save(ctx.Request(), ctx.Response()) } // GetAuthenticatedUserID returns the authenticated user's ID, if the user is logged in func (c *AuthClient) GetAuthenticatedUserID(ctx echo.Context) (int, error) { - sess, err := session.Get(sessionName, ctx) + sess, err := session.Get(authSessionName, ctx) if err != nil { return 0, err } - if sess.Values[sessionKeyAuthenticated] == true { - return sess.Values[sessionKeyUserID].(int), nil + if sess.Values[authSessionKeyAuthenticated] == true { + return sess.Values[authSessionKeyUserID].(int), nil } return 0, NotAuthenticatedError{} @@ -171,7 +171,7 @@ func (c *AuthClient) GetValidPasswordToken(ctx echo.Context, token string, userI } } - return nil, InvalidTokenError{} + return nil, InvalidPasswordTokenError{} } // DeletePasswordTokens deletes all password tokens in the database for a belonging to a given user. diff --git a/container/auth_test.go b/services/auth_test.go similarity index 99% rename from container/auth_test.go rename to services/auth_test.go index 68b8b99..9032b60 100644 --- a/container/auth_test.go +++ b/services/auth_test.go @@ -1,4 +1,4 @@ -package container +package services import ( "context" diff --git a/container/container.go b/services/container.go similarity index 99% rename from container/container.go rename to services/container.go index 4016a82..0ff87c0 100644 --- a/container/container.go +++ b/services/container.go @@ -1,4 +1,4 @@ -package container +package services import ( "context" diff --git a/container/container_test.go b/services/container_test.go similarity index 98% rename from container/container_test.go rename to services/container_test.go index 029ac46..63a5fa1 100644 --- a/container/container_test.go +++ b/services/container_test.go @@ -1,4 +1,4 @@ -package container +package services import ( "context"