Marketplace: List plugins

Change-Id: I3bd0d0f1db8224cbfe7efd255eb99d7cd54faf11
diff --git a/lib/Kalamar/Plugin/Auth.pm b/lib/Kalamar/Plugin/Auth.pm
index 80422ab..5df0568 100644
--- a/lib/Kalamar/Plugin/Auth.pm
+++ b/lib/Kalamar/Plugin/Auth.pm
@@ -111,6 +111,11 @@
           registerSuccess => 'Registrierung erfolgreich',
           registerFail => 'Registrierung fehlgeschlagen',
           oauthSettings => 'OAuth',
+          #for marketplace settings
+          marketplace => 'Marktplatz',
+          mp_regby => "Registriert von",
+          mp_regdate => "Registrierungsdatum",
+
           oauthUnregister => {
             -long => 'Möchten sie <span class="client-name"><%= $client_name %></span> wirklich löschen?',
             short => 'Löschen'
@@ -170,6 +175,10 @@
           registerSuccess => 'Registration successful',
           registerFail => 'Registration denied',
           oauthSettings => 'OAuth',
+          #for marketplace settings
+          marketplace => 'Marketplace',
+          mp_regby =>"Registered by",
+          mp_regdate =>"Registration date",
           oauthUnregister => {
             -long => 'Do you really want to unregister <span class="client-name"><%= $client_name %></span>?',
             short => 'Unregister'
@@ -400,11 +409,13 @@
     }
   );
 
