Debian Codegraph @

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

raw
1 ## @file
2 # Client side of the SOAP proxy mechanism for Apache::Session modules
3
4 ## @class
5 # Client side of the SOAP proxy mechanism for Apache::Session modules
6 package Lemonldap::NG::Common::Apache::Session::SOAP;
7
8 use strict;
9 use SOAP::Lite;
10 use JSON qw(from_json to_json);
11
12 our $VERSION = '2.21.0';
13
14 #parameter proxy Url of SOAP service
15 #parameter proxyOptions SOAP::Lite options
16 #parameter User Username
17 #parameter Password Password
18 #parameter localStorage Cache module
19 #parameter localStorageOptions Cache module options
20
21 # Variables shared with SOAP::Transport::HTTP::Client
22 our ( $user, $password ) = ( '', '' );
23
24 BEGIN {
25
26 sub SOAP::Transport::HTTP::Client::get_basic_credentials {
27 return $Lemonldap::NG::Common::Apache::Session::SOAP::user =>
28 $Lemonldap::NG::Common::Apache::Session::SOAP::password;
29 }
30 }
31
32 # PUBLIC INTERFACE
33
34 ## @cmethod Lemonldap::NG::Common::Apache::Session::SOAP TIEHASH(string session_id, hashRef args)
35 # Constructor for Perl TIE mechanism. See perltie(3) for more.
36 # @return Lemonldap::NG::Common::Apache::Session::SOAP object
37 sub TIEHASH {
38 my $class = shift;
39
40 my $session_id = shift;
41 my $args = shift;
42 my ( $proxy, $proxyOptions );
43 die "proxy argument is required"
44 unless ( $args and $args->{proxy} );
45 my $self = {
46 data => { _session_id => $session_id },
47 modified => 0,
48 };
49 foreach (qw(proxy proxyOptions ns localStorage localStorageOptions)) {
50 $self->{$_} = $args->{$_};
51 }
52 ( $user, $password ) = ( $args->{User}, $args->{Password} );
53 bless $self, $class;
54
55 if ( defined $session_id && $session_id ) {
56 die "unexistant session $session_id"
57 unless ( $self->get($session_id) );
58 }
59 else {
60 die "unable to create session"
61 unless ( $self->newSession() );
62 }
63 return $self;
64 }
65
66 sub FETCH {
67 my $self = shift;
68 my $key = shift;
69 return $self->{data}->{$key};
70 }
71
72 sub STORE {
73 my $self = shift;
74 my $key = shift;
75 my $value = shift;
76
77 $self->{data}->{$key} = $value;
78 $self->{modified} = 1 unless $key eq '_session_id';
79 return $value;
80 }
81
82 sub DELETE {
83 my $self = shift;
84 my $key = shift;
85
86 $self->{modified} = 1;
87
88 delete $self->{data}->{$key};
89 }
90
91 sub CLEAR {
92 my $self = shift;
93
94 $self->{modified} = 1;
95
96 $self->{data} = {};
97 }
98
99 sub EXISTS {
100 my $self = shift;
101 my $key = shift;
102 return exists $self->{data}->{$key};
103 }
104
105 sub FIRSTKEY {
106 my $self = shift;
107 my $reset = keys %{ $self->{data} };
108 return each %{ $self->{data} };
109 }
110
111 sub NEXTKEY {
112 my $self = shift;
113 return each %{ $self->{data} };
114 }
115
116 sub DESTROY {
117 my $self = shift;
118 $self->save;
119 }
120
121 ## @method private SOAP::Lite _connect()
122 # @return The SOAP::Lite object. Build it at the first call.
123 sub _connect {
124 my $self = shift;
125 return $self->{service} if ( $self->{service} );
126 my @args = ( $self->{proxy} );
127 if ( $self->{proxyOptions} ) {
128 push @args, %{ $self->{proxyOptions} };
129 }
130 $self->{ns} ||= 'urn:Lemonldap/NG/Common/PSGI/SOAPService';
131 return $self->{service} = SOAP::Lite->ns( $self->{ns} )->proxy(@args);
132 }
133
134 ## @method private $ _soapCall(string func, @args)
135 # @param $func remote function to call
136 # @param @args Functions parameters
137 # @return Result
138 sub _soapCall {
139 my $self = shift;
140 my $func = shift;
141 my $r = $self->_connect->$func(@_);
142 if ( $r->fault ) {
143 print STDERR "SOAP Error: " . $r->fault->{faultstring};
144 return ();
145 }
146 return $r->result;
147 }
148
149 ## @method hashRef get(string id)
150 # @param $id Apache::Session session ID.
151 # @return User data
152 sub get {
153 my $self = shift;
154 my $id = shift;
155
156 # Check cache
157 if ( $self->{localStorage} and my $res = $self->cache->get($id) ) {
158 $res = eval { from_json($res) };
159 return $self->{data} = $res if $res;
160 }
161
162 # No cache, use SOAP and set cache
163 my $r = $self->_soapCall( "getAttributes", $id );
164 return 0 unless ( $r or $r->{error} );
165 $self->{data} = $r->{attributes};
166
167 $self->cache->set( $id, to_json( $self->{data} ) ) if $self->{localStorage};
168
169 return $self->{data};
170 }
171
172 ## @method hashRef newSession()
173 # Build a new Apache::Session session.
174 # @return User data (just the session ID)
175 sub newSession {
176 my $self = shift;
177 $self->{data} = $self->_soapCall("newSession");
178
179 # Set cache
180 if ( $self->{localStorage} ) {
181 my $id = $self->{data}->{_session_id};
182 if ( $self->cache->get($id) ) {
183 $self->cache->remove($id);
184 }
185 $self->cache->set( $id, to_json( $self->{data} ) );
186 }
187
188 return $self->{data};
189 }
190
191 ## @method boolean save()
192 # Save user data if modified.
193 sub save {
194 my $self = shift;
195 return unless ( $self->{modified} );
196
197 # Update session in cache
198 if ( $self->{localStorage} ) {
199 my $id = $self->{data}->{_session_id};
200 if ( $self->cache->get($id) ) {
201 $self->cache->remove($id);
202 }
203 $self->cache->set( $id, to_json( $self->{data} ) );
204 }
205
206 # SOAP
207 return $self->_soapCall( "setAttributes", $self->{data}->{_session_id},
208 $self->{data} );
209 }
210
211 ## @method boolean delete()
212 # Deletes the current session.
213 sub delete {
214 my $self = shift;
215
216 # Remove session from cache
217 if ( $self->{localStorage} ) {
218 my $id = $self->{data}->{_session_id};
219 if ( $self->cache->get($id) ) {
220 $self->cache->remove($id);
221 }
222 }
223
224 # SOAP
225 return $self->_soapCall( "deleteSession", $self->{data}->{_session_id} );
226 }
227
228 ## @method get_key_from_all_sessions()
229 # Not documented.
230 sub get_key_from_all_sessions() {
231 my $class = shift;
232 my $args = shift;
233 my $data = shift;
234 my $self = bless {}, $class;
235 foreach (qw(proxy proxyOptions ns)) {
236 $self->{$_} = $args->{$_};
237 }
238 die('proxy is required') unless ( $self->{proxy} );
239 ( $user, $password ) = ( $args->{User}, $args->{Password} );
240
241 # Get token before query
242 my $token = Lemonldap::NG::Handler::Main->tsv->{cipher}
243 ->decrypt( $self->_soapCall('getCipheredToken') );
244 if ( ref($data) eq 'CODE' ) {
245 my $r = $self->_soapCall( "get_key_from_all_sessions", $token );
246 my $res;
247 if ($r) {
248 foreach my $k ( keys %$r ) {
249 my $tmp = &$data( $r->{$k}, $k );
250 $res->{$k} = $tmp if ( defined($tmp) );
251 }
252 }
253 }
254 else {
255 return $self->_soapCall( "get_key_from_all_sessions", $token, $data );
256 }
257 }
258
259 sub cache {
260 my $self = shift;
261
262 return $self->{cache} if $self->{cache};
263
264 my $module = $self->{localStorage};
265 eval "use $module;";
266 $self->{cache} = $module->new( $self->{localStorageOptions} );
267
268 return $self->{cache};
269 }
270
271 1;
272 __END__
273
274 =head1 NAME
275
276 =encoding utf8
277
278 Lemonldap::NG::Common::Apache::Session::SOAP - Perl extension written to
279 access to Lemonldap::NG Web-SSO sessions via SOAP.
280
281 =head1 SYNOPSIS
282
283 =over
284
285 =item * With Lemonldap::NG::Handler
286
287 package My::Package;
288 use Lemonldap::NG::Handler::SharedConf;
289
290 our @ISA = qw(Lemonldap::NG::Handler::Simple);
291
292 __PACKAGE__->init ({
293 globalStorage => 'Lemonldap::NG::Common::Apache::Session::SOAP',
294 globalStorageOptions => {
295 proxy => 'http://auth.example.com/sessions',
296 proxyOptions => {
297 timeout => 5,
298 },
299 # If soapserver is protected by HTTP Basic:
300 User => 'http-user',
301 Password => 'pass',
302 # To have a local session cache
303 localStorage => "Cache::FileCache",
304 localStorageOptions => {
305 'namespace' => 'lemonldap-ng',
306 'default_expires_in' => 600,
307 },
308 },
309 configStorage => {
310 ... # See Lemonldap::NG::Handler
311
312 =item * With Lemonldap::NG::Portal
313
314 use Lemonldap::NG::Portal::SharedConf;
315 my $portal = new Lemonldap::NG::Portal::SharedConf (
316 globalStorage => 'Lemonldap::NG::Common::Apache::Session::SOAP',
317 globalStorageOptions => {
318 proxy => 'http://auth.example.com/sessions',
319 proxyOptions => {
320 timeout => 5,
321 },
322 # If soapserver is protected by HTTP Basic:
323 User => 'http-user',
324 Password => 'pass',
325 # To have a local session cache
326 localStorage => "Cache::FileCache",
327 localStorageOptions => {
328 'namespace' => 'lemonldap-ng',
329 'default_expires_in' => 600,
330 },
331 },
332 configStorage => {
333 ... # See Lemonldap::NG::Portal
334
335 You can also set parameters corresponding to "Apache::Session module" in the
336 manager.
337
338 =back
339
340 =head1 DESCRIPTION
341
342 Lemonldap::NG::Common::Conf provides a simple interface to access to
343 Lemonldap::NG Web-SSO configuration. It is used by L<Lemonldap::NG::Handler>,
344 L<Lemonldap::NG::Portal> and L<Lemonldap::NG::Manager>.
345
346 Lemonldap::NG::Common::Apache::Session::SOAP used with
347 L<Lemonldap::NG::Portal> provides the ability to access to
348 Lemonldap::NG sessions via SOAP: the portal act as a proxy to access to the
349 real Apache::Session module (see HTML documentation for more)
350
351 =head2 SECURITY
352
353 As Lemonldap::NG::Common::Conf::SOAP use SOAP::Lite, you have to see
354 L<SOAP::Transport> to know arguments that can be passed to C<proxyOptions>.
355 Lemonldap::NG provides a system for HTTP basic authentication.
356
357 Examples :
358
359 =over
360
361 =item * HTTP Basic authentication
362
363 SOAP::transport can use basic authentication by rewriting
364 C<>SOAP::Transport::HTTP::Client::get_basic_credentials>:
365
366 package My::Package;
367
368 use base Lemonldap::NG::Handler::SharedConf;
369
370 __PACKAGE__->init ( {
371 globalStorage => 'Lemonldap::NG::Common::Apache::Session::SOAP',
372 globalStorageOptions => {
373 proxy => 'http://auth.example.com/sessions',
374 User => 'http-user',
375 Password => 'pass',
376 },
377 } );
378
379 =item * SSL Authentication
380
381 SOAP::transport provides a simple way to use SSL certificate: you've just to
382 set environment variables.
383
384 package My::Package;
385
386 use base Lemonldap::NG::Handler::SharedConf;
387
388 # AUTHENTICATION
389 $ENV{HTTPS_CERT_FILE} = 'client-cert.pem';
390 $ENV{HTTPS_KEY_FILE} = 'client-key.pem';
391
392 __PACKAGE__->init ( {
393 globalStorage => 'Lemonldap::NG::Common::Apache::Session::SOAP',
394 globalStorageOptions => {
395 proxy => 'https://auth.example.com/sessions',
396 },
397 } );
398
399 =back
400
401 =head1 SEE ALSO
402
403 L<Lemonldap::NG::Manager>, L<Lemonldap::NG::Common::Conf::SOAP>,
404 L<Lemonldap::NG::Handler>, L<Lemonldap::NG::Portal>,
405 L<http://lemonldap-ng.org/>
406
407 =head1 AUTHORS
408
409 =over
410
411 =item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
412
413 =back
414
415 =head1 BUG REPORT
416
417 Use OW2 system to report bug or ask for features:
418 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
419
420 =head1 DOWNLOAD
421
422 Lemonldap::NG is available at
423 L<https://lemonldap-ng.org/download>
424
425 =head1 COPYRIGHT AND LICENSE
426
427 See COPYING file for details.
428
429 This library is free software; you can redistribute it and/or modify
430 it under the terms of the GNU General Public License as published by
431 the Free Software Foundation; either version 2, or (at your option)
432 any later version.
433
434 This program is distributed in the hope that it will be useful,
435 but WITHOUT ANY WARRANTY; without even the implied warranty of
436 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
437 GNU General Public License for more details.
438
439 You should have received a copy of the GNU General Public License
440 along with this program. If not, see L<http://www.gnu.org/licenses/>.
441
442 =cut