Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/Session.pm

raw
1 ##@file
2 # Base package for LemonLDAP::NG session object
3
4 ##@class
5 # Specify a session object, how to create/update/remove session
6
7 package Lemonldap::NG::Common::Session;
8
9 use strict;
10 use Exporter 'import';
11 use Digest::SHA;
12 use JSON;
13 use Lemonldap::NG::Common::Apache::Session;
14 use Lemonldap::NG::Common::Apache::Session::Generate::SHA256;
15
16 our $VERSION = '2.20.0';
17
18 # Export method needed to handle hashed storage
19 our @EXPORT = qw(id2storage hashedKinds reHashedKinds);
20
21 use constant hashedKinds => ( 'SSO', 'OIDC', 'CDA' );
22
23 sub reHashedKinds {
24 my $s = '^(' . join( '|', hashedKinds() ) . ')$';
25 return qr/$s/;
26 }
27
28 # Workaround for another ModPerl/Mouse issue...
29 BEGIN {
30 require Mouse;
31 no warnings;
32 my $v =
33 $Mouse::VERSION
34 ? sprintf( "%d.%03d%03d", ( $Mouse::VERSION =~ /(\d+)/g ) )
35 : 0;
36 if ( $v < 2.005001 and $Lemonldap::NG::Handler::Apache2::Main::VERSION ) {
37 require Moose;
38 Moose->import();
39 }
40 else {
41 Mouse->import();
42 }
43 }
44
45 # Convert a session ID into store entry
46 sub id2storage {
47 return $_[0] ? Digest::SHA::sha256_hex( $_[0] ) : undef;
48 }
49
50 sub randomId {
51 my $tmp = {};
52 &Lemonldap::NG::Common::Apache::Session::Generate::SHA256::generate($tmp);
53 return $tmp->{data}->{_session_id};
54 }
55
56 has id => (
57 is => 'rw',
58 isa => 'Str|Undef',
59 trigger => sub {
60 $_[0]->{storageId} =
61 ( $_[0]->hashStore && $_[0]->id )
62 ? id2storage( $_[0]->id )
63 : $_[0]->id;
64 }
65 );
66
67 has storageId => ( is => 'rw', );
68
69 has force => (
70 is => 'rw',
71 isa => 'Bool',
72 default => 0,
73 );
74
75 has kind => (
76 is => 'rw',
77 isa => 'Str|Undef',
78 );
79
80 has data => (
81 is => 'rw',
82 isa => 'HashRef',
83 default => sub { {} },
84 );
85
86 has options => (
87 is => 'rw',
88 isa => 'HashRef',
89 );
90
91 has storageModule => (
92 is => 'ro',
93 isa => 'Str',
94 required => 1,
95 );
96
97 has storageModuleOptions => (
98 is => 'ro',
99 isa => 'HashRef|Undef',
100 );
101
102 has cacheModule => (
103 is => 'rw',
104 isa => 'Str|Undef',
105 );
106
107 has cacheModuleOptions => (
108 is => 'rw',
109 isa => 'HashRef|Undef',
110 );
111
112 has error => (
113 is => 'rw',
114 isa => 'Str|Undef',
115 );
116
117 has info => ( is => 'rw' );
118
119 has timeout => ( is => 'rw', default => 5 );
120
121 has hashStore => ( is => 'rw' );
122
123 sub BUILD {
124 my ($self) = @_;
125
126 # Load Apache::Session module
127 unless ( $self->storageModule->can('populate') ) {
128 eval "require " . $self->storageModule;
129 return undef if $@;
130 }
131
132 # Register options for common Apache::Session module
133 my $moduleOptions = $self->storageModuleOptions || {};
134 $self->timeout( delete $moduleOptions->{timeout} )
135 if $moduleOptions->{timeout};
136 my %options = (
137 %$moduleOptions,
138 backend => $self->storageModule,
139 localStorage => $self->cacheModule,
140 localStorageOptions => $self->cacheModuleOptions
141 );
142
143 $self->options( \%options );
144
145 my $data = $self->_tie_session;
146
147 # Is it a session creation request?
148 my $creation = 1
149 if ( !$self->id or ( $self->id and !$data and $self->force ) );
150
151 # If session id was submitted but session is not found
152 # And we want to force id
153 # Then use setId to create session
154 if ( $self->id and $creation ) {
155 $options{setId} = $self->id;
156 $self->options( \%options );
157 $self->id(undef);
158 $self->error(undef);
159 $data = $self->_tie_session;
160 }
161
162 if ( $self->{info} ) {
163 foreach ( keys %{ $self->{info} } ) {
164 next if ( $_ eq "_session_id" and $data->{_session_id} );
165 next if ( $_ eq "_session_kind" and $data->{_session_kind} );
166 if ( defined $self->{info}->{$_} ) {
167 $data->{$_} = $self->{info}->{$_};
168 }
169 else {
170 delete $data->{$_};
171 }
172 }
173 delete $self->{info};
174 }
175
176 # If session is created
177 # Then set session kind in session
178 if ( $creation and $self->kind ) {
179 $data->{_session_kind} = $self->kind;
180 }
181
182 # Load session data into object
183 if ($data) {
184 if ( $self->kind and $data->{_session_kind} ) {
185 unless ( $data->{_session_kind} eq $self->kind ) {
186 $self->error(
187 "Session kind mismatch: $data->{_session_kind} is not "
188 . $self->kind );
189 return undef;
190 }
191 }
192 $self->_save_data($data);
193 $self->kind( $data->{_session_kind} );
194 $self->id( $data->{_session_id} );
195 if ( $self->hashStore and $self->id ) {
196 $self->_hashDataSessionId($data);
197 $data->{_session_hashed} ||= 1;
198 }
199
200 untie(%$data);
201 }
202 }
203
204 sub _tie_session {
205 my $self = $_[0];
206 my $options = $_[1] || {};
207 my %h;
208
209 # Secured storage for new session: generate a new random ID and calculate
210 # the storage ID
211 my $securedId = $self->id;
212 if ( $self->hashStore ) {
213 if ( !$self->id ) {
214 my $id = $self->options->{setId} || randomId();
215 $securedId = $id;
216 $self->storageId( id2storage($securedId) );
217 $self->options->{setId} = $options->{setId} = $self->storageId;
218 $self->error(undef);
219 }
220 }
221
222 eval {
223 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
224 eval {
225 alarm $self->timeout;
226
227 # SOAP/REST session module must be directly tied
228 if ( $self->storageModule =~
229 /^Lemonldap::NG::Common::Apache::Session/ )
230 {
231 tie %h, $self->storageModule,
232 ( $options->{setId} ? $self->id : $self->storageId ),
233 { %{ $self->options }, %$options, kind => $self->kind };
234 }
235 else {
236 tie %h, 'Lemonldap::NG::Common::Apache::Session',
237 ( $options->{setId} ? $self->id : $self->storageId ),
238 { %{ $self->options }, %$options };
239 }
240 };
241 alarm 0;
242 die $@ if $@;
243
244 };
245 if ( $@ or not tied(%h) ) {
246 my $msg = "Session cannot be tied";
247 $msg .= ": $@" if $@;
248 $self->error($msg);
249 return undef;
250 }
251 if ( $self->hashStore ) {
252
253 # Before returning the session, set here the real cookie value
254 my $status = tied(%h)->{status};
255 $h{_session_id} = $securedId;
256 tied(%h)->{status} = $status;
257 }
258
259 return \%h;
260 }
261
262 sub _save_data {
263 my ( $self, $data ) = @_;
264
265 my %saved_data = %$data;
266 $self->data( \%saved_data );
267 }
268
269 sub update {
270 my ( $self, $infos, $tieOptions ) = @_;
271
272 unless ( ref $infos eq "HASH" ) {
273 $self->error("You need to provide a HASHREF");
274 return 0;
275 }
276
277 my $data = $self->_tie_session(
278 { ( $tieOptions ? %$tieOptions : () ), noCache => 1 } );
279
280 if ($data) {
281 foreach ( keys %$infos ) {
282 if ( defined $infos->{$_} ) {
283 $data->{$_} = $infos->{$_};
284 }
285 else {
286 delete $data->{$_};
287 }
288 }
289
290 $self->_save_data($data);
291 $self->id( $data->{_session_id} );
292 if ( $self->hashStore and $self->id ) {
293 $self->_hashDataSessionId($data);
294 $data->{_session_hashed} ||= 1;
295 }
296
297 untie(%$data);
298 return 1;
299 }
300
301 $self->error("No data found in session");
302 return 0;
303 }
304
305 sub remove {
306 my ( $self, $tieOptions ) = @_;
307
308 my $data = $self->_tie_session($tieOptions);
309 unless ($data) {
310 $self->error("Unable to delete session: $@");
311 return 0;
312 }
313
314 # Before saving, hide the real ID and replace it by the storage ID
315 $self->_hashDataSessionId($data) if $self->hashStore;
316
317 eval { tied(%$data)->delete(); };
318
319 if ($@) {
320 $self->error("Unable to delete session: $@");
321 return 0;
322 }
323
324 return 1;
325 }
326
327 sub _hashDataSessionId {
328 my ( $self, $data, $id ) = @_;
329 my $nid = id2storage( $id || $self->id );
330 if ( $nid ne $data->{_session_id} ) {
331 my $status = tied(%$data)->{status};
332 $data->{_session_id} = id2storage( $self->id );
333 tied(%$data)->{status} = $status;
334 }
335 }
336
337 no Mouse;
338
339 1;