Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward the source IP as value for "client" in sendRequest() #45

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/PrivacyIDEA.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class PrivacyIDEA
/* @var string Realm for a service account to the privacyIDEA server. This is required to use the /validate/triggerchallenge endpoint. This is optional. */
public $serviceAccountRealm = "";

/* @var bool Send the "client" parameter to allow using the original IP address in the privacyIDEA policies. */
public $forwardClientIP = false;

/* @var object Implementation of the PILog interface. */
public $logger = null;

Expand Down Expand Up @@ -422,6 +425,22 @@ public function sendRequest(array $params, array $headers, $httpMethod, $endpoin
assert('string' === gettype($httpMethod));
assert('string' === gettype($endpoint));

// Add the client parameter if wished.
if ($this->forwardClientIP === true)
{
$serverHeaders = $_SERVER;
foreach (array("X-Forwarded-For", "HTTP_X_FORWARDED_FOR", "REMOTE_ADDR") as $clientKey)
{
if (array_key_exists($clientKey, $serverHeaders))
{
$clientIP = $serverHeaders[$clientKey];
$this->debugLog("Forwarding Client IP: " . $clientKey . ": " . $clientIP);
$params['client'] = $clientIP;
break;
}
}
}

$this->debugLog("Sending " . http_build_query($params, '', ', ') . " to " . $endpoint);

$completeUrl = $this->serverURL . $endpoint;
Expand Down
Loading