NAME
Dancer2::Core::Role::SessionFactory - Role for session factories
VERSION
version 1.1.2
DESCRIPTION
Any class that consumes this role will be able to store, create, retrieve and destroy session objects.
The default values for attributes can be overridden in your Dancer2 configuration. See "Session-engine" in Dancer2::Config.
ATTRIBUTES
cookie_name
The name of the cookie to create for storing the session key
Defaults to dancer.session
cookie_domain
The domain of the cookie to create for storing the session key. Defaults to the empty string and is unused as a result.
cookie_path
The path of the cookie to create for storing the session key. Defaults to "/".
cookie_duration
Default duration before session cookie expiration. If set, the Dancer2::Core::Session expires
attribute will be set to the current time plus this duration (expression parsed by Dancer2::Core::Time).
cookie_same_site
Restricts the session cookie to a first-party or same-site context. Defaults to the empty string and is unused as a result. See "same_site" in Dancer2::Core::Cookie.
session_duration
Duration in seconds before sessions should expire, regardless of cookie expiration. If set, then SessionFactories should use this to enforce a limit on session validity.
is_secure
Boolean flag to tell if the session cookie is secure or not.
Default is false.
is_http_only
Boolean flag to tell if the session cookie is http only.
Default is true.
INTERFACE
Following is the interface provided by this role. When specified the required methods to implement are described.
create
Create a brand new session object and store it. Returns the newly created session object.
Triggers an exception if the session is unable to be created.
my $session = MySessionFactory->create();
This method does not need to be implemented in the class.
generate_id
Returns a randomly-generated, guaranteed-unique string. By default, it is a 32-character, URL-safe, Base64 encoded combination of a 32 bit timestamp and a 160 bit SHA1 digest of random seed data. The timestamp ensures that session IDs are generally monotonic.
The default algorithm is not guaranteed cryptographically secure, but it's still reasonably strong for general use.
If you have installed Math::Random::ISAAC::XS and Crypt::URandom, the seed data will be generated from a cryptographically-strong random number generator.
This method is used internally by create() to set the session ID.
This method does not need to be implemented in the class unless an alternative method for session ID generation is desired.
validate_id
Returns true if a session id is of the correct format, or false otherwise.
By default, this ensures that the session ID is a string of characters from the Base64 schema for "URL Applications" plus the ~
character.
This method does not need to be implemented in the class unless an alternative set of characters for session IDs is desired.
retrieve
Return the session object corresponding to the session ID given. If none is found, triggers an exception.
my $session = MySessionFactory->retrieve(id => $id);
The method _retrieve
must be implemented. It must take $id
as a single argument and must return a hash reference of session data.
change_id
Changes the session ID of the corresponding session.
MySessionFactory->change_id(session => $session_object);
The method _change_id
must be implemented. It must take $old_id
and $new_id
as arguments and change the ID from the old one to the new one in the underlying session storage.
destroy
Purges the session object that matches the ID given. Returns the ID of the destroyed session if succeeded, triggers an exception otherwise.
MySessionFactory->destroy(id => $id);
The _destroy
method must be implemented. It must take $id
as a single argument and destroy the underlying data.
flush
Make sure the session object is stored in the factory's backend. This method is called to notify the backend about the change in the session object.
The Dancer application will not call flush unless the session is_dirty
attribute is true to avoid unnecessary writes to the database when no data has been modified.
An exception is triggered if the session is unable to be updated in the backend.
MySessionFactory->flush(session => $session);
The _flush
method must be implemented. It must take two arguments: the $id
and a hash reference of session data.
set_cookie_header
Sets the session cookie into the response object
MySessionFactory->set_cookie_header(
response => $response,
session => $session,
destroyed => undef,
);
The response
parameter contains a Dancer2::Core::Response object. The session
parameter contains a Dancer2::Core::Session object.
The destroyed
parameter is optional. If true, it indicates the session was marked destroyed by the request context. The default set_cookie_header
method doesn't need that information, but it is included in case a SessionFactory must handle destroyed sessions differently (such as signalling to middleware).
cookie
Coerce a session object into a Dancer2::Core::Cookie object.
MySessionFactory->cookie(session => $session);
sessions
Return a list of all session IDs stored in the backend. Useful to create cleaning scripts, in conjunction with session's creation time.
The _sessions
method must be implemented. It must return an array reference of session IDs (or an empty array reference).
CONFIGURATION
If there are configuration values specific to your session factory in your config.yml or environment, those will be passed to the constructor of the session factory automatically. In order to accept and store them, you need to define accessors for them.
engines:
session:
Example:
database_connection: "some_data"
In your session factory:
package Dancer2::Session::Example;
use Moo;
with "Dancer2::Core::Role::SessionFactory";
has database_connection => ( is => "ro" );
You need to do this for every configuration key. The ones that do not have accessors defined will just go to the void.
AUTHOR
Dancer Core Developers
COPYRIGHT AND LICENSE
This software is copyright (c) 2024 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.