| 1 | ############################################################################## | ||
| 2 | ## | ||
| 3 | ## Web::Reactor application machinery | ||
| 4 | ## 2013-2016 (c) Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | ## <cade@bis.bg> <cade@biscom.net> <cade@cpan.org> | ||
| 6 | ## | ||
| 7 | ## LICENSE: GPLv2 | ||
| 8 | ## | ||
| 9 | ############################################################################## | ||
| 10 | package Web::Reactor::Sessions; | ||
| 11 | use strict; | ||
| 12 | use Exception::Sink; | ||
| 13 | |||
| 14 | use parent 'Web::Reactor::Base'; | ||
| 15 | |||
| 16 | sub new | ||
| 17 | { | ||
| 18 | my $class = shift; | ||
| 19 | my %env = @_; | ||
| 20 | |||
| 21 | $class = ref( $class ) || $class; | ||
| 22 | my $self = { | ||
| 23 | 'ENV' => \%env, | ||
| 24 | }; | ||
| 25 | bless $self, $class; | ||
| 26 | |||
| 27 | return $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | ############################################################################## | ||
| 31 | ## | ||
| 32 | ## public interface methods, should be used via Reactor object, see specs | ||
| 33 | ## | ||
| 34 | |||
| 35 | # create new session id of given type and length and allocate storage for it | ||
| 36 | # args: | ||
| 37 | # type -- alphanumeric type name (selects storage only) | ||
| 38 | # len -- session id length | ||
| 39 | # returns: | ||
| 40 | # new session id or undef when failed | ||
| 41 | sub create | ||
| 42 | { | ||
| 43 | my $self = shift; | ||
| 44 | my $type = uc shift; | ||
| 45 | my $len = shift || 128; | ||
| 46 | |||
| 47 | die "Web::Reactor::Sessions::create: invalid type, expected ALPHANUMERIC, got [$type]" unless $type =~ /^[A-Z0-9]+$/; | ||
| 48 | die "Web::Reactor::Sessions::create: invalid length, expected len > 0, got [$len]" unless $len > 0; | ||
| 49 | |||
| 50 | my $env = $self->_renv(); | ||
| 51 | |||
| 52 | my $id; | ||
| 53 | my $t = time(); | ||
| 54 | my $to = $env->{ 'SESS_CREATE_TIMEOUT' } || 5; # seconds | ||
| 55 | while(4) | ||
| 56 | { | ||
| 57 | $id = $self->create_id( $len ); | ||
| 58 | |||
| 59 | my @key = $self->compose_key_from_id( $type, $id ); | ||
| 60 | last if $self->_storage_create( @key ); | ||
| 61 | |||
| 62 | if ( time() - $t > $to ) | ||
| 63 | { | ||
| 64 | $id = undef; | ||
| 65 | # FIXME: report error | ||
| 66 | die "Web::Reactor::Sessions::create: cannot create new session: timeout, key[@key]"; | ||
| 67 | return undef; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | return $id; | ||
| 72 | }; | ||
| 73 | |||
| 74 | # loads session data from the storage | ||
| 75 | # args: | ||
| 76 | # type -- alphanumeric type name (selects storage only) | ||
| 77 | # id -- session id | ||
| 78 | # returns: | ||
| 79 | # hashref of session data or undef if error | ||
| 80 | sub load | ||
| 81 | { | ||
| 82 | my $self = shift; | ||
| 83 | my $type = shift; | ||
| 84 | my $id = shift; | ||
| 85 | |||
| 86 | return 0 unless $id; | ||
| 87 | |||
| 88 | my @key = $self->compose_key_from_id( $type, $id ); | ||
| 89 | |||
| 90 | return $self->_storage_load( @key ); | ||
| 91 | } | ||
| 92 | |||
| 93 | # saves session data to the storage | ||
| 94 | # args: | ||
| 95 | # type -- alphanumeric type name (selects storage only) | ||
| 96 | # id -- session id | ||
| 97 | # data -- hashref of session data | ||
| 98 | # returns: | ||
| 99 | # 1 if successful, undef if failed | ||
| 100 | sub save | ||
| 101 | { | ||
| 102 | my $self = shift; | ||
| 103 | my $type = shift; | ||
| 104 | my $id = shift; | ||
| 105 | my $data = shift; | ||
| 106 | |||
| 107 | return 0 unless $id; | ||
| 108 | |||
| 109 | my @key = $self->compose_key_from_id( $type, $id ); | ||
| 110 | |||
| 111 | return $self->_storage_save( $data, @key ); | ||
| 112 | } | ||
| 113 | |||
| 114 | # deletes a session from the storage | ||
| 115 | # args: | ||
| 116 | # type -- alphanumeric type name (selects storage only) | ||
| 117 | # id -- session id | ||
| 118 | # returns: | ||
| 119 | # 1 if successful, undef if failed | ||
| 120 | sub delete | ||
| 121 | { | ||
| 122 | my $self = shift; | ||
| 123 | my $type = shift; | ||
| 124 | my $id = shift; | ||
| 125 | |||
| 126 | return 0 unless $id; | ||
| 127 | |||
| 128 | my @key = $self->compose_key_from_id( $type, $id ); | ||
| 129 | |||
| 130 | return $self->_storage_delete( @key ); | ||
| 131 | } | ||
| 132 | |||
| 133 | # checks if session exists in the storage | ||
| 134 | # args: | ||
| 135 | # type -- alphanumeric type name (selects storage only) | ||
| 136 | # id -- session id | ||
| 137 | # returns: | ||
| 138 | # 1 if exists, undef if not | ||
| 139 | sub exists | ||
| 140 | { | ||
| 141 | my $self = shift; | ||
| 142 | my $type = shift; | ||
| 143 | my $id = shift; | ||
| 144 | |||
| 145 | return 0 unless $id; | ||
| 146 | |||
| 147 | my @key = $self->compose_key_from_id( $type, $id ); | ||
| 148 | |||
| 149 | return $self->_storage_exists( @key ); | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | ############################################################################## | ||
| 154 | ## | ||
| 155 | ## methods, which must be implemented in sub-classes! | ||
| 156 | ## they are all internal to this package | ||
| 157 | ## | ||
| 158 | |||
| 159 | # create new session storage indexed by the given key components | ||
| 160 | # it is important that this function try to do atomic create in the storage. | ||
| 161 | # it must (and expected to) fail if session with the same key exists and never | ||
| 162 | # overwrite existing session storage! | ||
| 163 | # args: | ||
| 164 | # @key (i.e. @_) -- key components array, usually filled with 2 or 3 elements | ||
| 165 | # when 2: TYPE, SESS7c87y32d78asa4 | ||
| 166 | # when 3: TYPE, SESS187yc5v87thccf, SESS2jdhfh74yc3847 | ||
| 167 | # usually it is simple to $key = join '.' @_; | ||
| 168 | # | ||
| 169 | # returns: | ||
| 170 | # 1 if successful or 0 or undef if not possible or session id already exists | ||
| 171 | sub _storage_create { die "Web::Reactor::Sessions::*::_storage_create() is not implemented!"; } | ||
| 172 | |||
| 173 | # loads session data from the storage | ||
| 174 | # args: | ||
| 175 | # @key (i.e. @_) -- key components array, example: $key = join '.' @_; | ||
| 176 | # returns: | ||
| 177 | # hashref of session data or undef if error | ||
| 178 | sub _storage_load { die "Web::Reactor::Sessions::*::_storage_load() is not implemented!"; } | ||
| 179 | |||
| 180 | # saves session data to the storage | ||
| 181 | # args: | ||
| 182 | # data -- hashref of session data | ||
| 183 | # @key (i.e. @_) -- key components array, example: $key = join '.' @_; | ||
| 184 | # returns: | ||
| 185 | # 1 if successful, undef if failed | ||
| 186 | sub _storage_save { die "Web::Reactor::Sessions::*::_storage_save() is not implemented!"; } | ||
| 187 | |||
| 188 | # checks if session exists in the storage | ||
| 189 | # args: | ||
| 190 | # @key (i.e. @_) -- key components array, example: $key = join '.' @_; | ||
| 191 | # returns: | ||
| 192 | # 1 if exists, undef if not | ||
| 193 | sub _storage_exists { die "Web::Reactor::Sessions::*::_storage_exists() is not implemented!"; } | ||
| 194 | |||
| 195 | ############################################################################## | ||
| 196 | ## | ||
| 197 | ## | ||
| 198 | ## | ||
| 199 | |||
| 200 | # return string with new session id with given LEN argument or default length | ||
| 201 | # args: | ||
| 202 | # len -- session id length (optional, 0 or undef for default length) | ||
| 203 | # letters -- letters to be used for session id creation (optional) | ||
| 204 | # must be non-whitespace characters string with no duplicates | ||
| 205 | # returns: | ||
| 206 | # id -- session id string | ||
| 207 | sub create_id | ||
| 208 | { | ||
| 209 | my $self = shift; | ||
| 210 | my $env = $self->_renv(); | ||
| 211 | |||
| 212 | my $len = shift() || $env->{ 'SESS_LENGTH' } || 128; | ||
| 213 | my $let = shift() || $env->{ 'SESS_LETTERS' } || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
| 214 | |||
| 215 | my $l = length( $let ); | ||
| 216 | my $id; | ||
| 217 | # FIXME: move this line to function in Utils.pm | ||
| 218 | $id .= substr( $let, int(rand() * $l), 1 ) for ( 1 .. $len ); | ||
| 219 | return $id; | ||
| 220 | }; | ||
| 221 | |||
| 222 | sub compose_key_from_id | ||
| 223 | { | ||
| 224 | my $self = shift; | ||
| 225 | my $type = uc shift; | ||
| 226 | my $id = shift; | ||
| 227 | |||
| 228 | boom "Web::Reactor::Sessions::compose_key_from_id: invalid type, expected ALPHANUMERIC" unless $type =~ /^[A-Z0-9]+$/; | ||
| 229 | |||
| 230 | my @key; | ||
| 231 | |||
| 232 | push @key, $type; | ||
| 233 | push @key, $self->get_user_sid() if $type ne 'USER'; # FIXME: not only! UPDs also! (user permanent data) | ||
| 234 | push @key, $id; | ||
| 235 | |||
| 236 | return @key; | ||
| 237 | } | ||
| 238 | |||
| 239 | ############################################################################## | ||
| 240 | ## | ||
| 241 | ## helpers | ||
| 242 | ## | ||
| 243 | |||
| 244 | sub get_user_sid | ||
| 245 | { | ||
| 246 | my $self = shift; | ||
| 247 | my $user_sid = $self->{ 'REO_REACTOR' }->{ 'SESSIONS' }{ 'SID' }{ 'USER' }; | ||
| 248 | |||
| 249 | boom "missing USER SESSION" unless $user_sid; | ||
| 250 | |||
| 251 | return $user_sid; | ||
| 252 | } | ||
| 253 | |||
| 254 | # return ENV hash reference from the reactor | ||
| 255 | # FIXME: TODO: move to Web::Reactor::Base::get_env() | ||
| 256 | sub _renv | ||
| 257 | { | ||
| 258 | my $self = shift; | ||
| 259 | |||
| 260 | $self->get_reo()->{ 'ENV' }; | ||
| 261 | } | ||
| 262 | |||
| 263 | #sub DESTROY | ||
| 264 | #{ | ||
| 265 | # my $self = shift; | ||
| 266 | # | ||
| 267 | # print "DESTROY: $self\n"; | ||
| 268 | #} | ||
| 269 | |||
| 270 | ############################################################################## | ||
| 271 | 1; | ||
| 272 | ###EOF######################################################################## |