abstract class Session::Store(T)

Overview

Abstract base class for session storage backends

Generic Constraint: T must inherit from Session::Base T must provide a parameterless constructor (T.new)

Example usage:

class UserSession < Session::Base
property? authenticated : Bool = false
property user_id : Int64? = nil
end

# Use memory store for development
store = Session::MemoryStore(UserSession).new

# Use Redis for production
store = Session::RedisStore(UserSession).new(Redis.new)

Direct Known Subclasses

Defined in:

store.cr

Instance Method Summary

Instance Method Detail

abstract def [](key : String) : T #

Core store operations


[View source]
abstract def []=(key : String, session : T) : T #

[View source]
abstract def []?(key : String) : T | Nil #

[View source]
def authenticate(& : T -> _) : T #

Change the session's privileges (log in, switch user, elevate) and give it a new id in one step. Re-using the id a visitor had before logging in lets anyone who planted or observed it (session fixation) share the authenticated session.

store.authenticate { |session| session.user_id = user.id }

[View source]
def build_cookie(name : String, value : String, host : String = "") : HTTP::Cookie #

A cookie carrying the configured security attributes, expiring with the session timeout.


[View source]
abstract def clear #

[View source]
def create : T #

[View source]
def create_session_cookie(host : String) : HTTP::Cookie #

[View source]
def current_session : T #

The session of the request being served. A store is shared by every request, so it is kept per fiber: concurrent requests, and the next request on a keep-alive connection, never see each other's session. SessionHandler calls #release when a request ends.

A fiber spawned during a request starts with a blank session; capture the session before spawn to use it there.


[View source]
def current_session=(current_session : T) #

[View source]
def current_session? : T | Nil #

The current fiber's session, or nil when no request is being served on it.


[View source]
abstract def delete(key : String) #

[View source]
def delete #

Delete current session and create a new one


[View source]
def encrypts_with_secret? : Bool #

Whether this store encrypts sessions with Session.config.secret. Session.configure and SessionHandler require a usable secret only when it does. The default follows encrypt_redis_data (RedisStore, ClusteredRedisStore); a store that never encrypts returns false (MemoryStore), and one that always does returns true (CookieStore).


[View source]
def flash : Flash #

The current request's flash messages (those of its session).


[View source]
def load_from(request_cookies : HTTP::Cookies) : T | Nil #

[View source]
def on(event : Symbol, session_id : String, session : T) #

[View source]
def regenerate_id : T #

Regenerate session ID while preserving session data Important for security after authentication state changes


[View source]
def release : Nil #

Forget the current fiber's session once its request is done.


[View source]
def session_id : String #

[View source]
def session_key #

[View source]
def set_cookies(response_cookies : HTTP::Cookies, host : String = "") : Nil #

[View source]
abstract def size : Int64 #

[View source]
abstract def storage : String #

Storage abstraction - must be implemented by subclasses


[View source]
def timeout #

[View source]
def valid? : Bool #

[View source]