From d159033842227003e97e5c4b49dede5676d46081 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Wed, 11 Mar 2020 13:38:50 +0100
Subject: [PATCH] factorisation

---
 lib/AccountManager/App.pm | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm
index e7a30ee..35f8b07 100644
--- a/lib/AccountManager/App.pm
+++ b/lib/AccountManager/App.pm
@@ -403,9 +403,7 @@ 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 $source_ip = get_source_address();
     my $user =
         $ENV{'HTTP_DISPLAYNAME'} ? $ENV{'HTTP_DISPLAYNAME'} :
         $ENV{'displayName'}      ? $ENV{'displayName'}      :
@@ -709,9 +707,7 @@ sub req_status {
     my ($self) = @_;
 
     Net::IP->require();
-    my $source_ip_string = $ENV{HTTP_X_FORWARDED_FOR} ?
-        (split(/, /, $ENV{HTTP_X_FORWARDED_FOR}))[0] :
-        $ENV{REMOTE_ADDR};
+    my $source_ip_string = get_source_address();
     my $source_ip = Net::IP->new($source_ip_string);
     my @allowed_ips_strings = $self->{configuration}->{status}->{allow} ?
         split(/, */, $self->{configuration}->{status}->{allow}) : ();
@@ -821,4 +817,10 @@ sub check_authentication {
     ) if !$ENV{'HTTP_DISPLAYNAME'};
 }
 
+sub get_source_address {
+    return $ENV{HTTP_X_FORWARDED_FOR} ?
+        (split(/, /, $ENV{HTTP_X_FORWARDED_FOR}))[0] :
+        $ENV{REMOTE_ADDR};
+}
+
 1;
-- 
GitLab