Fixed authorization in caching

Change-Id: I00aabe2ba4eaef430124c4991789c007b9125cca
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index d8e1386..340d1fc 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -207,12 +207,11 @@
   });
 
   # Base query route
-  $r->get('/')->to('search#query')->name('index');
+  $r->get('/')->to('search2#query')->name('index');
   $r->get('/q2')->to('search2#query');
 
   # Collection route
   $r->get('/corpus')->to('Search#corpus_info')->name('corpus');
-  # $r->get('/collection/:id')->to('Search#corpus_info')->name('collection');
 
   # Documentation routes
   $r->get('/doc')->to('documentation#page', page => 'korap')->name('doc_start');
@@ -236,8 +235,8 @@
   my $user = $r->any('/user')->to(controller => 'User');
   $user->post('/login')->to(action => 'login')->name('login');
   $user->get('/logout')->to(action => 'logout')->name('logout');
-#  $r->any('/register')->to(action => 'register')->name('register');
-#  $r->any('/forgotten')->to(action => 'pwdforgotten')->name('pwdforgotten');
+  # $r->any('/register')->to(action => 'register')->name('register');
+  # $r->any('/forgotten')->to(action => 'pwdforgotten')->name('pwdforgotten');
 
   # Default user is called 'korap'
   # $r->route('/user/:user/:collection')
diff --git a/lib/Kalamar/Controller/Search2.pm b/lib/Kalamar/Controller/Search2.pm
index e16a744..397d7c5 100644
--- a/lib/Kalamar/Controller/Search2.pm
+++ b/lib/Kalamar/Controller/Search2.pm
@@ -381,18 +381,15 @@
   # Set result values
   $self->stash(items_per_page => $meta->{itemsPerPage});
 
-  # Set authorization
-  # $index->authorized($meta->{authorized}) if $meta->{authorized};
+  ## Bouncing query
+  ##  if ($json->{query}) {
+  ##    $index->query_jsonld($json->{query});
+  ##  };
 
-  # Bouncing query
-  #  if ($json->{query}) {
-  #    $index->query_jsonld($json->{query});
-  #  };
-
-  # Legacy
-  # elsif ($json->{request}->{query}) {
-  #   $index->query_jsonld($json->{request}->{query});
-  # };
+  ## Legacy
+  ## elsif ($json->{request}->{query}) {
+  ##   $index->query_jsonld($json->{request}->{query});
+  ## };
 
 
   if ($meta->{totalResults}) {
diff --git a/lib/Kalamar/Plugin/KalamarErrors.pm b/lib/Kalamar/Plugin/KalamarErrors.pm
index 977ecc5..fd9ead3 100644
--- a/lib/Kalamar/Plugin/KalamarErrors.pm
+++ b/lib/Kalamar/Plugin/KalamarErrors.pm
@@ -84,7 +84,6 @@
 
       # There is json
       if ($json) {
-        $c->stash(api_response => $json);
 
         # There are errors
         if ($c->notify_on_errors($json)) {
diff --git a/lib/Kalamar/Plugin/KalamarHelpers.pm b/lib/Kalamar/Plugin/KalamarHelpers.pm
index 7d1e1b8..b0006c3 100644
--- a/lib/Kalamar/Plugin/KalamarHelpers.pm
+++ b/lib/Kalamar/Plugin/KalamarHelpers.pm
@@ -284,17 +284,28 @@
 
       # In case the user is not known, it is assumed,
       # the user is not logged in
-      my $user = $c->stash('user') // 'not_logged_in';
+      my $user = $c->stash('user');
+      unless ($user) {
+        $user = $c->session('user');
+        if ($user) {
+          $c->stash(user => $user);
+        }
+        else {
+          $user = 'not_logged_in';
+        }
+      };
 
       # Set api request for debugging
       my $cache_str = "$method-$user-" . $url->to_string;
-      $c->stash(api_request => $cache_str);
+      $c->stash(api_request => $url->to_string);
 
       if ($c->no_cache) {
         return $c->user->auth_request_p($method => $url)->then(
           sub {
+            my $json = shift;
             # Catch errors and warnings
-            return $c->catch_errors_and_warnings(shift)
+            $c->stash(api_response => $json);
+            return $c->catch_errors_and_warnings($json);
           }
         );
       };
@@ -315,6 +326,7 @@
           sub {
             my $json = shift;
             $c->notify_on_warnings($json);
+            $c->stash(api_response => $json);
             return $json;
           }
         );
@@ -323,14 +335,17 @@
       # Resolve request
       return $c->user->auth_request_p($method => $url)->then(
         sub {
+          my $json = shift;
           # Catch errors and warnings
-          return $c->catch_errors_and_warnings(shift)
+          $c->stash(api_response => $json);
+          return $c->catch_errors_and_warnings($json);
         }
       )->then(
         # Cache on success
         sub {
           my $json = shift;
           $c->chi->set($cache_str => $json);
+          $c->stash(api_response => $json);
           return $json;
         }
       );
diff --git a/lib/Kalamar/Plugin/KalamarUser.pm b/lib/Kalamar/Plugin/KalamarUser.pm
index 2f1a7a3..41251a1 100644
--- a/lib/Kalamar/Plugin/KalamarUser.pm
+++ b/lib/Kalamar/Plugin/KalamarUser.pm
@@ -41,6 +41,7 @@
 
       # Get token from stash
       my $token = $c->stash('auth');
+
       return $token if $token;
 
       # Get auth from session
@@ -48,6 +49,7 @@
 
       # Set token to stash
       $c->stash(auth => $auth);
+
       return $auth;
     }
   );
@@ -127,6 +129,7 @@
 
       my $tx;
       if ($c->user_auth) {
+
         $tx = $plugin->build_authorized_tx(
           $c->user_auth, $c->client_ip, uc($method), $path, @_
         );
diff --git a/t/remote_user.t b/t/remote_user.t
index ee0b872..baa8967 100644
--- a/t/remote_user.t
+++ b/t/remote_user.t
@@ -63,7 +63,11 @@
   ;
 
 
-$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass', csrf_token => $csrf })
+$t->post_ok('/user/login' => form => {
+  handle_or_email => 'test',
+  pwd => 'pass',
+  csrf_token => $csrf
+})
   ->status_is(302)
   ->header_is('Location' => '/');
 
@@ -84,7 +88,6 @@
   ->content_like(qr/\"authorized\"\:\"test\"/)
   ;
 
-
 # Logout
 $t->get_ok('/user/logout')
   ->status_is(302)