From 4eb4f73f0329e0e0c0e1f7a06a987237b0c92b84 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Mon, 28 Jan 2019 10:50:33 +0100
Subject: [PATCH] use source_ip instead of user name when there is no
 authentication

---
 lib/AccountManager/App.pm                         | 4 ++++
 lib/AccountManager/L10N/fr.pm                     | 5 +++++
 templates/mail/send_authentication_token.tt2.html | 4 ++++
 templates/mail/send_authentication_token.tt2.txt  | 4 ++++
 4 files changed, 17 insertions(+)

diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm
index 4678a58..5983c42 100644
--- a/lib/AccountManager/App.pm
+++ b/lib/AccountManager/App.pm
@@ -413,6 +413,9 @@ sub req_complete_challenge {
         PRE_CHOMP    => CHOMP_ONE,
         INCLUDE_PATH => $templates_dir
     });
+    my $source_ip = $ENV{HTTP_X_FORWARDED_FOR} ?
+        (split(/, /, $ENV{HTTP_X_FORWARDED_FOR}))[0] :
+        $ENV{REMOTE_ADDR};
     my $user =
         $ENV{'HTTP_DISPLAYNAME'} ? $ENV{'HTTP_DISPLAYNAME'} :
         $ENV{'displayName'}      ? $ENV{'displayName'}      :
@@ -429,6 +432,7 @@ sub req_complete_challenge {
             name          => $self->{configuration}->{app}->{name},
         },
         user      => $user,
+        source_ip => $source_ip,
         idp       => {
             entityid => $idp,
         },
diff --git a/lib/AccountManager/L10N/fr.pm b/lib/AccountManager/L10N/fr.pm
index 4d440b9..bc6ef89 100644
--- a/lib/AccountManager/L10N/fr.pm
+++ b/lib/AccountManager/L10N/fr.pm
@@ -182,6 +182,11 @@ msgstr "Sélectionnez votre adresse e-mail"
 msgid "Select your service provider"
 msgstr "Sélectionnez votre fournisseur de service"
 
+#. (source_ip, sp.entityid)
+#: templates/mail/send_authentication_token.tt2.html:6 templates/mail/send_authentication_token.tt2.txt:5
+msgid "Somebody has requested creation of test accounts for service provider %1, from IP address %2."
+msgstr "Quelqu'un a demandé la création de comptes de test pour le fournisseur de service %1, depuis l'adresse IP %2."
+
 #: templates/web/edugain/create_accounts.tt2.html:8 templates/web/renater/create_accounts.tt2.html:10
 msgid "Test accounts created"
 msgstr "Comptes de test créés"
diff --git a/templates/mail/send_authentication_token.tt2.html b/templates/mail/send_authentication_token.tt2.html
index 1114551..e29f23c 100644
--- a/templates/mail/send_authentication_token.tt2.html
+++ b/templates/mail/send_authentication_token.tt2.html
@@ -1,6 +1,10 @@
 <p>
     [% lh.maketext("This is an email challenge automatically sent to you by [_1].", app.name) %] 
+[% IF user && idp %]
     [% lh.maketext("User [_1], authenticated by Identity Provider [_2], has requested creation of test accounts for Service Provider [_3].", user, idp.entityid, sp.entityid) %]
+[% ELSE %]
+    [% lh.maketext("Somebody has requested creation of test accounts for service provider [_1], from IP address [_2].", sp.entityid, source_ip) %]
+[% END %]
     [% lh.maketext("The address [_1] is mentioned in the federation metadata as a contact for this Service Provider.", to) %]
 </p>
 
diff --git a/templates/mail/send_authentication_token.tt2.txt b/templates/mail/send_authentication_token.tt2.txt
index 953d62b..bda58be 100644
--- a/templates/mail/send_authentication_token.tt2.txt
+++ b/templates/mail/send_authentication_token.tt2.txt
@@ -1,5 +1,9 @@
 [% lh.maketext("This is an email challenge automatically sent to you by [_1].", app.name) %]
+[% IF user && idp %]
 [% lh.maketext("User [_1], authenticated by Identity Provider [_2], has requested creation of test accounts for Service Provider [_3].", user, idp.entityid, sp.entityid) %] 
+[% ELSE %]
+[% lh.maketext("Somebody has requested creation of test accounts for service provider [_1], from IP address [_2].", sp.entityid, source_ip) %]
+[% END %]
 [% lh.maketext("The address [_1] is mentioned in the federation metadata as a contact for this Service Provider.", to) %]
 
 [% lh.maketext("To complete the creation of test accounts, paste the following validation token in [_1]:", app.name) %]
-- 
GitLab