Minor fixes to user login
Change-Id: I2e6050dc54832e4adc594c3629b345754e40ba01
diff --git a/lib/Kalamar/Controller/User.pm b/lib/Kalamar/Controller/User.pm
index 1584225..6fb1f02 100644
--- a/lib/Kalamar/Controller/User.pm
+++ b/lib/Kalamar/Controller/User.pm
@@ -7,15 +7,18 @@
# Validate input
my $v = $c->validation;
- $v->required('handle_or_email');
- $v->required('pwd');
+ $v->required('handle_or_email', 'trim');
+ $v->required('pwd', 'trim');
# Login user
- $c->user->login(
+ if ($c->user->login(
$v->param('handle_or_email'),
$v->param('pwd')
- );
+ )) {
+ $c->notify(success => 'Login successful!');
+ };
+ # return $c->render(text => 'ok');
return $c->redirect_to('/');
};
diff --git a/lib/Kalamar/Plugin/KalamarUser.pm b/lib/Kalamar/Plugin/KalamarUser.pm
index 76233c5..81a5fda 100644
--- a/lib/Kalamar/Plugin/KalamarUser.pm
+++ b/lib/Kalamar/Plugin/KalamarUser.pm
@@ -24,7 +24,10 @@
# Set API!
$plugin->api($param->{api}) or return;
- $plugin->ua(Mojo::UserAgent->new);
+ $plugin->ua(Mojo::UserAgent->new(
+ connect_timeout => 15,
+ inactivity_timeout => 60
+ ));
# Get the user token necessary for authorization
$mojo->helper(
@@ -48,12 +51,17 @@
'user.ua' => sub {
my $c = shift;
my $auth = $c->user_auth;
+ my $client = $c->req->headers->header('X-Forwarded-For');
+
return $plugin->ua unless $auth;
+
my $ua = Mojo::UserAgent->new;
$ua->on(
start => sub {
my ($ua, $tx) = @_;
- $tx->req->headers->header('Authorization' => $auth);
+ my $headers = $tx->req->headers;
+ $headers->header('Authorization' => $auth);
+ $headers->header('X-Forwarded-For' => $client);
}
);
return $ua;
@@ -68,16 +76,24 @@
return if (index($user, ':') >= 0);
+ $c->app->log->debug("Login from user $user:$pwd");
+
my $url = Mojo::URL->new($plugin->api)->path('auth/apiToken');
my $tx = $plugin->ua->get($url => {
- Authorization => 'Basic ' . b($user . ':' . $pwd)->b64_encode
+ Authorization => 'Basic ' . b($user . ':' . $pwd)->b64_encode->trim
});
# Login successful
if (my $res = $tx->success) {
+ $c->app->log->debug("Transaction: " . $res->to_string);
+
my $jwt = $res->json;
+ unless ($jwt) {
+ $c->notify(error => 'Response is no valid JWT (remote)');
+ return;
+ };
# TODO: Deal with user return values.
@@ -103,6 +119,7 @@
($e->{code} ? $e->{code} . ': ' : '') .
$e->{message} . ' for Login (remote)'
);
+ $c->app->log->debug($e->{code} . ($e->{message} ? ' - ' . $e->{message} : ''));
};
$mojo->log->debug(qq!Login fail: "$user"!);
diff --git a/t/user.t b/t/user.t
deleted file mode 100644
index 22297d3..0000000
--- a/t/user.t
+++ /dev/null
@@ -1,19 +0,0 @@
-use Mojo::Base -strict;
-use lib;
-use Test::More;
-use Test::Mojo;
-use Data::Dumper;
-
-$ENV{MOJO_USERAGENT_DEBUG} = 1;
-
-my $t = Test::Mojo->new('Kalamar');
-
-my $c = $t->app->build_controller;
-
-# Login with user credentials
-ok($c->user->login('test_h', 'p278h'), 'Login with demo user');
-is($c->stash('user'), 'test_h', 'Kustvakt is logged in');
-like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
-
-done_testing;
-__END__