File Coverage

blib/lib/WebService/AngelXML/Auth.pm
Criterion Covered Total %
statement 79 99 79.8
branch 51 52 98.0
condition 1 3 33.3
subroutine 18 20 90.0
pod 14 15 93.3
total 163 189 86.2


line stmt bran cond sub pod time code
1             package WebService::AngelXML::Auth;
2 3     3   78197 use strict;
  3         10  
  3         123  
3 3     3   3040 use XML::Writer qw{};
  3         55834  
  3         74  
4 3     3   217380 use CGI qw{};
  3         50546  
  3         111  
5              
6             BEGIN {
7 3     3   32 use vars qw($VERSION);
  3         4  
  3         154  
8 3     3   3146 $VERSION = '0.14';
9             }
10              
11             =head1 NAME
12              
13             WebService::AngelXML::Auth - Generates XML Authentication Document for Angel Web service
14              
15             =head1 SYNOPSIS
16              
17             use WebService::AngelXML::Auth;
18             my $ws = WebService::AngelXML::Auth->new();
19             $ws->allow(1) if "test";
20             print $ws->header, $ws->response;
21              
22             =head1 DESCRIPTION
23              
24             WebService::AngelXML::Auth is a Perl object oriented interface that allows for the creation of the XML response required for AngleXML Authentication.
25              
26             =head1 USAGE
27              
28             use WebService::AngelXML::Auth;
29             my $ws=WebService::AngelXML::Auth->new();
30             if ("Some test here") {
31             $ws->allow(1);
32             } else {
33             $ws->deny(1); #default
34             }
35             print $ws->header, $ws->response;
36              
37             =head1 CONSTRUCTOR
38              
39             =head2 new
40              
41             my $ws=WebService::AngelXML::Auth->new(
42             cgi => $query, #pass this if already constructed else will construct
43             allow => 0, #allow and deny are both stored in $ws->{'deny'};
44             deny => 1, #default is deny=1 only set deny=0 if you are permissive
45             mimetype => "text/xml", #default is application/vnd.angle-xml.xml+xml
46             page => "/1000", #default next page is "/1000"
47             );
48              
49             =cut
50              
51             sub new {
52 14     14 1 11069 my $this = shift();
53 14   33     73 my $class = ref($this) || $this;
54 14         22 my $self = {};
55 14         35 bless $self, $class;
56 14         47 $self->initialize(@_);
57 14         812 return $self;
58             }
59              
60             =head1 METHODS
61              
62             =cut
63              
64             sub initialize {
65 14     14 0 17 my $self=shift;
66 14         54 %$self=@_;
67 14 100       41 $self->allow(delete $self->{'allow'}) if defined $self->{'allow'};
68 14 100       56 $self->deny(1) unless defined $self->{'deny'};
69 14 100       36 $self->cgi(CGI->new) unless ref($self->cgi) eq 'CGI';
70 14 100       34 $self->mimetype("application/vnd.angle-xml.xml+xml")
71             unless defined $self->mimetype;
72 14 100       33 $self->param_id("id") unless defined $self->param_id;
73 14 100       31 $self->param_pin("pin") unless defined $self->param_pin;
74 14 100       26 $self->param_page("page") unless defined $self->param_page;
75 14 100       29 $self->prompt(".") unless defined $self->prompt;
76 14 100       31 $self->page(delete $self->{'page'}) if defined $self->{'page'};
77 14 100       92 $self->page("/1000") unless defined $self->page;
78             }
79              
80             =head2 allow
81              
82             Set or returns the current allow state. Allow and deny methods are inversly related.
83              
84             You may set the allow and deny methods with any value that Perl evaluates to true or false. However, they will always return "-1" for true and "0" for false.
85              
86             if ($ws->allow) { "Do something!" }
87             print $ws->allow; #will always return "-1" for true and "0" for false
88             $ws->allow(0); #will set the allow to "0" and the deny to "-1"
89             $ws->allow(1); #will set the allow to "-1" and the deny to "0"
90              
91             =cut
92              
93             sub allow {
94 14     14 1 3047 my $self=shift;
95 14 100       35 if (@_) {
96 4         6 my $value=shift;
97 4 100       15 $self->{'deny'} = $value ? "0" : "-1";
98             }
99 14 100       56 return $self->{'deny'} ? "0" : "-1";
100             }
101              
102             =head2 deny
103              
104             Set or returns the current deny state. Allow and deny methods are inversly related.
105              
106             You may set the allow and deny methods with any value that Perl evaluates to true or false. However, they will always return -1 for true and 0 for false.
107              
108             if ($ws->deny) { "Do something!" }
109             print $ws->deny; #will always return -1 for true and 0 for false
110             $ws->deny(0); #will set the deny to "0" and the allow to "-1"
111             $ws->deny(1); #will set the deny to "-1" and the allow to "0"
112              
113             =cut
114              
115             sub deny {
116 22     22 1 36 my $self=shift;
117 22 100       51 if (@_) {
118 12         15 my $value=shift;
119 12 100       38 $self->{'deny'} = $value ? "-1" : "0";
120             }
121 22 100       75 return $self->{'deny'} ? "-1" : "0";
122             }
123              
124             =head2 response
125              
126             Returns an XML document with an XML declaration and a root name of "ANGELXML"
127              
128             print $ws->response;
129              
130             Example (Deny):
131              
132            
133            
134            
135             .
136            
137            
138            
139            
140            
141            
142            
143              
144             =cut
145              
146             sub response {
147 0     0 1 0 my $self=shift;
148 0         0 my $document='';
149 0         0 my $writer=XML::Writer->new(OUTPUT=>\$document, DATA_MODE=>1, DATA_INDENT=>2);
150            
151 0         0 $writer->startTag("ANGELXML");
152 0         0 $writer->startTag("MESSAGE");
153 0         0 $writer->startTag("PLAY");
154 0         0 $writer->startTag("PROMPT", type=>"text");
155 0         0 $writer->characters($self->prompt);
156 0         0 $writer->endTag("PROMPT");
157 0         0 $writer->endTag("PLAY");
158 0         0 $writer->emptyTag("GOTO", destination=>$self->page);
159 0         0 $writer->endTag("MESSAGE");
160 0         0 $writer->startTag("VARIABLES");
161 0         0 $writer->emptyTag("VAR", name=>"status_code", value=>$self->deny);
162 0         0 $writer->endTag("VARIABLES");
163 0         0 $writer->endTag("ANGELXML");
164 0         0 $writer->end();
165 0         0 return $document;
166             }
167              
168             =head2 header
169              
170             print $document->header;
171              
172             Example:
173              
174             Content-Type: application/vnd.angle-xml.xml+xml
175              
176             =cut
177              
178             sub header {
179 0     0 1 0 my $self=shift;
180 0         0 return sprintf("Content-Type: %s\n\n", $self->mimetype);
181             }
182              
183             =head2 mimetype
184              
185             Sets or returns mime type the default is application/vnd.angle-xml.xml+xml
186              
187             $ws->mimetype('text/xml'); #This works better when testing with MSIE
188             my $mt=$ws->mimetype;
189              
190             =cut
191              
192             sub mimetype {
193 29     29 1 324 my $self=shift;
194 29 100       56 if (@_) {
195 13         27 $self->{'mimetype'}=shift;
196             }
197 29         80 return $self->{'mimetype'};
198             }
199              
200             =head2 cgi
201              
202             Sets or returns the cgi object which must be CGI from cpan. Default is to construct a new CGI object. If you already have a CGI object, you MUST pass it on construction.
203              
204             $cgi=CGI->new("id=9999;pin=0000;page=/1000");
205             $ws=WebService::AngelXML::Auth->new(cgi=>$cgi);
206              
207             DO NOT do this as we would have already created two CGI objects.
208              
209             $cgi=CGI->new("id=9999;pin=0000;page=/1000"); #a new CGI object is created
210             $ws=WebService::AngelXML::Auth->new(); #a new CGI object is created on initialization
211             $ws->cgi($cgi); #this CGI object may not be iniatialized correctly
212              
213             CGI object is fully functional
214              
215             print $ws->cgi->p("Hello World!"); #All CGI methods are available
216              
217             =cut
218              
219             sub cgi {
220 91     91 1 13811 my $self=shift;
221 91 100       174 if (@_) {
222 11         18 my $obj=shift;
223 11 50       36 $self->{'cgi'}=$obj if ref($obj) eq 'CGI';
224             }
225 91         296 return $self->{'cgi'};
226             }
227              
228             =head2 id
229              
230             Returns the user id which is passed from the CGI parameter. The default CGI parameter is "id" but can be overriden by the param_id method.
231              
232             print $ws->id;
233             $ws->id("0000"); #if you want to set it for testing.
234              
235             =cut
236              
237             sub id {
238 4     4 1 1024 my $self=shift;
239 4 100       17 if (@_) {
240 1         3 $self->cgi->param(-name=>$self->param_id, -value=>shift);
241             }
242 4         97 my $return=$self->cgi->param(-name=>$self->param_id);
243 4         308 return $return;
244             }
245              
246             =head2 param_id
247              
248             The value of the CGI parameter holding the value of the user id.
249              
250             $ws->param_id("id"); #default
251              
252             =cut
253              
254             sub param_id {
255 33     33 1 41 my $self=shift();
256 33 100       73 $self->{'param_id'} = shift if @_;
257 33         82 return $self->{'param_id'};
258             }
259              
260             =head2 pin
261              
262             Returns the user pin which is passed from the CGI parameter. The default CGI parameter is "pin" but can be overriden by the param_pin method.
263              
264             print $ws->pin;
265             $ws->pin("0000"); #if you want to set it for testing.
266              
267             =cut
268              
269             sub pin {
270 4     4 1 15 my $self=shift;
271 4 100       14 if (@_) {
272 1         4 $self->cgi->param(-name=>$self->param_pin, -value=>shift);
273             }
274 4         96 my $return=$self->cgi->param(-name=>$self->param_pin);
275 4         409 return $return;
276             }
277              
278             =head2 param_pin
279              
280             The value of the CGI parameter holding the value of the user pin.
281              
282             $ws->param_pin("pin"); #default
283              
284             =cut
285              
286             sub param_pin {
287 33     33 1 43 my $self=shift;
288 33 100       66 $self->{'param_pin'}=shift if @_;
289 33         85 return $self->{'param_pin'};
290             }
291              
292             =head2 page
293              
294             Returns the authentication next page which can be passed from the POST parameter. See param_page method.
295              
296             Three ways to set next page.
297              
298             $ws=WebService::AngelXML::Auth->new(page=>"/1000"); #during construction
299             $ws->page("/1000"); #after constructing
300             script.cgi?page=/1000 #as cgi parameter
301              
302             =cut
303              
304             sub page {
305 33     33 1 982 my $self=shift;
306 33 100       84 if (@_) {
307 14         18 my $value=shift;
308 14         26 $self->cgi->param(-name=>$self->param_page, -value=>$value);
309             }
310 33         1020 return $self->cgi->param(-name=>$self->param_page);
311             }
312              
313             =head2 param_page
314              
315             The value of the CGI parameter holding the value of the next page.
316              
317             $ws->param_page("page"); #default
318              
319             =cut
320              
321             sub param_page {
322 75     75 1 80 my $self=shift;
323 75 100       151 if (@_) {
324 14         20 $self->{'param_page'}=shift;
325             }
326 75         227 return $self->{'param_page'};
327             }
328              
329             =head2 prompt
330              
331             Sets or returns the prompt text.
332              
333             print $ws->prompt;
334             $ws->prompt("."); #default
335              
336             =cut
337              
338             sub prompt {
339 31     31 1 2164 my $self=shift;
340 31 100       59 if (@_) {
341 14         30 $self->{'prompt'}=shift;
342             }
343 31         74 return $self->{'prompt'};
344              
345             }
346              
347             =head1 BUGS
348              
349             =head1 SUPPORT
350              
351             Try Angel first then the author of this package who is not an Angel employee
352              
353             =head1 AUTHOR
354              
355             Michael R. Davis (mrdvt92)
356             CPAN ID: MRDVT
357              
358             =head1 COPYRIGHT
359              
360             Copyright 2008 - STOP, LLC
361             Copyright 2008 - Michael R. Davis (mrdvt92)
362              
363             This program is free software licensed under the...
364              
365             The BSD License
366              
367             The full text of the license can be found in the
368             LICENSE file included with this module.
369              
370             =head1 SEE ALSO
371              
372             L is used by this package to generate XML.
373              
374             L is used by this package to handle HTTP POST/GET parameters.
375              
376             =cut
377              
378             1;