diff options
author | Daniel Friesel <derf@finalrewind.org> | 2019-03-02 18:08:48 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2019-03-02 18:08:48 +0100 |
commit | 856a66c0bea917af8c7efd907d2200c01bab382b (patch) | |
tree | 20a0de23381c7a1593ed37529e1fcc87022364d9 /index.pl | |
parent | c6fd0a0efb8582e48d2eacafc0968938126bddd4 (diff) |
implement user/password/csrf checks for login form
Diffstat (limited to 'index.pl')
-rwxr-xr-x | index.pl | 63 |
1 files changed, 43 insertions, 20 deletions
@@ -34,26 +34,28 @@ my %action_type = ( undo => 3, ); -app->plugin(authentication => { - autoload_user => 1, - session_key => 'foodor', - load_user => sub { - my ($app, $uid) = @_; - if ($uid == 1) { - return { - name => 'derf', - }; - } - return undef; - }, - validate_user => sub { - my ($c, $username, $password, $extradata) = @_; - if ($username eq 'derf' and $password eq 'hallo') { - return 1; - } - return undef; - }, -}); +app->plugin( + authentication => { + autoload_user => 1, + session_key => 'foodor', + load_user => sub { + my ( $app, $uid ) = @_; + if ( $uid == 1 ) { + return { + name => 'dev', + }; + } + return undef; + }, + validate_user => sub { + my ( $c, $username, $password, $extradata ) = @_; + if ( $username eq 'dev' and $password eq 'ohai' ) { + return 1; + } + return undef; + }, + } +); app->defaults( layout => 'default' ); @@ -799,6 +801,27 @@ get '/x/login' => sub { $self->render('login'); }; +post '/x/login' => sub { + my ($self) = @_; + my $user = $self->req->param('user'); + my $password = $self->req->param('password'); + + if ( $self->validation->csrf_protect->has_error('csrf_token') ) { + $self->render( + 'login', + invalid => 'csrf', + ); + } + else { + if ( $self->authenticate( $user, $password ) ) { + $self->redirect_to('/'); + } + else { + $self->render( 'login', invalid => 'credentials' ); + } + } +}; + get '/x/register' => sub { my ($self) = @_; $self->render('register'); |