+
   # Get a list of registered clients
   $app->helper(
     'auth.client_list_p' => sub {
       my $c = shift;
 
+
       # Get list of registered clients
       state $r_url = Mojo::URL->new($c->korap->api)->path('oauth2/client/list');
 
@@ -854,6 +865,8 @@
   )->name('logout');
 
 
+
+
   # If "experimental_registration" is set, open
   # OAuth registration dialogues.
   if ($param->{experimental_client_registration}) {
@@ -861,7 +874,111 @@
     # Add settings
     $app->navi->add(settings => (
       $app->loc('Auth_oauthSettings'), 'oauth'
-    ));
+    )
+    );
+    #$app->navi->add(settings => (
+    #  $app->loc('Auth_marketplace'), 'marketplace'
+    #));
+
+
+    # Lists all permitted registered plugins
+    $app->helper(
+    'auth.plugin_list_m' => sub {
+
+      my $c = shift;
+      state $r_url = Mojo::URL->new($c->korap->api)->path('plugins');
+      return $c->korap_request(post => $r_url, {} => form => {
+        super_client_id => $client_id,
+        super_client_secret => $client_secret,
+        #list only permitted plugins
+        permitted_only => 'true'
+      })->then( 
+          sub {
+          my $tx = shift;
+          my $json = $tx->result->json;
+
+          # Response is fine
+          if ($tx->res->is_success) {
+            return Mojo::Promise->resolve($json);
+          };
+
+          $c->log->error($c->dumper($tx->res->to_string));
+
+          # Failure
+          $c->notify(error => $c->loc('Auth_responseError'));
+          return Mojo::Promise->reject($json // 'No response');
+        }
+      );
+     }
+   );
+
+    # Route to marketplace settings
+    $r->get('/settings/marketplace')->to(
+      cb => sub {
+      my $c = shift;
+      _set_no_cache($c->res->headers);
+      
+      unless ($c->auth->token) {
+      #TODO: Handle authorization (forward to Login for example)
+        return $c->render(
+            template => 'exception',
+            msg => $c->loc('Auth_authenticationFail'),
+            status => 401
+          );
+        };
+
+      $c->render_later;
+      $c->auth->plugin_list_m->then(
+        sub {
+          $c->stash('plugin_list' => shift);
+          }
+        )->catch(
+         sub {
+            return;
+          }
+       )->finally(
+         sub {
+           return $c->render(template => 'auth/marketplace');
+         }
+       );
+      }
+     )->name('marketplace');
+
+
+
+    # Route to OAuth settings
+    $r->get('/settings/oauth')->to(
+      cb => sub {
+        my $c = shift;
+
+        _set_no_cache($c->res->headers);
+
+        unless ($c->auth->token) {
+          return $c->render(
+            template => 'exception',
+            msg => $c->loc('Auth_authenticationFail'),
+            status => 401
+          );
+        };
+
+        # Wait for async result
+        $c->render_later;
+
+        $c->auth->client_list_p->then(
+          sub {
+            $c->stash('client_list' => shift);
+          }
+        )->catch(
+          sub {
+            return;
+          }
+        )->finally(
+          sub {
+            return $c->render(template => 'auth/clients')
+          }
+        );
+      }
+    )->name('oauth-settings');
 
     # Route to oauth settings
     $r->get('/settings/oauth')->to(
diff --git a/lib/Kalamar/Plugin/Auth/templates/auth/marketplace.html.ep b/lib/Kalamar/Plugin/Auth/templates/auth/marketplace.html.ep
new file mode 100644
index 0000000..75c1252
--- /dev/null
+++ b/lib/Kalamar/Plugin/Auth/templates/auth/marketplace.html.ep
@@ -0,0 +1,27 @@
+% extends 'settings', title => loc('Auth_marketplace'), page => 'oauth';
+
+%= page_title
+
+
+% my $plugins = stash('plugin_list');
+
+% if ($plugins) {
+<ul class="plugin-list">
+  %foreach (@$plugins) {
+    <li class="plugin">
+      <span class="client-name client-type-<%= lc($_->{'client_type'} // 'PUBLIC') %>"><%=$_->{client_name} %></span>
+      %if ($_->{client_description}) {
+        <p class="plugin-desc"><%= $_->{client_description} %></p>
+        % };
+      %if ($_->{client_url}) {
+        <p class="plugin-url"><a href="<%= $_->{client_url} %>"><%= $_->{client_url} %></a></p>
+         %}
+      %if ($_->{registration_date}) {
+        <p class="registration_date"> <%=loc('Auth_mp_regdate')%>: <%= $_->{registration_date} %></p>
+        %};
+      %if ($_->{registered_by}) {
+        <p class="registered_by"> <%=loc('Auth_mp_regby')%>: <%= $_->{registered_by} %></p>
+        %};
+      %}
+  </ul>
+  %};
\ No newline at end of file
diff --git a/t/plugin/auth-oauth.t b/t/plugin/auth-oauth.t
index 3e5b78c..9deec0e 100644
--- a/t/plugin/auth-oauth.t
+++ b/t/plugin/auth-oauth.t
@@ -126,6 +126,11 @@
   ->text_is('p.no-results', 'Not authenticated')
   ;
 
+$t->get_ok('/settings/marketplace')
+  ->status_is(401)
+  ->text_is('p.no-results', 'Not authenticated')
+  ;
+
 # Test for bug with long password
 $t->post_ok('/user/login' => form => {
   handle_or_email => 'test',
@@ -491,6 +496,12 @@
   ->header_is('Pragma','no-cache')
   ;
 
+
+$t->get_ok('/settings/marketplace')
+  ->status_is(200)
+  ->text_is('html head title' => 'Marketplace')
+  ;
+
 $csrf = $t->post_ok('/settings/oauth/register' => form => {
   name => 'MyApp',
   type => 'PUBLIC',
@@ -843,6 +854,7 @@
 #  "client_redirect_uri" => $redirect_uri
 });
 
+
 $fake_backend_app->add_client({
   "client_id" => 'xyz2',
   "client_name" => 'New added client',
@@ -878,6 +890,21 @@
   ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
   ;
 
+
+$fake_backend_app->add_plugin({
+"source" => {"key1" => 'wert1', "key2" => 'wert2'},
+"client_id" => "52abc",
+"permitted" => 'true',
+"client_id" => '52abc',
+"client_name" => 'Plugin 1',
+"client_type" => 'CONFIDENTIAL',
+"client_description" =>"Description Plugin 1",
+"client_url" => "http://example.client.de",
+"registration_date" => "2022-05-31T14:30:09+02:00[Europe/Berlin]",
+"registered_by" => "system"
+});
+
+
 $fwd = $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
   client_id => 'xyz',
   state => 'abcde',
@@ -923,6 +950,49 @@
   ->element_exists_not('blockquote.warning')
   ;
 
+$t->get_ok('/settings/marketplace')
+  ->status_is(200)
+  ->text_is('html head title' => 'Marketplace')
+  ->element_exists('ul.plugin-list')
+  ->element_exists('ul.plugin-list > li')
+  ->element_exists('p.registration_date')
+  ->element_exists('p.registered_by')
+  ->text_is('span.client-name','Plugin 1')
+  ->text_is('p.plugin-desc','Description Plugin 1')
+  ;
+
+$fake_backend_app->add_plugin({
+"source" => {"one" => '1', "two" => '2'},
+"permitted" => 'false',
+"client_id" => '53abc',
+"client_name" => 'Plugin 2',
+"client_type" => 'CONFIDENTIAL',
+"client_description" =>'Description Plugin 2'
+});
+
+$fake_backend_app->add_plugin({
+"source" => {"answer" => '42', "hello" => 'world'},
+"permitted" => 'true',
+"client_id" => '54abc',
+"client_name" => 'Plugin 3',
+"client_type" => 'CONFIDENTIAL',
+"client_description" =>'Description Plugin 3'
+});
+
+$t->get_ok('/settings/marketplace')
+  ->status_is(200)
+  ->element_exists('ul.plugin-list')
+  ->element_exists('ul.plugin-list > li')
+  ->text_is('span.client-name','Plugin 1')
+  ->text_is('p.plugin-desc','Description Plugin 1')
+  ->element_exists('ul.plugin-list > li + li')
+  ->text_isnt('ul.plugin-list > li + li >span.client-name','Plugin 2')
+  ->text_isnt('ul.plugin-list > li + li >p.plugin-desc','Description Plugin 2')
+  ->text_is('ul.plugin-list > li + li >span.client-name','Plugin 3')
+  ->text_is('ul.plugin-list > li + li >p.plugin-desc','Description Plugin 3')
+  ;
+
+
 $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
   client_id => 'xyz',
   state => 'abcde',
diff --git a/t/server/mock.pl b/t/server/mock.pl
index e615306..af03832 100644
--- a/t/server/mock.pl
+++ b/t/server/mock.pl
@@ -57,6 +57,13 @@
   push @$list, $client;
 };
 
+# Add plugin to plugin list for marketplace
+helper 'add_plugin' => sub {
+  my $c = shift;
+  my $cplugin = shift;
+  my $pl_list = $c->app->defaults('oauth.plugin_list');
+  push @$pl_list, $cplugin;
+};
 
 # Load fixture responses
 helper 'load_response' => sub {
@@ -89,6 +96,7 @@
 };
 
 app->defaults('oauth.client_list' => []);
+app->defaults('oauth.plugin_list' => []);
 
 
 # Base page
@@ -552,9 +560,8 @@
   });
 };
 
-
-# Register a client
-post '/v1.0/oauth2/client/list' => sub {
+# List plugins
+post '/v1.0/plugins' => sub {
   my $c = shift;
 
   my $v = $c->validation;
@@ -568,6 +575,48 @@
       status => 400
     );
   };
+  
+  my $p;
+  if($c->param("permitted_only")){
+    $p = $c->param("permitted_only");
+  }
+  else{
+    $p="false";
+  }
+
+  #Mocks the return only of permitted plugins
+  if($p eq "true"){
+    my @p_plugin_list = grep{$_->{permitted} eq "true"}  @{$c->stash('oauth.plugin_list')};
+    my $listref = \@p_plugin_list;
+    return $c->render(
+      json => $listref,
+      status => 200
+    );
+  }
+  else{
+  return $c->render(
+   json => $c->stash('oauth.plugin_list'),
+    status => 200
+  );
+  }
+
+};
+
+# Register a client
+post '/v1.0/oauth2/client/list' => sub {
+  my $c = shift;
+
+  my $v = $c->validation;
+  $v->required('super_client_id');
+  $v->required('super_client_secret');
+
+  if ($v->has_error) {
+    return $c->render(
+      json => [],
+      status => 400
+    );
+  };
+
 
   # $c->param('client_secret');