Skip to content

Commit

Permalink
activate usage of proxy vars if symfony detects a trusted one
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Jan 30, 2020
1 parent b2868f9 commit 04223e0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
7 changes: 5 additions & 2 deletions config/services-saml.yaml
Expand Up @@ -5,12 +5,15 @@ services:
# SAML
# ================================================================================

App\Saml\SamlAuth:
alias: onelogin_auth

OneLogin\Saml2\Auth:
alias: onelogin_auth

onelogin_auth:
class: OneLogin\Saml2\Auth
arguments: ['%kimai.saml.connection%']
class: App\Saml\SamlAuth
arguments: ['@request_stack', '%kimai.saml.connection%']

App\Saml\User\SamlUserFactory:
arguments: ['%kimai.saml%']
Expand Down
6 changes: 3 additions & 3 deletions src/Saml/Controller/SamlController.php
Expand Up @@ -9,7 +9,7 @@

namespace App\Saml\Controller;

use OneLogin\Saml2\Auth;
use App\Saml\SamlAuth;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -22,11 +22,11 @@
final class SamlController extends AbstractController
{
/**
* @var Auth
* @var SamlAuth
*/
private $oneLoginAuth;

public function __construct(Auth $oneLoginAuth)
public function __construct(SamlAuth $oneLoginAuth)
{
$this->oneLoginAuth = $oneLoginAuth;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Saml/Logout/SamlLogoutHandler.php
Expand Up @@ -9,8 +9,8 @@

namespace App\Saml\Logout;

use App\Saml\SamlAuth;
use Hslavich\OneloginSamlBundle\Security\Authentication\Token\SamlTokenInterface;
use OneLogin\Saml2\Auth;
use OneLogin\Saml2\Error;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -20,11 +20,11 @@
final class SamlLogoutHandler implements LogoutHandlerInterface
{
/**
* @var Auth
* @var SamlAuth
*/
private $samlAuth;

public function __construct(Auth $samlAuth)
public function __construct(SamlAuth $samlAuth)
{
$this->samlAuth = $samlAuth;
}
Expand Down
26 changes: 26 additions & 0 deletions src/Saml/SamlAuth.php
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Saml;

use OneLogin\Saml2\Auth;
use OneLogin\Saml2\Utils;
use Symfony\Component\HttpFoundation\RequestStack;

class SamlAuth extends Auth
{
public function __construct(RequestStack $request, array $settings = null)
{
parent::__construct($settings);

if (null !== $request->getMasterRequest() && $request->getMasterRequest()->isFromTrustedProxy()) {
Utils::setProxyVars(true);
}
}
}
11 changes: 8 additions & 3 deletions tests/Mocks/Saml/SamlAuthFactory.php
Expand Up @@ -9,12 +9,14 @@

namespace App\Tests\Mocks\Saml;

use App\Saml\SamlAuth;
use App\Tests\Mocks\AbstractMockFactory;
use OneLogin\Saml2\Auth;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class SamlAuthFactory extends AbstractMockFactory
{
public function create(?array $connection = null): Auth
public function create(?array $connection = null): SamlAuth
{
if (null === $connection) {
$connection = [
Expand Down Expand Up @@ -74,6 +76,9 @@ public function create(?array $connection = null): Auth
];
}

return new Auth($connection);
$requestStack = new RequestStack();
$requestStack->push(new Request());

return new SamlAuth($requestStack, $connection);
}
}
6 changes: 3 additions & 3 deletions tests/Saml/Logout/SamlLogoutHandlerTest.php
Expand Up @@ -11,8 +11,8 @@

use App\Entity\User;
use App\Saml\Logout\SamlLogoutHandler;
use App\Saml\SamlAuth;
use Hslavich\OneloginSamlBundle\Security\Authentication\Token\SamlToken;
use OneLogin\Saml2\Auth;
use OneLogin\Saml2\Error;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -25,7 +25,7 @@ class SamlLogoutHandlerTest extends TestCase
{
public function testLogout()
{
$auth = $this->getMockBuilder(Auth::class)->disableOriginalConstructor()->getMock();
$auth = $this->getMockBuilder(SamlAuth::class)->disableOriginalConstructor()->getMock();
$auth->expects($this->once())->method('processSLO')->willThrowException(new Error('blub'));
$auth->expects($this->once())->method('getSLOurl')->willReturn('');

Expand All @@ -39,7 +39,7 @@ public function testLogout()

public function testLogoutWithLogoutUrl()
{
$auth = $this->getMockBuilder(Auth::class)->disableOriginalConstructor()->getMock();
$auth = $this->getMockBuilder(SamlAuth::class)->disableOriginalConstructor()->getMock();
$auth->expects($this->once())->method('processSLO')->willThrowException(new Error('blub'));
$auth->expects($this->once())->method('getSLOurl')->willReturn('/logout');
$auth->expects($this->once())->method('logout')->willReturnCallback(function () {
Expand Down

0 comments on commit 04223e0

Please sign in to comment.