.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Dancer2::Core::Role::SessionFactory 3pm" .TH Dancer2::Core::Role::SessionFactory 3pm "2023-12-15" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Dancer2::Core::Role::SessionFactory \- Role for session factories .SH "VERSION" .IX Header "VERSION" version 1.1.0 .SH "DESCRIPTION" .IX Header "DESCRIPTION" Any class that consumes this role will be able to store, create, retrieve and destroy session objects. .PP The default values for attributes can be overridden in your Dancer2 configuration. See \*(L"Session-engine\*(R" in Dancer2::Config. .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" .SS "cookie_name" .IX Subsection "cookie_name" The name of the cookie to create for storing the session key .PP Defaults to \f(CW\*(C`dancer.session\*(C'\fR .SS "cookie_domain" .IX Subsection "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. .SS "cookie_path" .IX Subsection "cookie_path" The path of the cookie to create for storing the session key. Defaults to \*(L"/\*(R". .SS "cookie_duration" .IX Subsection "cookie_duration" Default duration before session cookie expiration. If set, the Dancer2::Core::Session \f(CW\*(C`expires\*(C'\fR attribute will be set to the current time plus this duration (expression parsed by Dancer2::Core::Time). .SS "cookie_same_site" .IX Subsection "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 \*(L"same_site\*(R" in Dancer2::Core::Cookie. .SS "session_duration" .IX Subsection "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. .SS "is_secure" .IX Subsection "is_secure" Boolean flag to tell if the session cookie is secure or not. .PP Default is false. .SS "is_http_only" .IX Subsection "is_http_only" Boolean flag to tell if the session cookie is http only. .PP Default is true. .SH "INTERFACE" .IX Header "INTERFACE" Following is the interface provided by this role. When specified the required methods to implement are described. .SS "create" .IX Subsection "create" Create a brand new session object and store it. Returns the newly created session object. .PP Triggers an exception if the session is unable to be created. .PP .Vb 1 \& my $session = MySessionFactory\->create(); .Ve .PP This method does not need to be implemented in the class. .SS "generate_id" .IX Subsection "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 \s-1SHA1\s0 digest of random seed data. The timestamp ensures that session IDs are generally monotonic. .PP The default algorithm is not guaranteed cryptographically secure, but it's still reasonably strong for general use. .PP If you have installed Math::Random::ISAAC::XS and Crypt::URandom, the seed data will be generated from a cryptographically-strong random number generator. .PP This method is used internally by \fBcreate()\fR to set the session \s-1ID.\s0 .PP This method does not need to be implemented in the class unless an alternative method for session \s-1ID\s0 generation is desired. .SS "validate_id" .IX Subsection "validate_id" Returns true if a session id is of the correct format, or false otherwise. .PP By default, this ensures that the session \s-1ID\s0 is a string of characters from the Base64 schema for \*(L"\s-1URL\s0 Applications\*(R" plus the \f(CW\*(C`~\*(C'\fR character. .PP This method does not need to be implemented in the class unless an alternative set of characters for session IDs is desired. .SS "retrieve" .IX Subsection "retrieve" Return the session object corresponding to the session \s-1ID\s0 given. If none is found, triggers an exception. .PP .Vb 1 \& my $session = MySessionFactory\->retrieve(id => $id); .Ve .PP The method \f(CW\*(C`_retrieve\*(C'\fR must be implemented. It must take \f(CW$id\fR as a single argument and must return a hash reference of session data. .SS "change_id" .IX Subsection "change_id" Changes the session \s-1ID\s0 of the corresponding session. .PP .Vb 1 \& MySessionFactory\->change_id(session => $session_object); .Ve .PP The method \f(CW\*(C`_change_id\*(C'\fR must be implemented. It must take \f(CW$old_id\fR and \&\f(CW$new_id\fR as arguments and change the \s-1ID\s0 from the old one to the new one in the underlying session storage. .SS "destroy" .IX Subsection "destroy" Purges the session object that matches the \s-1ID\s0 given. Returns the \s-1ID\s0 of the destroyed session if succeeded, triggers an exception otherwise. .PP .Vb 1 \& MySessionFactory\->destroy(id => $id); .Ve .PP The \f(CW\*(C`_destroy\*(C'\fR method must be implemented. It must take \f(CW$id\fR as a single argument and destroy the underlying data. .SS "flush" .IX Subsection "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. .PP The Dancer application will not call flush unless the session \f(CW\*(C`is_dirty\*(C'\fR attribute is true to avoid unnecessary writes to the database when no data has been modified. .PP An exception is triggered if the session is unable to be updated in the backend. .PP .Vb 1 \& MySessionFactory\->flush(session => $session); .Ve .PP The \f(CW\*(C`_flush\*(C'\fR method must be implemented. It must take two arguments: the \f(CW$id\fR and a hash reference of session data. .SS "set_cookie_header" .IX Subsection "set_cookie_header" Sets the session cookie into the response object .PP .Vb 5 \& MySessionFactory\->set_cookie_header( \& response => $response, \& session => $session, \& destroyed => undef, \& ); .Ve .PP The \f(CW\*(C`response\*(C'\fR parameter contains a Dancer2::Core::Response object. The \f(CW\*(C`session\*(C'\fR parameter contains a Dancer2::Core::Session object. .PP The \f(CW\*(C`destroyed\*(C'\fR parameter is optional. If true, it indicates the session was marked destroyed by the request context. The default \&\f(CW\*(C`set_cookie_header\*(C'\fR method doesn't need that information, but it is included in case a SessionFactory must handle destroyed sessions differently (such as signalling to middleware). .SS "cookie" .IX Subsection "cookie" Coerce a session object into a Dancer2::Core::Cookie object. .PP .Vb 1 \& MySessionFactory\->cookie(session => $session); .Ve .SS "sessions" .IX Subsection "sessions" Return a list of all session IDs stored in the backend. Useful to create cleaning scripts, in conjunction with session's creation time. .PP The \f(CW\*(C`_sessions\*(C'\fR method must be implemented. It must return an array reference of session IDs (or an empty array reference). .SH "CONFIGURATION" .IX Header "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. .PP .Vb 4 \& engines: \& session: \& Example: \& database_connection: "some_data" .Ve .PP In your session factory: .PP .Vb 3 \& package Dancer2::Session::Example; \& use Moo; \& with "Dancer2::Core::Role::SessionFactory"; \& \& has database_connection => ( is => "ro" ); .Ve .PP You need to do this for every configuration key. The ones that do not have accessors defined will just go to the void. .SH "AUTHOR" .IX Header "AUTHOR" Dancer Core Developers .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2023 by Alexis Sukrieh. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.