1package TestApp::Controller::Root;
2
3use strict;
4use warnings;
5no warnings "uninitialized";
6use base 'Catalyst::Controller';
7use Net::OpenID::Server;
8
9__PACKAGE__->config->{namespace} = '';
10
11=head1 NAME
12
13TestApp::Controller::Root - Root Controller for TestApp.
14
15=head1 DESCRIPTION
16
17D'er... testing. Has an OpenID provider to test the OpenID credential against.
18
19=cut
20
21sub 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(<<"");
55You'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
73sub 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
80sub 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
105sub 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
124sub 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
136sub end : Private {
137    my ( $self, $c ) = @_;
138    $c->response->content_type("text/html");
139}
140
141=head1 LICENSE
142
143This library is free software, you can redistribute it and modify
144it under the same terms as Perl itself.
145
146=cut
147
1481;
149