Files
sonr/internal/ctx/session.go
T
Prad NukalaandGitHub 3790e926de feature/1109 grpc session model (#1141)
- **feat: remove Hway deployment**
- **feat: introduce session middleware for requests**
- **refactor: update path imports to use new pkg folder**
- **feat: add gRPC client for interacting with services**
- **feat: remove grpc client and use REST api**
- **refactor: move  from  to**
- **feat: add client views endpoint**
- **feat: add webauthn support**
- **closes: #1124**
- **refactor: Improve PR labeler configuration**
- **feat: add milestone discussion template**
- **feat: remove OKR tracking issue template**
- **feat: use gorilla sessions for session management**
- **refactor: move pubkey related code to**
- **<no value>**
- **refactor: remove unused identifier type**
- **feat: integrate Macaroon Keeper with Service Module**
- **refactor: rename worker routes for clarity**
2024-10-11 16:47:52 -04:00

81 lines
2.0 KiB
Go

package ctx
import (
"context"
"errors"
"net/http"
"time"
"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
"github.com/segmentio/ksuid"
)
var store sessions.Store
type ctxKeySessionID struct{}
// SessionMiddleware establishes a Session Cookie.
func SessionMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
store = sessions.NewCookieStore([]byte("SESSION_KEY"))
return func(c echo.Context) error {
ctx := c.Request().Context()
// Attempt to read the session ID from the "session" cookie
sessionID, err := readSessionIDFromCookie(c)
if err != nil {
// Generate a new KSUID if the session cookie is missing or invalid
sessionID = ksuid.New().String()
// Write the new session ID to the "session" cookie
err = writeSessionIDToCookie(c, sessionID)
if err != nil {
return c.JSON(
http.StatusInternalServerError,
map[string]string{"error": "Failed to set session cookie"},
)
}
}
// Inject the session ID into the context
ctx = context.WithValue(ctx, ctxKeySessionID{}, sessionID)
// Update the request with the new context
c.SetRequest(c.Request().WithContext(ctx))
return next(c)
}
}
func getSessionID(ctx context.Context) (string, error) {
sessionID, ok := ctx.Value(ctxKeySessionID{}).(string)
if !ok || sessionID == "" {
return "", errors.New("session ID not found in context")
}
return sessionID, nil
}
func readSessionIDFromCookie(c echo.Context) (string, error) {
cookie, err := c.Cookie("session")
if err != nil {
// Cookie not found or other error
return "", err
}
if cookie == nil || cookie.Value == "" {
// Cookie is empty
return "", http.ErrNoCookie
}
return cookie.Value, nil
}
func writeSessionIDToCookie(c echo.Context, sessionID string) error {
cookie := &http.Cookie{
Name: "session",
Value: sessionID,
Expires: time.Now().Add(24 * time.Hour),
HttpOnly: true,
Path: "/",
// Add Secure and SameSite attributes as needed
}
c.SetCookie(cookie)
return nil
}