1e87b3762dee8a7e084130a8dc211cc089428347
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Users.pm
1 package stemmaweb::Controller::Users;
2 use Moose;
3 use namespace::autoclean;
4
5 use Google::JWT;
6
7 use JSON::MaybeXS;
8 use JSON::WebToken;
9
10 use MIME::Base64;
11
12 BEGIN {extends 'CatalystX::Controller::Auth'; }
13 with 'Catalyst::TraitFor::Controller::reCAPTCHA';
14
15 =head1 NAME
16
17 stemmaweb::Controller::Users - Catalyst Controller
18
19 =head1 DESCRIPTION
20
21 The Users controller is based on L<CatalystX::Controller::Auth>, see
22 there for most of the functionality. Any localised parts are described
23 below.
24
25 This controller uses L<Catalyst::TraitFor::Controller::reCAPTCHA> to
26 create and check a reCaptcha form shown on the C<register> form to
27 help prevent spam signups.
28
29 =head1 METHODS
30
31 =cut
32
33 sub base :Chained('/') :PathPart('') :CaptureArgs(0)
34 {
35         my ( $self, $c ) = @_;
36
37         $self->next::method( $c );
38 }
39
40 =head2 index
41
42 The index action is not currently used.
43
44 =cut
45
46 sub index :Path :Args(0) {
47     my ( $self, $c ) = @_;
48
49     $c->response->body('Matched stemmaweb::Controller::Users in Users.');
50 }
51
52 =head2 login with openid
53
54 Logging in with openid/google requires two passes through the login
55 action, on the 2nd pass the C<openid-check> value is passed in when
56 the openid providing webserver links the user back to the stemmaweb
57 site. This adaptation to the C<login> action sets the realm we are
58 authenticating against to be C<openid> in this case.
59
60 =cut
61
62 before login => sub {
63   my($self, $c) = @_;
64   $c->req->param( realm => 'openid')
65     if $c->req->param('openid-check');
66
67   if ($c->req->params->{email} && $c->req->params->{id_token}) {
68     $c->req->param( realm => 'google');
69   }
70
71   $c->stash->{google_client_id} = $c->config->{'Authentication::Credential::Google'}->{client_id};
72 };
73
74 =head2 register with recaptcha
75
76 This adapts the C<register> action to add the recaptcha HTML to the
77 page, and verify the recaptcha info entered is correct when the form
78 is submitted. If the recaptcha is not correct, we just redisplay the
79 form with an error message.
80
81 =cut
82
83 before register => sub {
84     my ($self, $c) = @_;
85
86     ## Puts HTML into stash in "recaptcha" key.
87     $c->forward('captcha_get');
88
89     ## When submitting, check recaptcha passes, else re-draw form
90     if($c->req->method eq 'POST') {
91         if(!$c->forward('captcha_check') || 0 ) {
92             ## Need these two lines to detach, so end can draw the correct template again:
93             my $form = $self->form_handler->new( active => [ $self->login_id_field, 'password', 'confirm_password' ] );
94             $c->stash( template => $self->register_template, form => $form );
95
96             $c->detach();
97         }
98     }
99 };
100
101 =head2 success
102
103 A stub page returned on login / registration success.
104
105 =cut
106
107 sub success :Local :Args(0) {
108     my ( $self, $c ) = @_;
109
110         $c->load_status_msgs;
111     $c->stash->{template} = 'auth/success.tt';
112 }
113
114 =head2 post_logout
115
116 Return to the index page, not to the login page.
117
118 =cut
119
120 sub post_logout {
121         my( $self, $c ) = @_;
122         $c->response->redirect( $c->uri_for_action( '/index' ) );
123         $c->detach;
124 }
125
126 =head1 AUTHOR
127
128 A clever guy
129
130 =head1 LICENSE
131
132 This library is free software. You can redistribute it and/or modify
133 it under the same terms as Perl itself.
134
135 =cut
136
137 __PACKAGE__->meta->make_immutable;
138
139 1;