Class: Aws::SessionStore::DynamoDB::RackMiddleware

Inherits:
Rack::Session::Abstract::Persisted
  • Object
show all
Defined in:
lib/aws/session_store/dynamo_db/rack_middleware.rb

Overview

This class is an ID based Session Store Rack Middleware that uses a DynamoDB backend for session storage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RackMiddleware

Initializes SessionStore middleware.

Parameters:

  • app

    Rack application.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :table_name (String) — default: "sessions"

    Name of the session table.

  • :table_key (String) — default: "session_id"

    The hash key of the session table.

  • :secret_key (String)

    Secret key for HMAC encryption.

  • :consistent_read (Boolean) — default: true

    If true, a strongly consistent read is used. If false, an eventually consistent read is used. @see docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html

  • :read_capacity (Integer) — default: 10

    The maximum number of strongly consistent reads consumed per second before DynamoDB raises a ThrottlingException. @see docs.aws.amazon.com/amazondynamodb/latest/developerguide/read-write-operations.html

  • :write_capacity (Integer) — default: 5

    The maximum number of writes consumed per second before DynamoDB returns a ThrottlingException. @see docs.aws.amazon.com/amazondynamodb/latest/developerguide/read-write-operations.html

  • :raise_errors (Boolean) — default: false

    If true, all errors are raised up the stack when default ErrorHandler. If false, Only specified errors are raised up the stack when the default ErrorHandler is used.

  • :error_handler (#handle_error) — default: Errors::DefaultHandler

    An error handling object that handles all exceptions thrown during execution of the rack application.

  • :max_age (Integer) — default: nil

    Maximum number of seconds earlier from the current time that a session was created.

  • :max_stale (Integer) — default: nil

    Maximum number of seconds before current time that session was last accessed.

  • :enable_locking (Integer) — default: false

    If true, a pessimistic locking strategy will be used for all session accesses.

  • :lock_expiry_time (Integer) — default: 500

    Time in milliseconds after which the lock expires on session.

  • :lock_retry_delay (Integer) — default: 500

    Time in milliseconds to wait before retrying to obtain lock once an attempt to obtain the lock has been made and has failed.

  • :lock_max_wait_time (Integer) — default: 500

    Maximum time in seconds to wait to acquire the lock before giving up.

  • :config_file (String, Pathname)

    Path to a YAML file that contains configuration options.

  • :dynamo_db_client (Aws::DynamoDB::Client) — default: Aws::DynamoDB::Client.new

    DynamoDB client used to perform database operations inside of the rack application.

Raises:

  • (Aws::DynamoDB::Errors::ResourceNotFoundException)

    If a valid table name is not provided.

  • (Aws::SessionStore::DynamoDB::MissingSecretKey)

    If a secret key is not provided.



17
18
19
20
21
22
# File 'lib/aws/session_store/dynamo_db/rack_middleware.rb', line 17

def initialize(app, options = {})
  super
  @config = Configuration.new(options)
  validate_config
  set_locking_strategy
end

Instance Attribute Details

#configConfiguration (readonly)

Returns An instance of Configuration that is used for this middleware.

Returns:

  • (Configuration)

    An instance of Configuration that is used for this middleware.



59
60
61
# File 'lib/aws/session_store/dynamo_db/rack_middleware.rb', line 59

def config
  @config
end

Instance Method Details

#delete_session(req, sid, options) ⇒ String

Destroys session and removes session from database.

Returns:

  • (String)

    return a new session id or nil if options



52
53
54
55
# File 'lib/aws/session_store/dynamo_db/rack_middleware.rb', line 52

def delete_session(req, sid, options)
  @lock.delete_session(req.env, sid)
  generate_sid unless options[:drop]
end

#find_session(req, sid) ⇒ Object

Get session from the database or create a new session.

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aws/session_store/dynamo_db/rack_middleware.rb', line 28

def find_session(req, sid)
  case verify_hmac(sid)
  when nil
    set_new_session_properties(req.env)
  when false
    handle_error { raise Errors::InvalidIDError }
    set_new_session_properties(req.env)
  else
    data = @lock.get_session_data(req.env, sid)
    [sid, data || {}]
  end
end

#write_session(req, sid, session, options) ⇒ Hash, false

Sets the session in the database after packing data.

Returns:

  • (Hash)

    If session has been saved.

  • (false)

    If session has could not be saved.



45
46
47
# File 'lib/aws/session_store/dynamo_db/rack_middleware.rb', line 45

def write_session(req, sid, session, options)
  @lock.set_session_data(req.env, sid, session, options)
end