blib/lib/Web/Authenticate/Session/Handler.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 87 CC 6 SCAR 15.8
CC
6
Cov
0%
create_session
12.0
delete_session
6.0
update_expires
6.0
CRAP 4.8
1 use strict;
2 package Web::Authenticate::Session::Handler;
3 $Web::Authenticate::Session::Handler::VERSION = '0.003';
4 use Mouse;
5 use Carp;
6 use DateTime;
7 use Session::Token;
8 use Web::Authenticate::Cookie::Handler;
9 #ABSTRACT: The default implementation of Web::Authentication::Session::Handler::Role.
10  
11 with 'Web::Authenticate::Session::Handler::Role';
12  
13  
14 has session_storage_handler => (
15 does => 'Web::Authenticate::Session::Storage::Handler::Role',
16 is => 'ro',
17 required => 1,
18 );
19  
20  
21 has cookie_handler => (
22 does => 'Web::Authenticate::Cookie::Handler::Role',
23 is => 'ro',
24 required => 1,
25 default => sub { Web::Authenticate::Cookie::Handler->new },
26 );
27  
28  
29 has session_expires_in_seconds => (
30 isa => 'Int',
31 is => 'ro',
32 required => 1,
33 default => 86400,
34 );
35  
36  
37 has session_id_cookie_name => (
38 isa => 'Str',
39 is => 'ro',
40 required => 1,
41 default => 'session_id',
42 );
43  
44  
45 sub create_session {
46 my ($self, $user) = @_;
47 croak "must provide user" unless $user;
48  
49 my $session_id = Session::Token->new(entropy => 256)->get;
50 my $session = $self->session_storage_handler->store_session($user, $session_id, $self->_get_expires);
51  
52 return unless $session;
53  
54 $self->_set_session_cookie($session_id);
55  
56 return $session;
57 }
58  
59  
60 sub delete_session {
61 my ($self) = @_;
62  
63 my $session_id = $self->_get_session_id;
64 return unless $session_id;
65  
66 $self->session_storage_handler->delete_session($session_id);
67 $self->cookie_handler->delete_cookie($self->session_id_cookie_name);
68 }
69  
70  
71 sub update_expires {
72 my ($self) = @_;
73 my $session_id = $self->_get_session_id;
74  
75 return unless $session_id;
76  
77 $self->session_storage_handler->update_expires($session_id, $self->_get_expires);
78 $self->_set_session_cookie($session_id);
79 }
80  
81  
82 sub invalidate_user_sessions {
83 my ($self, $user) = @_;
84 $self->session_storage_handler->invalidate_user_sessions($user);
85 }
86  
87  
88 sub get_session {
89 my ($self) = @_;
90  
91 my $session_id = $self->_get_session_id;
92  
93 return unless $session_id;
94  
95 return $self->session_storage_handler->load_session($session_id);
96 }
97  
98 sub _get_session_id {
99 my ($self) = @_;
100 return $self->cookie_handler->get_cookie($self->session_id_cookie_name);
101 }
102  
103 sub _get_expires {
104 my ($self) = @_;
105 return DateTime->now->add(seconds => $self->session_expires_in_seconds)->epoch;
106 }
107  
108 sub _set_session_cookie {
109 my ($self, $session_id) = @_;
110 $self->cookie_handler->set_cookie($self->session_id_cookie_name, $session_id, $self->session_expires_in_seconds);
111 }
112  
113 1;
114  
115 __END__
116  
117 =pod
118  
119 =encoding UTF-8
120  
121 =head1 NAME
122  
123 Web::Authenticate::Session::Handler - The default implementation of Web::Authentication::Session::Handler::Role.
124  
125 =head1 VERSION
126  
127 version 0.003
128  
129 =head1 METHODS
130  
131 =head2 session_storage_handler
132  
133 Sets the object that does L<Web::Authenticate::Sesssion::Storage::Handler::Role>. This is required an has no default.
134  
135 =head2 cookie_handler
136  
137 Sets the object that does L<Web::Authenticate::Cookie::Handler::Role>. This is required. Default is L<Web::Authenticate::Cookie::Handler>.
138  
139 =head2 session_expires_in_seconds
140  
141 Sets the amount of time a session will expire in after last successful page load. Default is 86,400 (one day).
142  
143 =head2 session_id_cookie_name
144  
145 Sets the session_id cookie name. Default is 'session_id'.
146  
147 =head2 create_session
148  
149 Creates session for the user and stores the cookie for the session in the user's browser.
150  
151 =head2 delete_session
152  
153 Deletes the current session.
154  
155 $session_handler->delete_session;
156  
157 =head2 update_expires
158  
159 Updates the expires time for the logged in user to now + L</session_expires_in_seconds>.
160  
161 =head2 invalidate_user_sessions
162  
163 Deletes all sessions for user.
164  
165 =head2 get_session
166  
167 Returns the session for the logged in user. Returns undef if there is no session.
168  
169 =head1 AUTHOR
170  
171 Adam Hopkins <srchulo@cpan.org>
172  
173 =head1 COPYRIGHT AND LICENSE
174  
175 This software is copyright (c) 2017 by Adam Hopkins.
176  
177 This is free software; you can redistribute it and/or modify it under
178 the same terms as the Perl 5 programming language system itself.
179  
180 =cut