session

package
v0.4.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateRandomToken

func GenerateRandomToken(length int) string

GenerateRandomToken creates a cryptographically secure random token

func IsSessionManagerInitialized

func IsSessionManagerInitialized() bool

IsSessionManagerInitialized checks if the session manager is properly initialized

func ValidateSessionRequest

func ValidateSessionRequest(req SessionInitRequest) error

ValidateSessionRequest validates a session initialization request

Types

type Response

type Response struct {
	Success bool         `json:"success"`
	Message string       `json:"message"`
	Session *UserSession `json:"session,omitempty"`
}

Response represents the response after successful login

type SessionError

type SessionError struct {
	Message string
}

Error represents session-related errors

func (*SessionError) Error

func (e *SessionError) Error() string

type SessionInitRequest

type SessionInitRequest struct {
	PublicKey     string                 `json:"public_key"`
	RequestedMode SessionInteractionMode `json:"requested_mode"`
	SigningMethod SigningMethod          `json:"signing_method,omitempty"`
	PrivateKey    string                 `json:"private_key,omitempty"` // Only for encrypted key method
}

SessionInitRequest represents data needed to initialize a session

type SessionInteractionMode

type SessionInteractionMode string

SessionInteractionMode defines how the user interacts with the app

const (
	// ReadOnlyMode allows viewing content only
	ReadOnlyMode SessionInteractionMode = "read_only"
	// WriteMode allows creating/publishing events
	WriteMode SessionInteractionMode = "write"
)

type SessionManager

type SessionManager struct {
	// contains filtered or unexported fields
}

SessionManager handles comprehensive user authentication and session tracking

var SessionMgr *SessionManager

Global session manager instance

func NewSessionManager

func NewSessionManager() *SessionManager

NewSessionManager creates a new session manager

func (*SessionManager) CleanupSessions

func (sm *SessionManager) CleanupSessions(maxAge time.Duration)

CleanupSessions removes expired sessions

func (*SessionManager) ClearSession

func (sm *SessionManager) ClearSession(w http.ResponseWriter, r *http.Request)

ClearSession removes a user session and clears the cookie

func (*SessionManager) CreateSession

func (sm *SessionManager) CreateSession(w http.ResponseWriter, req SessionInitRequest) (*UserSession, error)

CreateSession creates a new lightweight user session (no user data - that goes in cache)

func (*SessionManager) GetCurrentUser

func (sm *SessionManager) GetCurrentUser(r *http.Request) *UserSession

GetCurrentUser retrieves the current user session from the request

func (*SessionManager) GetSessionStats

func (sm *SessionManager) GetSessionStats() map[string]interface{}

GetSessionStats returns statistics about active sessions

func (*SessionManager) GetSessionToken

func (sm *SessionManager) GetSessionToken(r *http.Request) string

GetSessionToken extracts the session token from a request

func (*SessionManager) GetUserSession

func (sm *SessionManager) GetUserSession(token string) *UserSession

GetUserSession retrieves a user session by token and updates last active time

type SigningMethod

type SigningMethod string

SigningMethod defines how events are signed

const (
	// BrowserExtension uses browser-based Nostr extensions
	BrowserExtension SigningMethod = "browser_extension"
	// AmberSigning uses Amber on Android for signing
	AmberSigning SigningMethod = "amber"
	// BunkerSigning uses remote signing bunkers
	BunkerSigning SigningMethod = "bunker"
	// EncryptedKey uses an encrypted private key stored in session
	EncryptedKey SigningMethod = "encrypted_key"
	// NoSigning for read-only mode
	NoSigning SigningMethod = "none"
)

type UserSession

type UserSession struct {
	// Core session data
	PublicKey  string    `json:"public_key"`
	LastActive time.Time `json:"last_active"`

	// Interaction mode and signing
	Mode          SessionInteractionMode `json:"mode"`
	SigningMethod SigningMethod          `json:"signing_method"`

	// Connection info (app-level relays, not user-specific)
	ConnectedRelays []string `json:"connected_relays"`

	// Session security
	EncryptedPrivateKey string `json:"encrypted_private_key,omitempty"` // Only if using EncryptedKey method
}

UserSession represents a lightweight user session (no user data - that's in cache)

func CreateUserSession

func CreateUserSession(w http.ResponseWriter, req SessionInitRequest) (*UserSession, error)

CreateUserSession creates a new user session and ensures user data is cached

func (*UserSession) CanCreateEvents

func (s *UserSession) CanCreateEvents() bool

CanCreateEvents returns true if the user can create new events

func (*UserSession) CanSign

func (s *UserSession) CanSign() bool

CanSign returns true if the user can sign events

func (*UserSession) IsReadOnly

func (s *UserSession) IsReadOnly() bool

IsReadOnly returns true if the session is in read-only mode

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL