Standalone.pm untested

filter: Statementstmt 0.0% 0 / 195 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 57 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 380 CC 24 SCAR 28.4
CC
24
Cov
0%
id
42.0
isTrusted
42.0
setup
30.0
CRAP 17.1
1 package Net::OpenID::Server::Standalone;
2  
3 use vars qw($VERSION);
4 BEGIN {
5 $VERSION = '0.1.1';
6 $Net::OpenID::Server::Standalone::Default = 'Net::OpenID::Server::Standalone';
7 }
8  
9 =pod
10  
11 =head1 NAME
12  
13 Net::OpenID::Server::Standalone - personal standalone OpenID server ready-to-use out-of-the-box
14  
15 =head1 SYNOPSIS
16  
17 id script use this:
18  
19 Net::OpenID::Server::Standalone::id;
20  
21 setup script use this:
22  
23 Net::OpenID::Server::Standalone::setup;
24  
25 Some kind of L<Net::OpenID::Server::Standalone::Config> is a must. For more sophisticated use see L</USAGE> below.
26
27 =head1 DESCRIPTION
28  
29 Nossa is dedicated for fast installation of your own OpenID 'Server' on a CGI/L<FCGI::Spawn> - enabled hosting. There is a lot of tweaks for common needs known as: your own identity source to be pluggable with Config.pm, your own design for user setup pages, location of your CGI::Session storage, your SRE information, redirect to your HTTPS server for setup, etc.
30  
31 Typical layout follows:
32  
33 ./ --- application root, e. g. $HOME on your hosting.
34 lib/Net/OpenID/Server/Standalone/
35 Config.pm --- configuration of your OpenID server,
36 created from sample Config.pm
37 www/ or public_html/
38 index.html or whatever to be your XRD document like it is at
39 http://peter.vereshagin.org.
40 cgi/ or perl/ or cgi-bin/ or www/
41 id.cgi or id.pl or id --- id script
42 setup.cgi or setup.pl or setup --- setup script
43  
44 Of course those mentioned last three can be on the different servers as different URLs. The workflow is as follows: id script checks user identity and setup shows forms. Target of those forms is an id script, too.
45  
46 You may use your own MyApp.pm and MyApp/Config.pm ( see below ).
47  
48 =head1 PREREQUISITES
49  
50 Net::OpenID::Server, Data::UUID, MIME::Base64, HTML::Entities, Digest::MD5, CGI, CGI::Session.
51  
52 =cut
53  
54 use strict;
55 use warnings;
56  
57 use Net::OpenID::Server;
58 use Data::UUID;
59 use MIME::Base64 qw/encode_base64/;
60 use HTML::Entities qw/encode_entities/;
61 use Digest::MD5 qw/md5_base64/;
62 use CGI;
63 use CGI::Session;
64  
65 my $configPackage;
66  
67 my( $cgi, $session, );
68  
69  
70 =pod
71  
72 =head1 Variables and Subs
73  
74  
75 =over
76  
77 =item * C<$htmlStyle>
78  
79 hash reference for HTML code for your setup pages: the 'start' key holds a value for start of the page, and the 'end' key holds a value for trhe end.
80  
81 =cut
82 our $htmlStyle = { start => '<html>', end => '</html>', };
83  
84 *_push_url_arg = \&Net::OpenID::Server::_push_url_arg;
85 *_eurl = \&Net::OpenID::Server::_eurl;
86  
87 =pod
88  
89 =item * hashFunction()
90  
91 is a function that provides hash for your password storage. MD5 is the default.
92  
93 =cut
94  
95 *hashFunction =\&md5_base64;
96  
97 =pod
98  
99 =item * new()
100  
101 Object constructor, starts session and so on.
102  
103 =cut
104  
105 sub new {
106 my $pkg = shift;
107 no strict 'refs';
108 $htmlStyle = ${ *{ $pkg.'::' }->{ htmlStyle } };
109 use strict 'refs';
110 $configPackage = $pkg."::Config";
111 eval( "use $configPackage;" );
112 length( $@ ) and Carp::croak "No $configPackage! (please create it from Config.pm.sample): $@";
113 $cgi = new CGI; $cgi->charset( 'utf-8' );
114 my $rnd = encode_base64( Data::UUID->new->create() ); chomp $rnd;
115 my $setupUrl = $configPackage->get( 'setupUrl' );
116 _push_url_arg( \$setupUrl , 'rnd' => $rnd, );
117 my $session_href = $configPackage->get( 'session' );
118 my( $session_name, $session_dsn, $session_expire ) = map{ $session_href->{ $_ } } qw/name dsn expire/;
119 CGI::Session->name( $session_name );
120 $session = new CGI::Session( $session_dsn, undef ) or die CGI::Session->errstr;
121 $session->expire( $session_expire );
122 bless {
123 rnd => $rnd,
124 setupUrl => $setupUrl,
125 }, $pkg ;
126 }
127  
128 =pod
129  
130 =item * id()
131  
132 performs identification, should be pointed as an 'openid.delegate' URL in your XRD document ( located on your OpenID URL ).
133  
134 =cut
135  
136 sub id {
137 my $self = ( @_ ? shift : __PACKAGE__ )->new ;
138 my $requireSsl = $configPackage->get( 'requireSsl' );
139 unless( $requireSsl and $self->isRedirectedToSsl() ){
140 my $setupUrl = $self->{ setupUrl };
141 my $nos = Net::OpenID::Server->new(
142 get_args => $cgi,
143 post_args => $cgi,
144 get_user => sub{ $self->getUser( @_ ) },
145 is_identity => \&isIdentity,
146 is_trusted => sub{ $self->isTrusted( @_ ) },
147 server_secret => $configPackage->get( 'serverSecret' ),
148 setup_url => $setupUrl ,
149 compat => 1,
150 );
151 my ($type, $data) = $nos->handle_page();
152 my $redirect = [ '200', $data, -type => $type, ];
153 if( $type eq 'redirect' ){
154 my $user = $nos->get_user()->();
155 #my $url = $data;
156 my $url = {
157 identity => $nos->args('openid.identity'),
158 return_to => $nos->args('openid.return_to'),
159 assoc_handle => $nos->args('openid.assoc_handle'),
160 trust_root => $nos->args('openid.trust_root'),
161 };
162 my $sre = $configPackage->get( 'users', $user, 'sre', );
163 if( defined( $sre ) and 'HASH' eq ref $sre ){
164 $url->{ additional_fields } = $sre;
165 }
166 $url = $nos->signed_return_url( %$url );
167 $redirect = [ '301 Identity Provided', $url, ];
168 } elsif( $type eq 'setup' ){
169 my $url = $setupUrl;
170 _push_url_arg( \$url, %$data, );
171 $redirect = [ '301 Setup Required', $url, ];
172 }
173 $self->redirect( @$redirect );
174 }
175 }
176  
177 =pod
178  
179 =item * setup()
180  
181 shows forms for login, logout and for trust the requesting URL. For use in the 'setup' script.
182  
183 =cut
184  
185 sub setup {
186 my $self = ( @_ ? shift : __PACKAGE__ )->new ;
187 my $idSvrUrl = $configPackage->get( 'idSvrUrl' );
188 _push_url_arg( \$idSvrUrl , 'rnd' => $self->{ rnd }, );
189 $self->{ idSvrUrl } = $idSvrUrl;
190 my $action = $cgi->param( 'action' );
191 print $session->header;
192 if( $session->param( 'login' ) ){
193 if( defined( $action ) and $action eq 'logout' ){
194 $session->delete;
195 $session->flush;
196 $self->printLoginForm;
197 } elsif( defined( $cgi->param( 'trust_root' ) ) and length $cgi->param( 'trust_root' ) ){
198 my $trustRoot = $cgi->param( 'trust_root' );
199 $self->printTrustForm( encode_entities( $trustRoot ) );
200 } else {
201 $self->printLogoutForm;
202 }
203 } else {
204 $self->printLoginForm;
205 }
206 }
207  
208 =pod
209  
210 =item * redirect() and redirectMessage()
211  
212 overrideable for your inherited package needs. Use to take HTTP status and URL location as parameters.
213 redirectMessage returns the message for user.
214  
215 =cut
216  
217 sub redirect {
218 my( $self, $status, $location, ) = @_;
219 print $session->header( -status => $status, -location => $location, @_ );
220 if( substr( $status, 0, 3 ) eq '200' ){
221 print $location;
222 } else {
223 print $self->redirectMessage( $status, $location, );
224 }
225 }
226 sub redirectMessage {
227 my( $status, $location, ) = @_;
228 return <<EOF;
229 $htmlStyle->{start}<h1
230 >$status</h1
231 ><p
232 >The document is moved <a href='$location'>here.</a
233 ></p><hr
234 />nossa &mdash; Net::OpenID::Server::Standalone.$htmlStyle->{ end }
235 EOF
236 }
237  
238 sub isRedirectedToSsl{
239 my $self = shift;
240 my $mode = $cgi->param( 'openid.mode' );
241 if(
242 (
243 ( $mode eq 'checkid_setup' )
244 or
245 ( $mode eq 'checkid_immediate' )
246 )
247 and
248 ( $ENV{ SERVER_PORT } != 443 )
249 ){
250 my $url = 'https://'.$ENV{ HTTP_HOST };
251 $url .= $ENV{ REQUEST_URI };
252 $self->redirect( "301 SSL please", $url, );
253 }
254 }
255  
256 sub getUser {
257 my $self = shift;
258 my $authorized = 0;
259 my ($login, $pass) = getAuth();
260 my $users = $configPackage->get( 'users' );
261 if ( defined( $login ) and defined $users->{$login}) {
262 my $user = $users->{$login};
263 if( defined( $pass ) and ( $user->{pass} eq $self->callHashFunction( $pass ) ) ) {
264 $session->param( login => $login );
265 $session->flush;
266 $authorized = 1;
267 }
268 } elsif( defined( $session->param( "login" ) ) and length $session->param( "login" ) ){
269 $login = $session->param( "login" );
270 $authorized = 1;
271 }
272 if( $authorized ) {
273 return $login;
274 } else {
275 $self->requireAuth();
276 }
277 }
278 sub getAuth {
279 my( $login, $password ) = map{ defined( $cgi->param( $_ ) ) ? $cgi->param( $_ ) : '';
280 } qw/login password/;
281 if( defined( $login ) and length( $login )
282 and defined( $password ) and length( $password )
283 ){
284 return $login, $password;
285 } else {
286 return;
287 }
288 }
289 sub requireAuth {
290 my $self = shift;
291 my $params = $cgi->Vars;
292 # map{ delete( $params->{ $_ } ) if defined $params->{ $_ } } qw/login password action setup_trust_root/;
293 $params = {
294 map{ substr( $_, 7, length( $_ ) -7 ) => $cgi->param( $_ ) }
295 grep /^openid\./, $cgi->param
296 };
297 my $setupUrl = $configPackage->get( 'setupUrl' );
298 _push_url_arg( \$setupUrl, %$params );
299 print $self->redirect( "301 Login please", $setupUrl, );
300 return undef;
301 }
302  
303 sub isIdentity {
304 my ($user, $url) = @_;
305 return unless defined $user;
306 my $configUrl = $configPackage->get( 'users', $user, 'url' );
307 return defined( $configUrl ) and $configUrl eq $url;
308 }
309  
310 sub isTrusted {
311 my ($self, $user, $trustRoot, $isIdentity) = @_;
312 my $trusted = 0; my $setupTrustRoot = $cgi->param( 'setup_trust_root' );
313 if( defined( $user ) and defined( $isIdentity ) and $isIdentity ){
314 if( defined( $setupTrustRoot ) and $setupTrustRoot eq 'Yes' ){
315 $trusted = 1;
316 } else {
317 my $trustRootCref = $configPackage->get( 'users', $user, 'trust_root', );
318 if( defined( $trustRootCref ) and 'CODE' eq ref $trustRootCref ) {
319 $trusted = $trustRootCref->( $trustRoot );
320 }
321 }
322 if( defined( $setupTrustRoot ) and length $setupTrustRoot ){
323 unless( $trusted ){
324 $self->redirect( "301 Not Trusted", $trustRoot, );
325 exit;
326 }
327 }
328 }
329 return $trusted;
330 }
331 =pod
332  
333 =item * cgiHiddens()
334  
335 function that turns OpenID values for the default setup_map of the Net::OpenID::Server into the hidden inputs for the form on the setup URL.
336  
337 =cut
338 sub cgiHiddens {
339 my $cgi_htmled = { map{
340 encode_entities( $_, '<>&"\'' ) => encode_entities( $cgi->param( $_ ), '<>&"\'' )
341 } $cgi->param };
342 $cgi_htmled->{ mode } = 'checkid_setup';
343 $cgi_htmled = join "\n",
344 map {
345 my $val = $cgi_htmled->{ $_ };
346 "<input type='hidden' name='openid.$_' value='$val' />";
347 } keys %$cgi_htmled ;
348 return \$cgi_htmled;
349 }
350 =pod
351  
352 =item * printLoginForm()
353  
354 prints login form for setup script;
355  
356 =cut
357 sub printLoginForm {
358 my $self = shift;
359 my $idSvrUrl = $self->{ idSvrUrl };
360 my $hiddens = $self->cgiHiddens;
361 print <<EOF;
362 $htmlStyle->{ start }<form action='$idSvrUrl' method='POST'
363 >$$hiddens<table width='0' cellspacing='0' cellpadding='0' border='0'>
364 <tr>
365 <td>Login: </td><td><input type='text' name='login' /></td>
366 </tr><tr>
367 <td>Pzzwd: </td><td><input type='password' name='password' /></td>
368 </tr>
369 <tr><td colspan='2' align='center'><input type='submit' name='button' value='Go' /></td></tr>
370 </table></form>$htmlStyle->{ end }
371 EOF
372 }
373 =pod
374  
375 =item * printLoginForm()
376  
377 prints 'trust this root' form for setup script;
378  
379 =cut
380 sub printTrustForm {
381 my $self = shift;
382 my $trustRootHtmled = shift;
383 my $idSvrUrl = $self->{ idSvrUrl };
384 my $hiddens = $self->cgiHiddens;
385 print <<EOF;
386 $htmlStyle->{ start }<form action='$idSvrUrl' method='POST'
387 >$$hiddens<table width='0' cellspacing='0' cellpadding='0' border='0'>
388 <tr>
389 <tr><td colspan='2' align='center'>Trust this root?<br /><b>$trustRootHtmled</b></td></tr>
390 <tr><td align='center'><input type='submit' name='setup_trust_root' value='Yes' /></td><td align='center'><input type='submit' name='setup_trust_root' value='No' /></td>
391 </tr>
392 </table></form>$htmlStyle->{ end }
393 EOF
394 }
395 =pod
396  
397 =item * printLoginForm()
398  
399 prints logout form for setup script;
400  
401 =cut
402 sub printLogoutForm {
403 my $self = shift;
404 my $setupUrl = $self->{ setupUrl };
405 print <<EOF;
406 $htmlStyle->{ start }<form action='$setupUrl' method='POST'
407 ><input type='hidden' name='action' value='logout'
408 /><input type='submit' name='button' value='Out'
409 /></form>$htmlStyle->{ end }
410 EOF
411 }
412  
413 sub callHashFunction {
414 my( $self, $pass ) = @_;
415 no strict 'refs';
416 my $rv = *{ ref( $self ). '::' }->{ hashFunction }->( $pass );
417 use strict 'refs';
418 return $rv;
419 }
420  
421 =pod
422  
423 =back
424  
425 =head1 USAGE
426  
427 For 'do it quick' see the examples: index.html, id and setup. Also, L<Net::OpenID::Server::Standalone::Config> is an example about how you could set up your own ::Config. You should do it in any case.
428  
429 For more custom-made setup you can inherit Nossa like this:
430  
431 $ cat lib/MyApp/Nossa.pm
432 package MyApp::Nossa;
433
434 use strict;
435 use warnings;
436
437 # inheritance stuff
438 use base qw/Net::OpenID::Server::Standalone/;
439
440 # your own hash function initialization
441 use Digest::SHA256;
442 my $dig = Digest::SHA256::new( 512 );
443
444 # your own stylings around forms; override the print*Form methods for even more and/or inner styling
445 our $htmlStyle = { start => "<html><body height='100%'><table width='100%' height='100%'"
446 ."><tr><td height='100%' align='center' valign='middle'"
447 .">",
448 end => "</td></tr>"
449 ."</table></form></td></tr></table></body></html>",
450 };
451
452 # the hash function
453 sub hashFunction{
454 my $pass = shift;
455 $dig->hexhash( $pass );
456 }
457  
458 1;
459  
460 $ cat lib/MyApp/Nossa/Config.pm
461 package MyApp::Nossa::Config;
462  
463 use strict;
464 use warnings;
465
466 use base qw/Net::OpenID::Server::Standalone::Config/;
467
468 our $config = {
469 # set up your values here as described in L<Net::OpenID::Server::Standalone::Config>
470 };
471  
472 1;
473  
474 for different storage methods, you may want to set up your own get() in your Config package. You shouldn't need to 'use base N:O:S:Sa::Config' in such a case.
475  
476 $ cat id
477 #!/usr/bin/perl
478
479 use warnings;
480 use strict;
481 our $nossaLibDir;
482
483 BEGIN{
484 use File::Basename qw/dirname/;
485 use Cwd qw/realpath/;
486 $nossaLibDir = realpath( dirname( __FILE__ ).'/../lib' );
487 push( @INC, $nossaLibDir )
488 unless grep { $_ eq $nossaLibDir } @INC;
489 }
490
491 use MyApp::Nossa;
492
493 MyApp::Nossa->id;
494  
495 Same goes here for setup script except
496  
497 MyApp::Nossa->setup;
498  
499 is the last line.
500  
501 =head1 Downloads and more info
502  
503 =head2 Web links
504  
505 L<http://gitweb.vereshagin.org/nossa> - browseable repository with snapshots
506  
507 L<http://code.google.com/p/nossa/> - home page with actual links, etc.
508  
509 L<http://github.com/petr999/nossa> - GitHub page
510  
511 L<http://bugs.vereshagin.org/buglist.cgi?product=Nossa> for bugs and reporting of them
512  
513 =head2 VCS downloads
514  
515 Git users use this:
516  
517 git clone git://github.com/petr999/nossa
518  
519 Subversion users use one of these:
520  
521 svn checkout http://nossa.googlecode.com/svn/trunk/ nossa-read-only
522 svn checkout http://svn.github.com/petr999/nossa.git
523  
524 =head1 AUTHOR, LICENSE
525  
526 Peter Vereshagin <peter@vereshagin.org> ( L<http://vereshagin.org> ).
527 Based on stuff from:
528  
529 # Author: Alex Efros <powerman-asdf@yandex.ru>, 2008
530 # License: Public Domain
531  
532 License: consider BSD is the closest to be of that domain.
533  
534 =cut
535  
536 1;