blib/lib/Google/SAML/Request.pm

filter: Statementstmt 91.1% 103 / 113 Branchbran 67.8% 19 / 28 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 20 / 20 Podpod 100% 9 / 9 totaltotal 84.3% 156 / 185 CC 24 SCAR 12.9
CC
24
Cov
84%
get_get_param
6.5
new
6.0
_decode_saml_msg
5.1
CRAP 3.6
↑ Summary
1 # Copyright (c) 2008-2009 Manni Heumann. All rights reserved.
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the same terms as Perl itself.
5 #
6 # Date: 2009-11-06
7 # Revision: 43
8 #
9  
10 package Google::SAML::Request;
11  
12 =head1 NAME
13  
14 Google::SAML::Request - Create or parse Google's SAML requests
15  
16 =head1 VERSION
17  
18 You are currently reading the documentation for version 0.05
19  
20 =head1 DESCRIPTION
21  
22 Google::SAML::Request will parse (and, for the sake of completeness, create)
23 SAML requests as used by Google. B<Please note> that Google::SAML::Request is by
24 no means a full implementation of the SAML 2.0 standard. But if you want to
25 talk to Google to authenticate users, you should be fine.
26  
27 In fact, if you want to talk to Google about SSO, just use
28 L<Google::SAML::Response|Google::SAML::Response>
29 and you should be fine.
30  
31 =head1 SYNOPSIS
32  
33 Create a new request object by taking the request ouf of the CGI environment:
34  
35 use Google::SAML::Request;
36 my $req = Google::SAML::Request->new_from_cgi();
37 if ( $req->ProviderName() eq 'google.com'
38 && $req->AssertionConsumerServiceURL() eq 'https://www.google.com/hosted/psosamldemo.net/acs' ) {
39  
40 processRequest();
41 }
42 else {
43 print "go talk to someone else\n";
44 }
45  
46 Or use a request string that you get from somewhere else (but make sure that it is no longer
47 URI-escaped):
48  
49 use Google::SAML::Request;
50 my $req = Google::SAML::Request->new_from_string( $request_string );
51 if ( $req->ProviderName() eq 'google.com'
52 && $req->AssertionConsumerServiceURL() eq 'https://www.google.com/hosted/psosamldemo.net/acs' ) {
53  
54 processRequest();
55 }
56 else {
57 print "go talk to someone else\n";
58 }
59  
60 Or, finally, create a request from scratch and send that to somebody else:
61  
62 use Google::SAML::Request;
63 my $req = Google::SAML::Request->new(
64 {
65 ProviderName => 'me.but.invalid',
66 AssertionConsumerServiceURL => 'http://send.your.users.here.invalid/script',
67 }
68 );
69  
70  
71  
72  
73 =head1 PREREQUISITES
74  
75 You will need the following modules installed:
76  
77 =over
78  
79 =item * L<MIME::Base64|MIME::Base64>
80  
81 =item * L<Compress::Zlib|Compress::Zlib>
82  
83 =item * L<Date::Format|Date::Format>
84  
85 =item * L<XML::Simple|XML::Simple>
86  
87 =item * L<URI::Escape|URI::Escape>
88  
89 =item * L<CGI|CGI> (if you are going to use the 'new_from_cgi' constructor)
90  
91 =back
92  
93 =head1 METHODS
94  
95 =cut
96  
97 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 3 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 3 ▶use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 3 ▶use Date::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 3 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107  
108 3 our $VERSION = '0.05';
109  
110  
111 =head2 new
112  
113 Create a new Google::SAML::Request object from scratch.
114  
115 You have to provide the needed parameters here. Some parameters
116 are optional and defaults are used if they are not supplied.
117  
118 The parameters need to be passed in in a hash reference as
119 key value pairs.
120  
121 =head3 Required parameters
122  
123 =over
124  
125 =item * ProviderName
126  
127 Your name, e.g. 'google.com'
128  
129 =item * AssertionConsumerServiceURL
130  
131 The URL the user used to contact you. E.g. 'https://www.google.com/hosted/psosamldemo.net/acs'
132  
133 =back
134  
135 =head3 Optional parameters
136  
137 =over
138  
139 =item * IssueInstant
140  
141 The time stamp for the Request. Default is I<now>.
142  
143 =item * ID
144  
145 If you need to create the ID yourself, use this option. Otherwise the ID is
146 generated from the current time and a pseudo-random number.
147  
148 =back
149  
150 =cut
151  
152 7 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
153 7 my $class = shift;
154 7 my $args = shift;
155  
156 7 my $self = {
157 ProviderName
158 => '',
159 AssertionConsumerServiceURL
160 => '',
161 IssueInstant
162 => time2str( "%Y-%m-%dT%XZ", time, 'UTC' ),
163 ID
164 => undef,
165 };
166  
167 7 bless $self, $class;
168  
169 7 foreach my $required ( qw/ ProviderName AssertionConsumerServiceURL / ) {
170 confess "You need to provide the $required parameter to Googe::SAML::Request::new()"
171 14 ▶ unless exists $args->{ $required };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$required} 0 14 14
172 14 $self->{ $required } = $args->{ $required };
173 }
174  
175 7 foreach my $optional ( qw/ IssueInstant ID / ) {
176 $self->{ $optional } = $args->{ $optional }
177 14 ▶ if exists $args->{ $optional };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$optional} 6 8 14
178 }
179  
180 7 ▶ unless ( defined $self->{ ID } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ID"}) 4 3 7
181 4 $self->{ ID } = $self->_generate_id();
182 }
183  
184 $self->{request} = {
185 'ID' => $self->{ID},
186 'Version' => '2.0',
187 'xmlns:samlp' => 'urn:oasis:names:tc:SAML:2.0:protocol',
188 'IssueInstant' => $self->{IssueInstant},
189 'ProviderName' => $self->{ProviderName},
190 'ProtocolBinding' => 'urn:oasis:names.tc:SAML:2.0:bindings:HTTP-Redirect',
191 'AssertionConsumerServiceURL' => $self->{AssertionConsumerServiceURL},
192 7 };
193  
194 7 return $self;
195 }
196  
197  
198  
199  
200 =head2 new_from_cgi
201  
202 Create a new Google::SAML::Request object by fishing it out of the CGI
203 environment.
204  
205 If you provide a hash-ref with the key 'param_name' you can determine
206 which cgi parameter to use. The default is 'SAMLRequest'.
207  
208 =cut
209  
210 2 ▶sub new_from_cgi {
Subroutine new_from_cgi: called CC=4 SCAR=14.3
Pod documented
211 2 my $class = shift;
212 2 my $args = shift;
213  
214 2 my $self ={};
215 2 bless $self, $class;
216  
217 2 require CGI;
218 2 my $cgi = CGI->new();
219  
220 2 ▶ my $param = ( exists $args->{param_name} ) ? $args->{param_name} : 'SAMLRequest';
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'param_name'} ? : 1 1 2
221 2 my $request = $cgi->param( $param );
222  
223 2 ▶ if ( ! $request ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $request) { } 0 2 2
elsif ($self->_decode_saml_msg($request)) { } 2 0 2
224 0 warn "could not get request from cgi environment through parameter '$param'.";
225 }
226 elsif ( $self->_decode_saml_msg( $request ) ) {
227 2 return $self;
228 }
229  
230 0 return;
231 }
232  
233  
234  
235  
236  
237  
238 =head2 new_from_string
239  
240 Pass in a (uri_unescaped!) string that contains the request string. The string
241 will be base64-unencoded, inflated and parsed. You'll get back a fresh
242 Google::SAML::Response object if the string can be parsed.
243  
244 =cut
245  
246 1 ▶sub new_from_string {
Subroutine new_from_string: called CC=2 SCAR=7.1
Pod documented
247 1 my $class = shift;
248 1 my $string = shift;
249  
250 1 my $self = {};
251 1 bless $self, $class;
252  
253 1 ▶ if ( $self->_decode_saml_msg( $string ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_decode_saml_msg($string)) { } 1 0 1
254 1 return $self;
255 }
256 else {
257 0 return;
258 }
259 }
260  
261  
262  
263 =head2 get_xml
264  
265 Returns the XML representation of the request.
266  
267 =cut
268  
269 1 ▶sub get_xml {
Subroutine get_xml: called CC=2 SCAR=7.4
Pod documented
270 1 my $self = shift;
271  
272 1 ▶ if ( exists $self->{request} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'request'}) { } 1 0 1
273 return
274 XMLout( $self->{request},
275 1 KeyAttr => [ keys %{$self->{request}} ],
276 RootName => 'samlp:AuthnRequest',
277 XMLDecl => 1
278 );
279 }
280 else {
281 0 confess "The request object hasn't even been made yet";
282 }
283 }
284  
285  
286 =head2 get_get_param
287  
288 No, that's not a typo. This method will return the request in a form
289 suitable to be used as a GET parameter. In other words, this method
290 will take the XML representation, compress it, base64-encode the result
291 and, finally, URI-escape that.
292  
293 =cut
294  
295 1 ▶sub get_get_param {
Subroutine get_get_param: called CC=5 SCAR=18.7
Pod documented
296 1 my $self = shift;
297  
298 1 my $xml = $self->get_xml();
299  
300 1 my ( $d, $status ) = deflateInit( -WindowBits => -&MAX_WBITS() );
301  
302 1 ▶ if ( $status == Z_OK && $d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == Z_OK() and $d) { } 1 0 1
Condition33%1/3
$status == Z_OK() and $d
!ll&&!rl&&r
001
MC/DC0%0/2
$status == Z_OK() and $d
$status == Z_OK() $d
Truth table33%1/3
A and B
A= $status == Z_OK()B= $d
ABresult
0X0
100
111
303 1 my ( $compressed, $status ) = $d->deflate( $xml );
304 1 $compressed .= $d->flush();
305  
306 1 ▶ if ( $status == Z_OK && length( $compressed ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == Z_OK() and length $compressed) { } 1 0 1
Condition33%1/3
$status == Z_OK() and length $compressed
!ll&&!rl&&r
001
MC/DC0%0/2
$status == Z_OK() and length $compressed
$status == Z_OK() length $compressed
Truth table33%1/3
A and B
A= $status == Z_OK()B= length $compressed
ABresult
0X0
100
111
307 1 my $encoded = encode_base64( $compressed, '' );
308 1 my $escaped = uri_escape( $encoded );
309  
310 1 return $escaped;
311 }
312 else {
313 0 warn "Could not compress xml";
314 }
315 }
316 else {
317 0 warn "Could not initialise deflation stream.";
318 }
319  
320 0 return;
321 }
322  
323  
324  
325  
326  
327 4 ▶sub _generate_id {
Subroutine _generate_id: called CC=3 SCAR=11.0
328 4 my $self = shift;
329  
330 4 my $id = '';
331  
332 4 my $time = time;
333 4 foreach ( split //, $time ) {
334 40 $id .= chr( $_ + 97 );
335 }
336  
337 4 foreach ( 1 .. 30 ) {
338 120 $id .= chr( int(rand( 26 )) + 97 );
339 }
340  
341 4 return $id;
342 }
343  
344  
345  
346  
347 3 ▶sub _decode_saml_msg {
Subroutine _decode_saml_msg: called CC=5 SCAR=16.3
348 3 my $self = shift;
349 3 my $msg = shift;
350  
351 3 my $decoded = decode_base64( $msg );
352 3 my $inflated = undef;
353  
354 3 foreach my $wbits ( -&MAX_WBITS(), &MAX_WBITS() ) {
355 5 $inflated = $self->_inflate( $decoded, $wbits );
356 5 ▶ last if defined $inflated;
Branch: 1 branch
BranchTrueFalseTotal
if defined $inflated 3 2 5
357 }
358  
359 3 ▶ if ( defined $inflated ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inflated) { } 3 0 3
360 3 $self->{request} = XMLin( $inflated, ForceArray => 0 );
361 3 foreach ( qw/ ProviderName AssertionConsumerServiceURL ID IssueInstant / ) {
362 12 $self->{ $_ } = $self->{request}->{ $_ };
363 }
364  
365 3 return 1;
366 }
367 else {
368 0 warn "Could not inflate base64-decoded string.";
369 }
370  
371 0 return;
372 }
373  
374  
375  
376  
377 5 ▶sub _inflate {
Subroutine _inflate: called CC=4 SCAR=14.0
378 5 my $self = shift;
379 5 my $string = shift;
380 5 my $windowBits = shift;
381  
382 5 my ( $i, $status ) = inflateInit( -WindowBits => $windowBits );
383  
384 5 ▶ if ( $status == Z_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == Z_OK()) { } 5 0 5
385 5 my $inflated;
386 5 ($inflated, $status) = $i->inflate( $string );
387  
388 5 ▶ if ( $status == Z_OK || $status == Z_STREAM_END ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == Z_OK() or $status == Z_STREAM_END()) 3 2 5
Condition66%2/3
$status == Z_OK() or $status == Z_STREAM_END()
l!l&&r!l&&!r
032
MC/DC50%1/2
$status == Z_OK() or $status == Z_STREAM_END()
$status == Z_OK() $status == Z_STREAM_END()
Truth table66%2/3
A or B
A= $status == Z_OK()B= $status == Z_STREAM_END()
ABresult
1X1
011
000
389 3 return $inflated;
390 }
391 }
392 else {
393 0 warn "No inflater!";
394 }
395  
396 2 return;
397 }
398  
399 =head3 Accessor methods (read-only)
400  
401 All of the following accessor methods return the value of the
402 attribute with the same name
403  
404 =head2 AssertionConsumerServiceURL
405  
406 =head2 ID
407  
408 =head2 IssueInstant
409  
410 =head2 ProtocolBinding
411  
412 =head2 ProviderName
413  
414 =head2 Version
415  
416 =cut
417  
418 6 ▶sub AssertionConsumerServiceURL { return shift->{AssertionConsumerServiceURL}; }
Subroutine AssertionConsumerServiceURL: called CC=1 SCAR=0.0
Pod documented
419 6 ▶sub ID { return shift->{ID}; }
Subroutine ID: called CC=1 SCAR=0.0
Pod documented
420 6 ▶sub IssueInstant { return shift->{IssueInstant}; }
Subroutine IssueInstant: called CC=1 SCAR=0.0
Pod documented
421 6 ▶sub ProviderName { return shift->{ProviderName}; }
Subroutine ProviderName: called CC=1 SCAR=0.0
Pod documented
422  
423 =head1 SOURCE CODE
424  
425 This module has a repository on github. Pull requests are welcome.
426  
427 https://github.com/mannih/Google-SAML-Request/
428  
429 =head1 AUTHOR
430  
431 Manni Heumann (saml at lxxi dot org)
432  
433  
434 =head1 LICENSE
435  
436 Copyright (c) 2008 Manni Heumann. All rights reserved.
437  
438 This program is free software; you can redistribute it and/or
439 modify it under the same terms as Perl itself.
440  
441 =cut
442  
443  
444 3 1;
↑ Summary