Removed echo-contrib dependency.
This commit is contained in:
parent
8eafb6b666
commit
75aefa8a0a
13 changed files with 108 additions and 18 deletions
|
|
@ -20,6 +20,9 @@ const (
|
|||
|
||||
// LoggerKey is the key value used to store a structured logger in context
|
||||
LoggerKey = "logger"
|
||||
|
||||
// SessionKey is the key value used to store the session data in context
|
||||
SessionKey = "session"
|
||||
)
|
||||
|
||||
// IsCanceledError determines if an error is due to a context cancelation
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
echomw "github.com/labstack/echo/v4/middleware"
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
"github.com/mikestefanello/pagoda/pkg/middleware"
|
||||
|
|
@ -40,7 +39,7 @@ func BuildRouter(c *services.Container) error {
|
|||
echomw.TimeoutWithConfig(echomw.TimeoutConfig{
|
||||
Timeout: c.Config.App.Timeout,
|
||||
}),
|
||||
session.Middleware(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))),
|
||||
middleware.Session(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))),
|
||||
middleware.LoadAuthenticatedUser(c.Auth),
|
||||
middleware.ServeCachedPage(c.TemplateRenderer),
|
||||
echomw.CSRFWithConfig(echomw.CSRFConfig{
|
||||
|
|
|
|||
19
pkg/middleware/session.go
Normal file
19
pkg/middleware/session.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gorilla/context"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/session"
|
||||
)
|
||||
|
||||
// Session sets the session storage in the request context
|
||||
func Session(store sessions.Store) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(ctx echo.Context) error {
|
||||
defer context.Clear(ctx.Request())
|
||||
session.Store(ctx, store)
|
||||
return next(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
24
pkg/middleware/session_test.go
Normal file
24
pkg/middleware/session_test.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/mikestefanello/pagoda/pkg/session"
|
||||
"github.com/mikestefanello/pagoda/pkg/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSession(t *testing.T) {
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
_, err := session.Get(ctx, "test")
|
||||
assert.Equal(t, session.ErrStoreNotFound, err)
|
||||
|
||||
store := sessions.NewCookieStore([]byte("secret"))
|
||||
err = tests.ExecuteMiddleware(ctx, Session(store))
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = session.Get(ctx, "test")
|
||||
assert.NotEqual(t, session.ErrStoreNotFound, err)
|
||||
}
|
||||
|
|
@ -2,9 +2,9 @@ package msg
|
|||
|
||||
import (
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/log"
|
||||
"github.com/mikestefanello/pagoda/pkg/session"
|
||||
)
|
||||
|
||||
// Type is a message type
|
||||
|
|
@ -78,7 +78,7 @@ func Get(ctx echo.Context, typ Type) []string {
|
|||
|
||||
// getSession gets the flash message session
|
||||
func getSession(ctx echo.Context) (*sessions.Session, error) {
|
||||
sess, err := session.Get(sessionName, ctx)
|
||||
sess, err := session.Get(ctx, sessionName)
|
||||
if err != nil {
|
||||
log.Ctx(ctx).Error("cannot load flash message session",
|
||||
"error", err,
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ import (
|
|||
"github.com/mikestefanello/pagoda/ent/passwordtoken"
|
||||
"github.com/mikestefanello/pagoda/ent/user"
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/session"
|
||||
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
"github.com/labstack/echo/v4"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
|
@ -62,7 +62,7 @@ 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(authSessionName, ctx)
|
||||
sess, err := session.Get(ctx, authSessionName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -73,7 +73,7 @@ func (c *AuthClient) Login(ctx echo.Context, userID int) error {
|
|||
|
||||
// Logout logs the requesting user out
|
||||
func (c *AuthClient) Logout(ctx echo.Context) error {
|
||||
sess, err := session.Get(authSessionName, ctx)
|
||||
sess, err := session.Get(ctx, authSessionName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -83,7 +83,7 @@ func (c *AuthClient) Logout(ctx echo.Context) error {
|
|||
|
||||
// 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(authSessionName, ctx)
|
||||
sess, err := session.Get(ctx, authSessionName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
|
|||
27
pkg/session/session.go
Normal file
27
pkg/session/session.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
)
|
||||
|
||||
// ErrStoreNotFound indicates that the session store was not present in the context
|
||||
var ErrStoreNotFound = errors.New("session store not found")
|
||||
|
||||
// Get returns a session
|
||||
func Get(ctx echo.Context, name string) (*sessions.Session, error) {
|
||||
s := ctx.Get(context.SessionKey)
|
||||
if s == nil {
|
||||
return nil, ErrStoreNotFound
|
||||
}
|
||||
store := s.(sessions.Store)
|
||||
return store.Get(ctx.Request(), name)
|
||||
}
|
||||
|
||||
// Store sets the session storage in the context
|
||||
func Store(ctx echo.Context, store sessions.Store) {
|
||||
ctx.Set(context.SessionKey, store)
|
||||
}
|
||||
23
pkg/session/session_test.go
Normal file
23
pkg/session/session_test.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetStore(t *testing.T) {
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
ctx := e.NewContext(req, httptest.NewRecorder())
|
||||
_, err := Get(ctx, "test")
|
||||
assert.Equal(t, ErrStoreNotFound, err)
|
||||
|
||||
Store(ctx, sessions.NewCookieStore([]byte("secret")))
|
||||
_, err = Get(ctx, "test")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
@ -11,12 +11,12 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/mikestefanello/pagoda/ent"
|
||||
"github.com/mikestefanello/pagoda/pkg/session"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
|
|
@ -29,8 +29,7 @@ func NewContext(e *echo.Echo, url string) (echo.Context, *httptest.ResponseRecor
|
|||
|
||||
// InitSession initializes a session for a given Echo context
|
||||
func InitSession(ctx echo.Context) {
|
||||
mw := session.Middleware(sessions.NewCookieStore([]byte("secret")))
|
||||
_ = ExecuteMiddleware(ctx, mw)
|
||||
session.Store(ctx, sessions.NewCookieStore([]byte("secret")))
|
||||
}
|
||||
|
||||
// ExecuteMiddleware executes a middleware function on a given Echo context
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue