From a5e53a5d8ea19b15f77713575783b31ce1e05af0 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Tue, 7 Nov 2017 15:16:54 +0100
Subject: [PATCH] replace contacts on the fly if needed

---
 lib/IdPAccountManager/WebRequest.pm | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/lib/IdPAccountManager/WebRequest.pm b/lib/IdPAccountManager/WebRequest.pm
index 220bcd5..e01e110 100755
--- a/lib/IdPAccountManager/WebRequest.pm
+++ b/lib/IdPAccountManager/WebRequest.pm
@@ -270,7 +270,7 @@ sub req_select_sp {
         if (!@$sps) {
             push @{ $self->{out}->{errors} }, "no_such_entity";
             $self->{logger}->errorf(
-                "No such entity %s in metadata", $self->{in}->{sp_entityid}
+                "No such SP '%s' in metadata", $self->{in}->{sp_entityid}
             );
             return undef;
         }
@@ -319,22 +319,26 @@ sub req_generate_token {
         return undef;
     }
 
-    ## Create a serviceprovider object to load parameters for this SP from DB
-    my $service_provider = IdPAccountManager::Data::ServiceProvider->new(
-        db             => $self->{db},
-        entityid       => $self->{in}->{sp_entityid},
-        dev_sp_contact => $self->{configuration}->{dev_sp_contact}
+    my $provider = IdPAccountManager::Data::ServiceProvider->new(
+        db       => $self->{db},
+        entityid => $self->{in}->{sp_entityid},
     );
 
-    # Try loading DB object first
-    unless ($service_provider->load(speculative => 1)) {
-        push @{ $self->{out}->{errors} }, "internal";
-        $self->{logger}->errorf("Failed to load SP with entityid '%s'", $self->{in}->{sp_entityid});
+    unless ($provider->load(speculative => 1)) {
+        push @{ $self->{out}->{errors} }, "no_such_entity";
+        $self->{logger}->errorf("No such SP '%s' in database", $self->{in}->{sp_entityid});
         return undef;
     }
 
+    if ($self->{configuration}->{dev_sp_contact}) {
+        # replace SP contacts
+        $provider->contacts(
+            split(/, */, $self->{configuration}->{dev_sp_contact})
+        );
+    }
+
     ## Check that email_address is a known contact for this SP
-    unless ($service_provider->is_contact($self->{in}->{email_address}))
+    unless ($provider->is_contact($self->{in}->{email_address}))
     {
         push @{ $self->{out}->{errors} }, "internal";
         $self->{logger}->errorf(
-- 
GitLab