From 345d1260e3d85e1dfbb75fe415cbbaf317394d8f Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Tue, 1 Feb 2022 11:44:05 +0100
Subject: [PATCH] factorisation

---
 lib/AccountManager/App/Controller.pm | 41 ++++++++++++++++++++++++++++
 lib/AccountManager/App/Step2.pm      | 14 ++--------
 lib/AccountManager/App/Step3.pm      | 11 ++------
 lib/AccountManager/App/Step4.pm      | 13 ++-------
 4 files changed, 48 insertions(+), 31 deletions(-)

diff --git a/lib/AccountManager/App/Controller.pm b/lib/AccountManager/App/Controller.pm
index 6b40e97..82c5c11 100644
--- a/lib/AccountManager/App/Controller.pm
+++ b/lib/AccountManager/App/Controller.pm
@@ -6,6 +6,7 @@ use English qw(-no_match_vars);
 use Syntax::Keyword::Try;
 
 use AccountManager::DB;
+use AccountManager::Entity;
 use AccountManager::L10N;
 use AccountManager::Token;
 
@@ -116,6 +117,46 @@ sub check_token {
     return 1;
 }
 
+sub get_sp {
+    my ($self, %args) = @_;
+
+    my $entityid = $args{entityid};
+
+    return $self->abort(
+        log_message  => "Missing parameter: entityid",
+        user_message => "missing_entityid"
+    ) if !$entityid;
+
+    my $pattern = qr{
+        ^
+        (?:
+            https?://[\w.:/-]+
+        |
+            urn:[\w.:-]+
+        )
+        $
+    }x;
+
+    return $self->abort(
+        log_message  => "Incorrect parameter format: entityid",
+        user_message => "format_entityid"
+    ) if $entityid !~ $pattern;
+
+    my $db = $self->stash('db');
+
+    my $sp = AccountManager::Entity->new(
+        db       => $db,
+        entityid => $entityid
+    );
+
+    return $self->abort(
+        log_message  => sprintf("No such SP '%s' in database", $entityid),
+        user_message => "no_such_entity"
+    ) if !$sp->load(speculative => 1);
+
+    return $sp;
+}
+
 sub abort {
     my $self = shift;
     my %args = @_;
diff --git a/lib/AccountManager/App/Step2.pm b/lib/AccountManager/App/Step2.pm
index 78d7601..8fa231d 100644
--- a/lib/AccountManager/App/Step2.pm
+++ b/lib/AccountManager/App/Step2.pm
@@ -5,8 +5,6 @@ use Mojo::Base qw(AccountManager::App::Controller);
 use English qw(-no_match_vars);
 use Syntax::Keyword::Try;
 
-use AccountManager::Entity;
-
 sub run {
     my $self = shift;
 
@@ -21,16 +19,8 @@ sub run {
     }
 
     my $entityid = $self->param('entityid');
-    my $db       = $self->stash('db');
-
-    my $sp = AccountManager::Entity->new(
-        db       => $db,
-        entityid => $entityid
-    );
-    return $self->abort(
-        log_message  => sprintf("No such SP '%s' in database", $entityid),
-        user_message => "no_such_entity"
-    ) if !$sp->load(speculative => 1);
+    my $sp = $self->get_sp(entityid => $entityid);
+    return if !$sp;
 
     # override metadata contacts if needed
     my $contacts =
diff --git a/lib/AccountManager/App/Step3.pm b/lib/AccountManager/App/Step3.pm
index cbf2af7..c927ebb 100644
--- a/lib/AccountManager/App/Step3.pm
+++ b/lib/AccountManager/App/Step3.pm
@@ -9,7 +9,6 @@ use English qw(-no_match_vars);
 use Syntax::Keyword::Try;
 use Template::Constants qw(:chomp);
 
-use AccountManager::Entity;
 use AccountManager::Token;
 use AccountManager::Tools;
 
@@ -31,14 +30,8 @@ sub run {
     my $db       = $self->stash('db');
     my $l10n     = $self->stash('l10n');
 
-    my $sp = AccountManager::Entity->new(
-        db       => $db,
-        entityid => $entityid
-    );
-    return $self->abort(
-        log_message  => sprintf("No such SP '%s' in database", $entityid),
-        user_message => "no_such_entity"
-    ) if !$sp->load(speculative => 1);
+    my $sp = $self->get_sp(entityid => $entityid);
+    return if !$sp;
 
     # override metadata contacts if needed
     my $contacts =
diff --git a/lib/AccountManager/App/Step4.pm b/lib/AccountManager/App/Step4.pm
index bb68968..ed5ac33 100644
--- a/lib/AccountManager/App/Step4.pm
+++ b/lib/AccountManager/App/Step4.pm
@@ -10,7 +10,6 @@ use Syntax::Keyword::Try;
 use Template::Constants qw(:chomp);
 
 use AccountManager::Account;
-use AccountManager::Entity;
 use AccountManager::Token;
 use AccountManager::Tools;
 
@@ -35,16 +34,10 @@ sub run {
     my $db       = $self->stash('db');
     my $l10n     = $self->stash('l10n');
 
-    return if !$self->check_token(token => $token, entityid => $entityid);
+    my $sp = $self->get_sp(entityid => $entityid);
+    return if !$sp;
 
-    my $sp = AccountManager::Entity->new(
-        db       => $db,
-        entityid => $entityid,
-    );
-    return $self->abort(
-        log_message  => sprintf("No such SP '%s' in database", $entityid),
-        user_message => "no_such_entity"
-    ) if !$sp->load(speculative => 1);
+    return if !$self->check_token(token => $token, entityid => $entityid);
 
     ## create test accounts
     my @accounts;
-- 
GitLab