mirror of
https://github.com/h44z/wg-portal.git
synced 2025-09-15 15:21:14 +00:00
chore: replace gin with standard lib net/http
This commit is contained in:
88
internal/app/api/v0/handlers/web_session.go
Normal file
88
internal/app/api/v0/handlers/web_session.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/alexedwards/scs/v2"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gob.Register(SessionData{})
|
||||
}
|
||||
|
||||
type SessionData struct {
|
||||
LoggedIn bool
|
||||
IsAdmin bool
|
||||
|
||||
UserIdentifier string
|
||||
|
||||
Firstname string
|
||||
Lastname string
|
||||
Email string
|
||||
|
||||
OauthState string
|
||||
OauthNonce string
|
||||
OauthProvider string
|
||||
OauthReturnTo string
|
||||
|
||||
CsrfToken string
|
||||
}
|
||||
|
||||
const sessionApiV0Key = "session_api_v0"
|
||||
|
||||
type SessionWrapper struct {
|
||||
*scs.SessionManager
|
||||
}
|
||||
|
||||
func NewSessionWrapper(cfg *config.Config) *SessionWrapper {
|
||||
sessionManager := scs.New()
|
||||
sessionManager.Lifetime = 24 * time.Hour
|
||||
sessionManager.IdleTimeout = 1 * time.Hour
|
||||
sessionManager.Cookie.Name = cfg.Web.SessionIdentifier
|
||||
sessionManager.Cookie.Secure = strings.HasPrefix(cfg.Web.ExternalUrl, "https")
|
||||
sessionManager.Cookie.HttpOnly = true
|
||||
sessionManager.Cookie.SameSite = http.SameSiteLaxMode
|
||||
sessionManager.Cookie.Path = "/"
|
||||
sessionManager.Cookie.Persist = false
|
||||
|
||||
wrappedSessionManager := &SessionWrapper{sessionManager}
|
||||
|
||||
return wrappedSessionManager
|
||||
}
|
||||
|
||||
func (s *SessionWrapper) SetData(ctx context.Context, value SessionData) {
|
||||
s.SessionManager.Put(ctx, sessionApiV0Key, value)
|
||||
}
|
||||
|
||||
func (s *SessionWrapper) GetData(ctx context.Context) SessionData {
|
||||
sessionData, ok := s.SessionManager.Get(ctx, sessionApiV0Key).(SessionData)
|
||||
if !ok {
|
||||
return s.defaultSessionData()
|
||||
}
|
||||
return sessionData
|
||||
}
|
||||
|
||||
func (s *SessionWrapper) DestroyData(ctx context.Context) {
|
||||
_ = s.SessionManager.Destroy(ctx)
|
||||
}
|
||||
|
||||
func (s *SessionWrapper) defaultSessionData() SessionData {
|
||||
return SessionData{
|
||||
LoggedIn: false,
|
||||
IsAdmin: false,
|
||||
UserIdentifier: "",
|
||||
Firstname: "",
|
||||
Lastname: "",
|
||||
Email: "",
|
||||
OauthState: "",
|
||||
OauthNonce: "",
|
||||
OauthProvider: "",
|
||||
OauthReturnTo: "",
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user