blib/lib/Lemonldap/NG/Common/Apache/Session/SOAP.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 239 CC 25 SCAR 27.2
CC
25
Cov
0%
TIEHASH
30.0
get
30.0
get_key_from_all_sessions
30.0
CRAP 15.2
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__