| blib/lib/Test/OpenID/Server.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 7 | 9 | 77.7 |
| branch | n/a | ||
| condition | n/a | ||
| subroutine | 3 | 3 | 100.0 |
| pod | n/a | ||
| total | 10 | 12 | 83.3 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | #!perl | ||||||
| 2 | 3 | 3 | 1141 | use warnings; | |||
| 3 | 3 | ||||||
| 3 | 79 | ||||||
| 3 | 3 | 3 | 9 | use strict; | |||
| 3 | 2 | ||||||
| 3 | 66 | ||||||
| 4 | |||||||
| 5 | package Test::OpenID::Server; | ||||||
| 6 | 3 | 3 | 1514 | use Net::OpenID::Server; | |||
| 0 | |||||||
| 0 | |||||||
| 7 | use base qw/Test::HTTP::Server::Simple HTTP::Server::Simple::CGI/; | ||||||
| 8 | |||||||
| 9 | our $VERSION = '0.03'; | ||||||
| 10 | |||||||
| 11 | =head1 NAME | ||||||
| 12 | |||||||
| 13 | Test::OpenID::Server - setup a simulated OpenID server | ||||||
| 14 | |||||||
| 15 | =head1 SYNOPSIS | ||||||
| 16 | |||||||
| 17 | Test::OpenID::Server will provide a server to test your OpenID client | ||||||
| 18 | against. To use it, do something like this: | ||||||
| 19 | |||||||
| 20 | use Test::More tests => 1; | ||||||
| 21 | use Test::OpenID::Server; | ||||||
| 22 | my $server = Test::OpenID::Server->new; | ||||||
| 23 | my $url_root = $server->started_ok("server started ok"); | ||||||
| 24 | |||||||
| 25 | Now you can run your OpenID tests against the URL in C<$url_root>. Identities | ||||||
| 26 | are any URL in the form of C<$url_root . "/foo">. There is one special | ||||||
| 27 | identity: C. This identity will causes the OpenID server | ||||||
| 28 | to return a non-identity page (which will mean the OpenID client won't find an | ||||||
| 29 | identity). Every other identity will return a successful authentication. | ||||||
| 30 | |||||||
| 31 | =head1 METHODS | ||||||
| 32 | |||||||
| 33 | =head2 new | ||||||
| 34 | |||||||
| 35 | Create a new test OpenID server | ||||||
| 36 | |||||||
| 37 | =cut | ||||||
| 38 | |||||||
| 39 | sub new { | ||||||
| 40 | my $class = shift; | ||||||
| 41 | my $port = shift; | ||||||
| 42 | |||||||
| 43 | $port = int(rand(5000) + 10000) if not defined $port; | ||||||
| 44 | |||||||
| 45 | my $self = $class->SUPER::new( $port ); | ||||||
| 46 | return $self; | ||||||
| 47 | } | ||||||
| 48 | |||||||
| 49 | =head2 started_ok | ||||||
| 50 | |||||||
| 51 | Test whether the server started, and if it did, return the URL it's | ||||||
| 52 | at. | ||||||
| 53 | |||||||
| 54 | =cut | ||||||
| 55 | |||||||
| 56 | #=head2 add_identity NAME | ||||||
| 57 | # | ||||||
| 58 | #Adds an OpenID identity to the server and returns the identity's URL. | ||||||
| 59 | # | ||||||
| 60 | #=cut | ||||||
| 61 | # | ||||||
| 62 | #sub add_identity { | ||||||
| 63 | # my $self = shift; | ||||||
| 64 | # my $id = shift; | ||||||
| 65 | # | ||||||
| 66 | # if ( not $self->_is_identity( $id ) ) { | ||||||
| 67 | # $self->{_identities}{$id} = {}; | ||||||
| 68 | # } | ||||||
| 69 | # return $self->_identity_url( $id ); | ||||||
| 70 | #} | ||||||
| 71 | |||||||
| 72 | #=head2 delete_identity NAME | ||||||
| 73 | # | ||||||
| 74 | #Removes an OpenID identity from the server. | ||||||
| 75 | # | ||||||
| 76 | #=cut | ||||||
| 77 | # | ||||||
| 78 | #sub delete_identity { | ||||||
| 79 | # my $self = shift; | ||||||
| 80 | # my $id = shift; | ||||||
| 81 | # delete $self->{_identities}{$id}; | ||||||
| 82 | #} | ||||||
| 83 | |||||||
| 84 | sub _is_identity { | ||||||
| 85 | my $self = shift; | ||||||
| 86 | my $id = shift; | ||||||
| 87 | return lc $id ne 'unknown' ? $id : undef; | ||||||
| 88 | } | ||||||
| 89 | |||||||
| 90 | sub _identity_url { | ||||||
| 91 | my $self = shift; | ||||||
| 92 | my $id = shift; | ||||||
| 93 | return "http://$ENV{HTTP_HOST}/$id"; | ||||||
| 94 | } | ||||||
| 95 | |||||||
| 96 | #=head2 modify_trust NAME, URL, BOOLEAN | ||||||
| 97 | # | ||||||
| 98 | #Sets whether or not URL is trusted by NAME. | ||||||
| 99 | # | ||||||
| 100 | #=cut | ||||||
| 101 | # | ||||||
| 102 | #sub modify_trust { | ||||||
| 103 | # my $self = shift; | ||||||
| 104 | # my ( $id, $url, $trusted ) = @_; | ||||||
| 105 | # $self->{_identities}{$id}{$url} = $trusted; | ||||||
| 106 | #} | ||||||
| 107 | |||||||
| 108 | =head1 INTERAL METHODS | ||||||
| 109 | |||||||
| 110 | These methods implement the HTTP server (see L |
||||||
| 111 | You shouldn't call them. | ||||||
| 112 | |||||||
| 113 | =head2 handle_request | ||||||
| 114 | |||||||
| 115 | =cut | ||||||
| 116 | |||||||
| 117 | sub handle_request { | ||||||
| 118 | my $self = shift; | ||||||
| 119 | my $cgi = shift; | ||||||
| 120 | |||||||
| 121 | if ( $ENV{'PATH_INFO'} eq '/openid.server' ) { | ||||||
| 122 | # We're dealing with the OpenID server endpoint | ||||||
| 123 | |||||||
| 124 | my $nos = Net::OpenID::Server->new( | ||||||
| 125 | args => $cgi, | ||||||
| 126 | get_user => \&_get_user, | ||||||
| 127 | is_identity => sub { $self->_is_identity( $_[1] ) }, | ||||||
| 128 | is_trusted => sub { return 1 }, | ||||||
| 129 | server_secret => 'squeamish_ossifrage', | ||||||
| 130 | setup_url => "http://example.com/non-existant", | ||||||
| 131 | ); | ||||||
| 132 | my ($type, $data) = $nos->handle_page( redirect_for_setup => 1 ); | ||||||
| 133 | if ($type eq "redirect") { | ||||||
| 134 | print "HTTP/1.0 301 REDIRECT\r\n"; # probably OK by now | ||||||
| 135 | print "Location: $data\r\n\r\n"; | ||||||
| 136 | } else { | ||||||
| 137 | print "HTTP/1.0 200 OK\r\n"; # probably OK by now | ||||||
| 138 | print "Content-Type: $type\r\n\r\n$data"; | ||||||
| 139 | } | ||||||
| 140 | } | ||||||
| 141 | else { | ||||||
| 142 | # We're dealing with an normal page request | ||||||
| 143 | print "HTTP/1.0 200 OK\r\n"; | ||||||
| 144 | print "Content-Type: text/html\r\n\r\n"; | ||||||
| 145 | |||||||
| 146 | my ($id) = $ENV{'PATH_INFO'} =~ m{/(.*)$}; | ||||||
| 147 | |||||||
| 148 | if ( $self->_is_identity( $id ) ) { | ||||||
| 149 | print <<" END"; | ||||||
| 150 | |||||||
| 151 | |||||||
| 152 | |||||||
| 153 | |||||||
| 154 | |||||||
| 155 | OpenID identity page for $id. |
||||||
| 156 | |||||||
| 157 | |||||||
| 158 | END | ||||||
| 159 | } | ||||||
| 160 | else { | ||||||
| 161 | print <<" END"; | ||||||
| 162 | |||||||
| 163 | |||||||
| 164 | "$id" is not an identity we recognize. |
||||||
| 165 | |||||||
| 166 | |||||||
| 167 | END | ||||||
| 168 | } | ||||||
| 169 | } | ||||||
| 170 | } | ||||||
| 171 | |||||||
| 172 | sub _get_user { | ||||||
| 173 | return "user"; | ||||||
| 174 | } | ||||||
| 175 | |||||||
| 176 | =head1 AUTHORS | ||||||
| 177 | |||||||
| 178 | =head1 COPYRIGHT | ||||||
| 179 | |||||||
| 180 | Copyright (c) 2007 Best Practical Solutions, LLC. | ||||||
| 181 | |||||||
| 182 | =head1 LICENSE | ||||||
| 183 | |||||||
| 184 | You may distribute this module under the same terms as Perl 5.8 itself. | ||||||
| 185 | |||||||
| 186 | =cut | ||||||
| 187 | |||||||
| 188 | 1; |