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

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 95 CC 7 SCAR 17.3
CC
7
Cov
0%
create_session
12.0
get_session
12.0
delete_session
6.0
CRAP 5.6
1 use strict;
2 package Web::Authenticate::Session::Handler;
3 $Web::Authenticate::Session::Handler::VERSION = '0.004';
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->_delete_session_cookie;
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 my $session = $self->session_storage_handler->load_session($session_id);
96  
97 $self->_delete_session_cookie unless $session;
98  
99 return $session;
100 }
101  
102 sub _get_session_id {
103 my ($self) = @_;
104 return $self->cookie_handler->get_cookie($self->session_id_cookie_name);
105 }
106  
107 sub _get_expires {
108 my ($self) = @_;
109 return DateTime->now->add(seconds => $self->session_expires_in_seconds)->epoch;
110 }
111  
112 sub _set_session_cookie {
113 my ($self, $session_id) = @_;
114 $self->cookie_handler->set_cookie($self->session_id_cookie_name, $session_id, $self->session_expires_in_seconds);
115 }
116  
117 sub _delete_session_cookie {
118 my ($self) = @_;
119 $self->cookie_handler->delete_cookie($self->session_id_cookie_name);
120 }
121  
122 1;
123  
124 __END__
125  
126 =pod
127  
128 =encoding UTF-8
129  
130 =head1 NAME
131  
132 Web::Authenticate::Session::Handler - The default implementation of Web::Authentication::Session::Handler::Role.
133  
134 =head1 VERSION
135  
136 version 0.004
137  
138 =head1 METHODS
139  
140 =head2 session_storage_handler
141  
142 Sets the object that does L<Web::Authenticate::Sesssion::Storage::Handler::Role>. This is required an has no default.
143  
144 =head2 cookie_handler
145  
146 Sets the object that does L<Web::Authenticate::Cookie::Handler::Role>. This is required. Default is L<Web::Authenticate::Cookie::Handler>.
147  
148 =head2 session_expires_in_seconds
149  
150 Sets the amount of time a session will expire in after last successful page load. Default is 86,400 (one day).
151  
152 =head2 session_id_cookie_name
153  
154 Sets the session_id cookie name. Default is 'session_id'.
155  
156 =head2 create_session
157  
158 Creates session for the user and stores the cookie for the session in the user's browser.
159  
160 =head2 delete_session
161  
162 Deletes the current session.
163  
164 $session_handler->delete_session;
165  
166 =head2 update_expires
167  
168 Updates the expires time for the logged in user to now + L</session_expires_in_seconds>.
169  
170 =head2 invalidate_user_sessions
171  
172 Deletes all sessions for user.
173  
174 =head2 get_session
175  
176 Returns the session for the logged in user. Returns undef if there is no session. If an invalid
177 session is found in the user's cookies, that session will be deleted from their cookies.
178  
179 =head1 AUTHOR
180  
181 Adam Hopkins <srchulo@cpan.org>
182  
183 =head1 COPYRIGHT AND LICENSE
184  
185 This software is copyright (c) 2017 by Adam Hopkins.
186  
187 This is free software; you can redistribute it and/or modify it under
188 the same terms as the Perl 5 programming language system itself.
189  
190 =cut