c6000eb03267840b4f996d535ce891f238478cd8
[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. Has an OpenID provider to test the OpenID credential against.
18
19 =cut
20
21 sub provider : Local {
22     my ( $self, $c, $username ) = @_;
23
24     my $nos = Net::OpenID::Server
25         ->new(
26               get_args     => $c->req->query_params,
27               post_args    => $c->req->body_params,
28               get_user => sub { $c->user },
29               is_identity  => sub {
30                   my ( $user, $identity_url ) = @_;
31                   return unless $user;
32                   my ( $check ) = $identity_url =~ /(\w+)\z/;
33                   return $check eq $user->id; # simple auth here
34               },
35               is_trusted => sub {
36                   my ( $user, $trust_root, $is_identity ) = @_;
37                   return $is_identity; # enough that they passed is_identity
38               },
39               setup_url => $c->uri_for($c->req->path, {moo => "setup"}),
40               server_secret => $c->config->{startup_time},
41               );
42
43   # From your OpenID server endpoint:
44
45     my ( $type, $data ) = $nos->handle_page;
46
47     if ($type eq "redirect")
48     {
49         $c->res->redirect($data);
50     }
51     elsif ($type eq "setup")
52     {
53         my %setup_opts = %{$data};
54         $c->res->body(<<"");
55 You're not signed in so you can't be verified.
56 <a href="/login">Sign in</a> | <a href="/signin_openid">OpenId</a>.
57
58       # it's then your job to redirect them at the end to "return_to"
59       # (or whatever you've named it in setup_map)
60     }
61     else
62     {
63         $c->res->content_type($type);
64         if ( $username )
65         {
66             my $server_uri = $c->uri_for($c->req->path);
67             $data =~ s,(?=</head>),<link rel="openid.server" href="$server_uri" />,;
68         }
69         $c->res->body($data);
70     }
71 }
72
73 sub logout : Local {
74     my($self, $c) = @_;
75     $c->logout if $c->user_exists;
76     $c->delete_session();
77     $c->res->redirect($c->uri_for("/"));
78 }
79
80 sub login : Local {
81     my($self, $c) = @_;
82
83     if ( $c->req->method eq 'POST'
84          and
85          $c->authenticate({ username => $c->req->body_params->{username},
86                             password => $c->req->body_params->{password} }) )
87     {
88 #        $c->res->body("You are signed in!");
89         $c->res->redirect($c->uri_for("/"));
90     }
91     else
92     {
93         my $action = $c->req->uri->path;
94         $c->res->body(<<"");
95 <html><head/><body><form name="login" action="$action" method="POST">
96   <input type="text" name="username" />
97   <input type="password" name="password" />
98   <input type="submit" value="Sign in" />
99 </form>
100 </body></html>
101
102     }
103 }
104
105 sub signin_openid : Local {
106     my($self, $c) = @_;
107
108     if ( $c->authenticate({}, "openid") )
109     {
110         $c->res->body("You did it with OpenID!");
111     }
112     else
113     {
114         my $action = $c->req->uri->path;
115         $c->res->body(<<"");
116  <form action="$action" method="GET" name="openid">
117   <input type="text" name="openid_identifier" class="openid" size="50" />
118   <input type="submit" value="Sign in with OpenID" />
119   </form>
120
121     }
122 }
123
124 sub default : Private {
125     my ( $self, $c ) = @_;
126     $c->response->body(
127                        join(" ",
128                             "You are",
129                             $c->user ? "" : "not",
130                             "signed in. <br/>",
131                             $c->user ? ( $c->user->id || %{$c->user} ) : '<a href="/login">Sign in</a> | <a href="/signin_openid">OpenId</a>.'
132                             )
133                        );
134 }
135
136 sub not_a_valid_openid_uri : Global {
137     my ( $self, $c ) = @_;
138     $c->response->body("OPENID. UR DOIN IT RONG.");
139 }
140
141 sub i_can_has_tarpit : Global {
142     my ( $self, $c ) = @_;
143     local $/ = 1;
144     $c->response->content_type("text/html");
145     # Expect an arbitrary, biggish amount of content; it's a lie.
146     $c->response->headers->header("Content-length" => 1_024 * 100);
147     # Do this for 30 seconds; tests will timeout at 10 or 15.
148     sleep 1 && $c->response->write("sucker\n") for 1 .. 30;
149 }
150
151 sub end : Private {
152     my ( $self, $c ) = @_;
153     $c->response->content_type("text/html") unless $c->response->content_type;
154 }
155
156 =head1 LICENSE
157
158 This library is free software, you can redistribute it and modify
159 it under the same terms as Perl itself.
160
161 =cut
162
163 1;
164 package TestApp::Controller::Root;
165
166 use strict;
167 use warnings;
168 no warnings "uninitialized";
169 use base 'Catalyst::Controller';
170 use Net::OpenID::Server;
171
172 __PACKAGE__->config->{namespace} = '';
173
174 =head1 NAME
175
176 TestApp::Controller::Root - Root Controller for TestApp.
177
178 =head1 DESCRIPTION
179
180 D'er... testing. Has an OpenID provider to test the OpenID credential against.
181
182 =cut
183
184 sub provider : Local {
185     my ( $self, $c, $username ) = @_;
186
187     my $nos = Net::OpenID::Server
188         ->new(
189               get_args     => $c->req->query_params,
190               post_args    => $c->req->body_params,
191               get_user => sub { $c->user },
192               is_identity  => sub {
193                   my ( $user, $identity_url ) = @_;
194                   return unless $user;
195                   my ( $check ) = $identity_url =~ /(\w+)\z/;
196                   return $check eq $user->id; # simple auth here
197               },
198               is_trusted => sub {
199                   my ( $user, $trust_root, $is_identity ) = @_;
200                   return $is_identity; # enough that they passed is_identity
201               },
202               setup_url => $c->uri_for($c->req->path, {moo => "setup"}),
203               server_secret => $c->config->{startup_time},
204               );
205
206   # From your OpenID server endpoint:
207
208     my ( $type, $data ) = $nos->handle_page;
209
210     if ($type eq "redirect")
211     {
212         $c->res->redirect($data);
213     }
214     elsif ($type eq "setup")
215     {
216         my %setup_opts = %{$data};
217         $c->res->body(<<"");
218 You're not signed in so you can't be verified.
219 <a href="/login">Sign in</a> | <a href="/signin_openid">OpenId</a>.
220
221       # it's then your job to redirect them at the end to "return_to"
222       # (or whatever you've named it in setup_map)
223     }
224     else
225     {
226         $c->res->content_type($type);
227         if ( $username )
228         {
229             my $server_uri = $c->uri_for($c->req->path);
230             $data =~ s,(?=</head>),<link rel="openid.server" href="$server_uri" />,;
231         }
232         $c->res->body($data);
233     }
234 }
235
236 sub logout : Local {
237     my($self, $c) = @_;
238     $c->logout if $c->user_exists;
239     $c->delete_session();
240     $c->res->redirect($c->uri_for("/"));
241 }
242
243 sub login : Local {
244     my($self, $c) = @_;
245
246     if ( $c->req->method eq 'POST'
247          and
248          $c->authenticate({ username => $c->req->body_params->{username},
249                             password => $c->req->body_params->{password} }) )
250     {
251 #        $c->res->body("You are signed in!");
252         $c->res->redirect($c->uri_for("/"));
253     }
254     else
255     {
256         my $action = $c->req->uri->path;
257         $c->res->body(<<"");
258 <html><head/><body><form name="login" action="$action" method="POST">
259   <input type="text" name="username" />
260   <input type="password" name="password" />
261   <input type="submit" value="Sign in" />
262 </form>
263 </body></html>
264
265     }
266 }
267
268 sub signin_openid : Local {
269     my($self, $c) = @_;
270
271     if ( $c->authenticate({}, "openid") )
272     {
273         $c->res->body("You did it with OpenID!");
274     }
275     else
276     {
277         my $action = $c->req->uri->path;
278         $c->res->body(<<"");
279  <form action="$action" method="GET" name="openid">
280   <input type="text" name="openid_identifier" class="openid" size="50" />
281   <input type="submit" value="Sign in with OpenID" />
282   </form>
283
284     }
285 }
286
287 sub default : Private {
288     my ( $self, $c ) = @_;
289     $c->response->body(
290                        join(" ",
291                             "You are",
292                             $c->user ? "" : "not",
293                             "signed in. <br/>",
294                             $c->user ? ( $c->user->id || %{$c->user} ) : '<a href="/login">Sign in</a> | <a href="/signin_openid">OpenId</a>.'
295                             )
296                        );
297 }
298
299 sub end : Private {
300     my ( $self, $c ) = @_;
301     $c->response->content_type("text/html");
302 }
303
304 =head1 LICENSE
305
306 This library is free software, you can redistribute it and modify
307 it under the same terms as Perl itself.
308
309 =cut
310
311 1;