initial commit
[catagits/Catalyst-Authentication-Credential-OpenID.git] / t / TestApp / lib / TestApp / Controller / Root.pm
1 package TestApp::Controller::Root;
2
3 use strict;
4 use warnings;
5 no warnings "uninitialized";
6 use base 'Catalyst::Controller';
7 use Net::OpenID::Server;
8
9 __PACKAGE__->config->{namespace} = '';
10
11 =head1 NAME
12
13 TestApp::Controller::Root - Root Controller for TestApp
14
15 =head1 DESCRIPTION
16
17 D'er... testing.
18
19 =cut
20
21
22 sub provider : Local {
23     my ( $self, $c, $username ) = @_;
24
25     my $nos = Net::OpenID::Server
26         ->new(
27               get_args     => $c->req->query_params,
28               post_args    => $c->req->body_params,
29               get_user => sub { $c->user },
30               is_identity  => sub {
31                   my ( $user, $identity_url ) = @_;
32                   return unless $user;
33                   my ( $check ) = $identity_url =~ /(\w+)\z/;
34                   return $check eq $user->id; # simple auth here
35               },
36               is_trusted => sub {
37                   my ( $user, $trust_root, $is_identity ) = @_;
38                   return $is_identity; # enough that they passed is_identity
39               },
40               setup_url => $c->uri_for($c->req->path, {moo => "setup"}),
41               server_secret => $c->config->{startup_time},
42               );
43
44   # From your OpenID server endpoint:
45
46     my ( $type, $data ) = $nos->handle_page;
47
48     if ($type eq "redirect")
49     {
50         $c->res->redirect($data);
51     }
52     elsif ($type eq "setup")
53     {
54         my %setup_opts = %{$data};
55         $c->res->body(<<"");
56 You're not signed in so you can't be verified.
57 <a href="/login">Sign in</a> | <a href="/signin_openid">OpenId</a>.
58
59       # it's then your job to redirect them at the end to "return_to"
60       # (or whatever you've named it in setup_map)
61     }
62     else
63     {
64         $c->res->content_type($type);
65         if ( $username )
66         {
67             my $server_uri = $c->uri_for($c->req->path);
68             $data =~ s,(?=</head>),<link rel="openid.server" href="$server_uri" />,;
69         }
70         $c->res->body($data);
71     }
72 }
73
74 sub logout : Local {
75     my($self, $c) = @_;
76     $c->logout if $c->user_exists;
77     $c->delete_session();
78     $c->res->redirect($c->uri_for("/"));
79 }
80
81 sub login : Local {
82     my($self, $c) = @_;
83
84     if ( $c->req->method eq 'POST'
85          and
86          $c->authenticate({ username => $c->req->body_params->{username},
87                             password => $c->req->body_params->{password} }) )
88     {
89 #        $c->res->body("You are signed in!");
90         $c->res->redirect($c->uri_for("/"));
91     }
92     else
93     {
94         my $action = $c->req->uri->path;
95         $c->res->body(<<"");
96 <html><head/><body><form name="login" action="$action" method="POST">
97   <input type="text" name="username" />
98   <input type="password" name="password" />
99   <input type="submit" value="Sign in" />
100 </form>
101 </body></html>
102
103     }
104 }
105
106
107 sub signin_openid : Local {
108     my($self, $c) = @_;
109
110     if ( $c->authenticate({}, "openid") )
111     {
112         $c->res->body("You did it with OpenID!");
113     }
114     else
115     {
116         my $action = $c->req->uri->path;
117         $c->res->body(<<"");
118  <form action="$action" method="GET" name="openid">
119   <input type="text" name="openid_identifier" class="openid" size="50" />
120   <input type="submit" value="Sign in with OpenID" />
121   </form>
122
123     }
124 }
125
126 sub default : Private {
127     my ( $self, $c ) = @_;
128     $c->response->body(
129                        join(" ",
130                             "You are",
131                             $c->user ? "" : "not",
132                             "signed in. <br/>",
133                             $c->user ? ( $c->user->id || %{$c->user} ) : '<a href="/login">Sign in</a> | <a href="/signin_openid">OpenId</a>.'
134                             )
135                        );
136 }
137
138 sub end : Private {
139     my ( $self, $c ) = @_;
140     $c->response->content_type("text/html");
141 }
142
143 =head1 LICENSE
144
145 This library is free software, you can redistribute it and/or modify
146 it under the same terms as Perl itself.
147
148 =cut
149
150 1;