Phoenix.SessionProcess.SessionId (phoenix_session_process v1.0.1)

Copy Markdown View Source

A Phoenix plug that generates and manages unique session IDs.

This plug creates a cryptographically secure, URL-safe session ID for each user session and stores it in the Plug session. The session ID is used to identify and manage the dedicated session process for each user.

Session ID Format

Session IDs are generated using:

  • 24 bytes (192 bits) of cryptographically secure random data
  • URL-safe Base64 encoding without padding
  • Results in 32-character strings like: aBcDeFgHiJkLmNoPqRsTuVwXyZaBcDeFg

Integration

Add this plug after the :fetch_session plug in your router:

pipeline :browser do
  plug :accepts, ["html"]
  plug :fetch_session
  plug Phoenix.SessionProcess.SessionId  # Add this line
  # ... other plugs ...
end

Security Considerations

  • Uses :crypto.strong_rand_bytes/1 for cryptographically secure randomness
  • 192 bits of entropy provides excellent protection against guessing attacks
  • URL-safe encoding ensures compatibility with web standards
  • No padding removes potential trailing characters that could cause issues

Session Lifecycle

  1. First request: Generates new session ID and stores in session
  2. Subsequent requests: Retrieves existing session ID from session
  3. Session ID is stored in the Plug session, retrieve with get_session(conn, :session_id)

Usage in Controllers

def index(conn, _params) do
  session_id = get_session(conn, :session_id)
  # Use session_id to start or communicate with session process
end

Summary

Functions

Generates a cryptographically secure, URL-safe session ID.

Functions

generate_unique_session_id()

@spec generate_unique_session_id() :: binary()

Generates a cryptographically secure, URL-safe session ID.

This function creates a 32-character session ID with 192 bits of entropy, suitable for use as a unique session identifier.

Examples

iex> session_id = Phoenix.SessionProcess.SessionId.generate_unique_session_id()
iex> is_binary(session_id)
true
iex> String.length(session_id)
32
iex> Base.url_decode64(session_id, padding: false)
{:ok, <<_::24*8>>}

Security

  • Uses :crypto.strong_rand_bytes/1 for cryptographically secure randomness
  • 192 bits of entropy makes guessing attacks infeasible
  • URL-safe Base64 encoding without padding
  • No special characters that could cause issues in URLs or headers

Returns

  • binary() - A 32-character URL-safe session ID