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

filter: Statementstmt 0.0% 0 / 177 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 313 CC 38 SCAR 32.1
CC
38
Cov
0%
save
56.0
TIEHASH
42.0
_getAll
42.0
CRAP 24.7
1 package Lemonldap::NG::Common::Apache::Session::REST;
2  
3 use strict;
4 use Lemonldap::NG::Common::UserAgent;
5 use Lemonldap::NG::Common::Apache::Session::Generate::SHA256;
6 use JSON qw(from_json to_json);
7  
8 our $VERSION = '2.21.0';
9  
10 our @ISA = qw(Lemonldap::NG::Common::Apache::Session::Generate::SHA256);
11  
12 # PUBLIC INTERFACE
13  
14 # Constructor for Perl TIE mechanism. See perltie(3) for more.
15 sub TIEHASH {
16 my ( $class, $session_id, $args ) = @_;
17 die "baseUrl argument is required"
18 unless ( $args and $args->{baseUrl} );
19 my $self = {
20 data => { _session_id => $session_id },
21 modified => 0,
22 };
23 foreach (
24 qw(baseUrl user password realm localStorage localStorageOptions lwpOpts lwpSslOpts kind)
25 )
26 {
27 $self->{$_} = $args->{$_};
28 }
29 bless $self, $class;
30  
31 if ( defined $session_id && $session_id ) {
32 die "unexistant session $session_id"
33 unless ( $self->get($session_id) );
34 }
35 elsif ( $args->{setId} ) {
36 $self->{data}->{_session_id} = $args->{setId};
37 $self->newSession;
38 }
39 else {
40 die "unable to create session"
41 unless ( $self->newSession() );
42 }
43 return $self;
44 }
45  
46 sub FETCH {
47 my $self = shift;
48 my $key = shift;
49 return $self->{data}->{$key};
50 }
51  
52 sub STORE {
53 my $self = shift;
54 my $key = shift;
55 my $value = shift;
56  
57 $self->{data}->{$key} = $value;
58 $self->{modified} = 1 unless $key eq '_session_id';
59 return $value;
60 }
61  
62 sub DELETE {
63 my $self = shift;
64 my $key = shift;
65  
66 $self->{modified} = 1;
67  
68 delete $self->{data}->{$key};
69 }
70  
71 sub CLEAR {
72 my $self = shift;
73  
74 $self->{modified} = 1;
75  
76 $self->{data} = {};
77 }
78  
79 sub EXISTS {
80 my $self = shift;
81 my $key = shift;
82 return exists $self->{data}->{$key};
83 }
84  
85 sub FIRSTKEY {
86 my $self = shift;
87 my $reset = keys %{ $self->{data} };
88 return each %{ $self->{data} };
89 }
90  
91 sub NEXTKEY {
92 my $self = shift;
93 return each %{ $self->{data} };
94 }
95  
96 sub DESTROY {
97 my $self = shift;
98 $self->save;
99 }
100  
101 sub ua {
102 my ($self) = @_;
103 return $self->{ua} if ( $self->{ua} );
104 my $ua = Lemonldap::NG::Common::UserAgent->new(
105 { lwpOpts => $self->{lwpOpts}, lwpSslOpts => $self->{lwpSslOpts} } );
106 if ( $self->{user} ) {
107 my $url = $self->{baseUrl};
108 my $port = ( $url =~ /^https/ ? 443 : 80 );
109 $url =~ s#https?://([^/]*).*$#$1#;
110 if ( $url =~ s/:(\d+)$// ) {
111 $port = $1;
112 }
113 $ua->credentials( "$url:$port", $self->{realm},
114 $self->{user}, $self->{password} );
115 }
116 return $self->{ua} = $ua;
117 }
118  
119 sub getJson {
120 my $self = shift;
121 my $id = shift;
122 my $resp = $self->ua->get(
123 $self->base
124 . $id
125 . ( $self->{kind} ne 'SSO' ? "?kind=$self->{kind}" : '' ),
126 @_
127 );
128 if ( $resp->is_success ) {
129 my $res;
130 eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
131 if ($@) {
132 print STDERR "Unable to decode session: $@\n";
133 return 0;
134 }
135 return $res;
136 }
137 elsif ( $resp->status_line =~ /400/ ) {
138 return 0;
139 }
140 else {
141 print STDERR 'REST server returns: ' . $resp->status_line . "\n";
142 return 0;
143 }
144 }
145  
146 sub base {
147 my ($self) = @_;
148 $self->{baseUrl} =~ s#/*$#/#;
149 return $self->{baseUrl};
150 }
151  
152 ## @method hashRef get(string id)
153 # @param $id Apache::Session session ID.
154 # @return User data
155 sub get {
156 my $self = shift;
157 my $id = shift;
158  
159 # Check cache
160 if ( $self->{localStorage} and my $res = $self->cache->get($id) ) {
161 $res = eval { from_json($res) };
162 return $self->{data} = $res if $res;
163 }
164  
165 # No cache, use REST and set cache
166 my $res = $self->getJson($id) or return 0;
167 $self->{data} = $res;
168  
169 $self->cache->set( $id, to_json( $self->{data} ) ) if $self->{localStorage};
170  
171 return $self->{data};
172 }
173  
174 ## @method hashRef newSession()
175 # Build a new Apache::Session session.
176 # @return User data (just the session ID)
177 sub newSession {
178 my $self = shift;
179 $self->generate unless ( $self->{data}->{_session_id} );
180 $self->{data}->{_utime} = time;
181  
182 #my $req = HTTP::Request->new( POST => $self->base );
183 #$req->content( to_json( { _utime => time } ) );
184 #$req->header( 'Content-Type' => 'application/json' );
185 #my $resp = $self->ua->request($req);
186 #if ( $resp->is_success ) {
187 # my $res;
188 # eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
189 # if ( $@ or !$res->{result} ) {
190 # die "Unable to create session: bad REST response $@";
191 # }
192 # $self->{data} = $res->{session};
193 #}
194 #else {
195 # die "REST server returns " . $resp->status_line;
196 #}
197  
198 ## Set cache
199 #if ( $self->{localStorage} ) {
200 # my $id = $self->{data}->{_session_id};
201 # if ( $self->cache->get($id) ) {
202 # $self->cache->remove($id);
203 # }
204 # $self->cache->set( $id, to_json( $self->{data} ) );
205 #}
206  
207 return $self->{data};
208 }
209  
210 ## @method boolean save()
211 # Save user data if modified.
212 sub save {
213 my $self = shift;
214 return unless ( $self->{modified} );
215  
216 # Update session in 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 $self->cache->set( $id, to_json( $self->{data} ) );
223 }
224  
225 # REST
226 my $req =
227 HTTP::Request->new( PUT => $self->base . $self->{data}->{_session_id} );
228 eval {
229 $self->{data}->{__secret} =
230 Lemonldap::NG::Handler::Main->tsv->{cipher}->encrypt(time);
231 };
232 print STDERR "$@\n" if ($@);
233 my $content = to_json( $self->{data} );
234 $req->content($content);
235 delete $self->{data}->{__secret};
236 $req->header( 'Content-Type' => 'application/json' );
237 $req->header( 'Content-Length' => length($content) );
238 my $resp = $self->ua->request($req);
239  
240 if ( $resp->is_success ) {
241 my $res;
242 eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
243 if ($@) {
244 die "Bad REST response: $@";
245 }
246 return $res;
247 }
248 else {
249 print STDERR "REST server returns " . $resp->status_line;
250 return;
251 }
252 }
253  
254 ## @method boolean delete()
255 # Deletes the current session.
256 sub delete {
257 my $self = shift;
258  
259 # Remove session from cache
260 if ( $self->{localStorage} ) {
261 my $id = $self->{data}->{_session_id};
262 if ( $self->cache->get($id) ) {
263 $self->cache->remove($id);
264 }
265 }
266  
267 # REST
268 my $req = HTTP::Request->new(
269 DELETE => $self->base . $self->{data}->{_session_id} );
270 $req->header( 'Content-Type' => 'application/json' );
271 my $resp = $self->ua->request($req);
272 return ( $resp->is_success ? 1 : 0 );
273 }
274  
275 sub searchOn {
276 my ( $class, $args, $selectField, $value, @fields ) = @_;
277 return $class->_getAll( "all=1&search=$selectField,$value",
278 $args, ( @fields ? \@fields : () ) );
279 }
280  
281 ## @method get_key_from_all_sessions()
282 # Not documented.
283 sub get_key_from_all_sessions() {
284 my ( $class, $args, $data ) = @_;
285 my $res = $class->_getAll( 'all=1', $args, $data );
286 return unless $res;
287  
288 if ( ref($data) eq 'CODE' ) {
289 my $r;
290 foreach my $k ( keys %$res ) {
291 my $tmp = &$data( $res->{$k}, $k );
292 $r->{$k} = $tmp if ( defined($tmp) );
293 }
294 $res = $r;
295 }
296 return $res;
297 }
298  
299 sub _getAll {
300 my ( $class, $query, $args, $data ) = @_;
301 my $self = bless {}, $class;
302 foreach (qw(baseUrl user password realm lwpOpts lwpSslOpts kind)) {
303 $self->{$_} = $args->{$_};
304 }
305 $self->{data} = { data => ( ref($data) eq 'CODE' ? undef : $data ) };
306 die('baseUrl is required') unless ( $self->{baseUrl} );
307 my $req = HTTP::Request->new( POST => $self->base . "?$query" );
308 eval {
309 $self->{data}->{__secret} =
310 Lemonldap::NG::Handler::Main->tsv->{cipher}->encrypt(time);
311 };
312 print STDERR "$@\n" if ($@);
313 my $content = to_json( $self->{data} );
314 $req->content($content);
315 $req->header( 'Content-Length' => length($content) );
316 delete $self->{data}->{__secret};
317 $req->header( 'Content-Type' => 'application/json' );
318 my $resp = $self->ua->request($req);
319  
320 if ( $resp->is_success ) {
321 my $res;
322 eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
323 if ($@) {
324 die "Bad REST response: $@";
325 }
326 return $res;
327 }
328 else {
329 print STDERR "REST server returns " . $resp->status_line . "\n";
330 return;
331 }
332 }
333  
334 sub cache {
335 my $self = shift;
336  
337 return $self->{cache} if $self->{cache};
338  
339 my $module = $self->{localStorage};
340 eval "use $module;";
341 $self->{cache} = $module->new( $self->{localStorageOptions} );
342  
343 return $self->{cache};
344 }
345  
346 1;
347 __END__