| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::Plugin::Session; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::Plugin::Session - Session manager plugin for the Nile framework. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use DateTime; | ||
| 24 | |||
| 25 | # plugin session must be set to autoload in config.xml | ||
| 26 | |||
| 27 | # save current username to session | ||
| 28 | if (!$app->session->{username}) { | ||
| 29 | $app->session->{username} = $username; | ||
| 30 | } | ||
| 31 | |||
| 32 | # get current username from session | ||
| 33 | $username = $app->session->{username}; | ||
| 34 | |||
| 35 | # save time of the user first visit to session | ||
| 36 | if (!$app->session->{first_visit}) { | ||
| 37 | $app->session->{first_visit} = time; | ||
| 38 | } | ||
| 39 | |||
| 40 | my $dt = DateTime->from_epoch(epoch => $app->session->{first_visit}); | ||
| 41 | $view->set("first_visit", $dt->strftime("%a, %d %b %Y %H:%M:%S")); | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | Nile::Plugin::Session - Session manager plugin for the Nile framework. | ||
| 46 | |||
| 47 | Plugin settings in th config file under C<plugin> section. The C<autoload> variable is must be set to true value for the plugin to be loaded | ||
| 48 | on application startup to setup hooks to work before actions dispatch. | ||
| 49 | |||
| 50 | This plugin uses the cache module L<CHI> for saving sessions. All drivers supported by the L<CHI> module are supported by this plugin. | ||
| 51 | |||
| 52 | <plugin> | ||
| 53 | |||
| 54 | <session> | ||
| 55 | <autoload>1</autoload> | ||
| 56 | <key>nile_session_key</key> | ||
| 57 | <expire>1 year</expire> | ||
| 58 | <cache> | ||
| 59 | <driver>File</driver> | ||
| 60 | <root_dir></root_dir> | ||
| 61 | <namespace>session</namespace> | ||
| 62 | </cache> | ||
| 63 | <cookie> | ||
| 64 | <path>/</path> | ||
| 65 | <secure></secure> | ||
| 66 | <domain></domain> | ||
| 67 | <httponly></httponly> | ||
| 68 | </cookie> | ||
| 69 | </session> | ||
| 70 | |||
| 71 | </plugin> | ||
| 72 | |||
| 73 | For DBI driver configuration example: | ||
| 74 | |||
| 75 | <driver> | ||
| 76 | <driver>DBI</driver> | ||
| 77 | <namespace>session</namespace> | ||
| 78 | <table_prefix>cache_</table_prefix> | ||
| 79 | <create_table>1</create_table> | ||
| 80 | </driver> | ||
| 81 | |||
| 82 | The DBI create table example: | ||
| 83 | |||
| 84 | CREATE TABLE <table_prefix><namespace> ( | ||
| 85 | `key` VARCHAR(...), | ||
| 86 | `value` TEXT, | ||
| 87 | PRIMARY KEY (`key`) | ||
| 88 | ) | ||
| 89 | |||
| 90 | The driver will try to create the table if you set C<create_table> in the config and table does not exist. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | use Nile::Plugin; # also extends Nile::Plugin | ||
| 95 | |||
| 96 | use CHI; | ||
| 97 | use Digest::SHA; | ||
| 98 | use Time::HiRes (); | ||
| 99 | use Time::Duration::Parse; | ||
| 100 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 101 | =head2 cache() | ||
| 102 | |||
| 103 | $app->plugin->session->cache(); | ||
| 104 | |||
| 105 | Returns the L<CHI> cache object instance used by the session. All L<CHI> methods can be accessed through this method. | ||
| 106 | |||
| 107 | =head2 get set compute remove expire is_valid add replace append clear purge get_keys exists_and_is_expired | ||
| 108 | |||
| 109 | $app->plugin->session->set($key, $data, "10 minutes"); | ||
| 110 | |||
| 111 | # same as | ||
| 112 | |||
| 113 | $app->plugin->session->cache->set($key, $data, "10 minutes"); | ||
| 114 | |||
| 115 | These methods are a proxy to the L<CHI> cache object methods. See L<CHI> for details about these methods. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | has 'cache' => ( | ||
| 120 | is => 'rw', | ||
| 121 | lazy => 1, | ||
| 122 | #isa => "CHI", | ||
| 123 | default => undef, | ||
| 124 | handles => [qw(get set compute remove expire is_valid add replace append clear purge get_keys exists_and_is_expired)], | ||
| 125 | ); | ||
| 126 | |||
| 127 | =head2 id() | ||
| 128 | |||
| 129 | $id = $app->plugin->session->id(); | ||
| 130 | $app->plugin->session->id($id); | ||
| 131 | |||
| 132 | Returns or sets the current session id. Session id's are auto generated. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | has 'id' => ( | ||
| 137 | is => 'rw', | ||
| 138 | lazy => 1, | ||
| 139 | default => undef | ||
| 140 | ); | ||
| 141 | |||
| 142 | =head2 sha_bits() | ||
| 143 | |||
| 144 | $bits = $app->plugin->session->sha_bits(); | ||
| 145 | |||
| 146 | # bits: 1= 40 bytes, 256=64 bytes, 512=128 bytes, 512224, 512256 | ||
| 147 | |||
| 148 | $bits = 1; | ||
| 149 | $app->plugin->session->sha_bits($bits); | ||
| 150 | |||
| 151 | Returns or sets the current session id generator L<Digest::SHA> sha_bits. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | has 'sha_bits' => ( | ||
| 156 | is => 'rw', | ||
| 157 | lazy => 1, | ||
| 158 | default => 1, # bits: 1= 40 bytes, 256=64 bytes, 512=128 bytes, 512224, 512256 | ||
| 159 | ); | ||
| 160 | |||
| 161 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 162 | sub main { # our sub new {} | ||
| 163 | |||
| 164 | my ($self, $arg) = @_; | ||
| 165 | |||
| 166 | my $app = $self->app; | ||
| 167 | my $setting = $self->setting(); | ||
| 168 | |||
| 169 | my $driver = $setting->{cache} || +{}; | ||
| 170 | |||
| 171 | $setting->{key} ||= "nile_session_key"; | ||
| 172 | $setting->{sha_bits} ||= 1; | ||
| 173 | $setting->{expire} ||= "1 year"; | ||
| 174 | |||
| 175 | $setting->{cookie}->{path} ||= "/"; | ||
| 176 | $setting->{cookie}->{domain} ||= ""; | ||
| 177 | $setting->{cookie}->{secure} ||= ""; | ||
| 178 | $setting->{cookie}->{httponly} ||= ""; | ||
| 179 | |||
| 180 | # convert human readable time to seconds | ||
| 181 | $setting->{expire} = parse_duration($setting->{expire}); | ||
| 182 | |||
| 183 | $self->sha_bits($setting->{sha_bits}); | ||
| 184 | |||
| 185 | $driver->{driver} ||= "File"; | ||
| 186 | $driver->{namespace} ||= "session"; # default namespace is Default | ||
| 187 | |||
| 188 | if (!$driver->{root_dir}) { | ||
| 189 | $driver->{root_dir} = $app->file->catdir($app->var->get("cache_dir"), $driver->{namespace}); | ||
| 190 | } | ||
| 191 | |||
| 192 | if ($driver->{driver} eq "DBI") { | ||
| 193 | $driver->{dbh} = $app->dbh(); | ||
| 194 | } | ||
| 195 | |||
| 196 | if (!$self->cache) { | ||
| 197 | $self->cache(CHI->new(%{$driver})); | ||
| 198 | } | ||
| 199 | |||
| 200 | # load session data after loading request | ||
| 201 | $app->hook->after_request(sub { | ||
| 202 | my ($me, @args) = @_; | ||
| 203 | #say "key: " . $app->request->cookie($setting->{key}); | ||
| 204 | $self->id($app->request->cookie($setting->{key}) || $app->request->param($setting->{key}) || undef); | ||
| 205 | |||
| 206 | if ($self->id) { | ||
| 207 | $app->session($self->cache->get($self->id) || +{}); | ||
| 208 | } | ||
| 209 | else { | ||
| 210 | $app->session(+{}); | ||
| 211 | } | ||
| 212 | |||
| 213 | #$app->dump($app->session); | ||
| 214 | }); | ||
| 215 | |||
| 216 | # save session data, write session headers etc | ||
| 217 | $app->hook->before_response(sub { | ||
| 218 | my ($me, @args) = @_; | ||
| 219 | |||
| 220 | #$cache->set( $name, $customer, "10 minutes" ); | ||
| 221 | # do not save empty sessions; | ||
| 222 | return if (!$app->session); | ||
| 223 | |||
| 224 | # create new session and save it | ||
| 225 | if (!$self->id()) { | ||
| 226 | $self->id($self->new_id()); | ||
| 227 | $self->cache->set($self->id(), +{}); | ||
| 228 | |||
| 229 | $app->response->cookies->{$setting->{key}} = { | ||
| 230 | value => $self->id(), | ||
| 231 | expires => time + $setting->{expire}, | ||
| 232 | path => $setting->{cookie}->{path}, | ||
| 233 | domain => $setting->{cookie}->{domain}, | ||
| 234 | secure => $setting->{cookie}->{secure}, | ||
| 235 | httponly => $setting->{cookie}->{httponly}, | ||
| 236 | }; | ||
| 237 | } | ||
| 238 | |||
| 239 | $self->cache->set($self->id, $app->session, $setting->{expire}); | ||
| 240 | }); | ||
| 241 | |||
| 242 | } | ||
| 243 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 244 | sub new_id { | ||
| 245 | my ($self) = @_; | ||
| 246 | my ($sec, $ms) = Time::HiRes::gettimeofday; | ||
| 247 | my $rand = sprintf("%.f", $$ * (+{}) * $ms * rand()); | ||
| 248 | # bits: 1= 40 bytes, 256=64 bytes, 512=128 bytes, 512224, 512256 | ||
| 249 | return Digest::SHA->new($self->sha_bits())->add($rand)->hexdigest(); | ||
| 250 | } | ||
| 251 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 252 | |||
| 253 | =pod | ||
| 254 | |||
| 255 | =head1 Bugs | ||
| 256 | |||
| 257 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 258 | |||
| 259 | =head1 HOMEPAGE | ||
| 260 | |||
| 261 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 262 | |||
| 263 | =head1 SOURCE | ||
| 264 | |||
| 265 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 266 | |||
| 267 | =head1 SEE ALSO | ||
| 268 | |||
| 269 | See L<Nile> for details about the complete framework. | ||
| 270 | |||
| 271 | =head1 AUTHOR | ||
| 272 | |||
| 273 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 274 | Website: http://www.mewsoft.com | ||
| 275 | |||
| 276 | =head1 COPYRIGHT AND LICENSE | ||
| 277 | |||
| 278 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 279 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 280 | |||
| 281 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | 1; |