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