Skip to content

Commit bd61e21

Browse files
committed
First commit
0 parents  commit bd61e21

File tree

13 files changed

+437
-0
lines changed

13 files changed

+437
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/build/
2+
composer.lock
3+
/vendor/

.php_cs.dist

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
$fileHeaderComment = <<<COMMENT
4+
This file is part of the simplesamlphp-module-authchain.
5+
6+
Copyright (C) 2018 by Sergio Gómez <sergio@uco.es>
7+
8+
This code was developed by Universidad de Córdoba (UCO https://www.uco.es)
9+
10+
For the full copyright and license information, please view the LICENSE
11+
file that was distributed with this source code.
12+
COMMENT;
13+
$finder = PhpCsFixer\Finder::create()
14+
->in(__DIR__)
15+
;
16+
return PhpCsFixer\Config::create()
17+
->setRiskyAllowed(true)
18+
->setRules([
19+
'@Symfony' => true,
20+
'@Symfony:risky' => true,
21+
'array_syntax' => ['syntax' => 'short'],
22+
'header_comment' => ['header' => $fileHeaderComment, 'separate' => 'both'],
23+
'linebreak_after_opening_tag' => true,
24+
'mb_str_functions' => true,
25+
'no_php4_constructor' => true,
26+
'no_unreachable_default_argument_value' => true,
27+
'no_useless_else' => true,
28+
'no_useless_return' => true,
29+
'ordered_imports' => true,
30+
'phpdoc_order' => true,
31+
'semicolon_after_instruction' => true,
32+
'strict_comparison' => true,
33+
'strict_param' => true,
34+
])
35+
->setFinder($finder)
36+
;

LICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
The MIT License (MIT)
3+
4+
Copyright (C) 2018 by Sergio Gómez <sergio@uco.es>
5+
6+
This code was developed by Universidad de Córdoba (UCO https://www.uco.es)
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SimpleSAMLphp Module UCOFilter
2+
3+
This module try to identify an user with multiple AuthSources in chain.
4+
5+
## Requirements
6+
7+
* PHP>=5.5
8+
9+
## Installation
10+
11+
Installation can be as easy as executing:
12+
13+
```bash
14+
bash$ composer require informaticauco/simplesamlphp-module-authchain
15+
```
16+
17+
## Usage
18+
19+
Edit `config/authsources.php` and add the next _authsource_:
20+
21+
```php
22+
<?php
23+
24+
use SimpleSAML\Modules\AuthChain\Auth\Source\AuthChain;
25+
26+
$config['as1'] = [/*...*/];
27+
$config['as2'] = [/*...*/];
28+
29+
$config['chained'] = [AuthChain::class,
30+
'sources' => ['as1', 'as2'],
31+
];
32+
```
33+
34+
_AuthSources_ defined in sources section must support `array function login(string $username, string $password)` method or will be ignored. The first AuthSource to identify the user will be used.

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "informaticauco/simplesamlphp-module-authchain",
3+
"description": "AuthSource Chain Authentication",
4+
"type": "simplesamlphp-module",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Sergio Gómez",
9+
"email": "sergio@uco.es"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"SimpleSAML\\Modules\\AuthChain\\": "lib"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"Tests\\SimpleSAML\\Modules\\AuthChain\\": "tests/"
20+
}
21+
},
22+
"require": {
23+
"php": ">=5.5.9",
24+
"simplesamlphp/composer-module-installer": "^1.0"
25+
},
26+
"require-dev": {
27+
"simplesamlphp/simplesamlphp": "^1.15",
28+
"phpunit/phpunit": "^7.4",
29+
"friendsofphp/php-cs-fixer": "^2.13",
30+
"roave/security-advisories": "dev-master"
31+
},
32+
"extra": {
33+
"branch-alias": {
34+
"dev-master": "1.0.x-dev"
35+
}
36+
}
37+
}

lib/Auth/Source/AuthChain.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the simplesamlphp-module-authchain.
5+
*
6+
* Copyright (C) 2018 by Sergio Gómez <sergio@uco.es>
7+
*
8+
* This code was developed by Universidad de Córdoba (UCO https://www.uco.es)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace SimpleSAML\Modules\AuthChain\Auth\Source;
15+
16+
use Webmozart\Assert\Assert;
17+
18+
class AuthChain extends \sspmod_core_Auth_UserPassBase
19+
{
20+
/**
21+
* @var array
22+
*/
23+
private $sources;
24+
25+
public function __construct(array $info, array $config)
26+
{
27+
parent::__construct($info, $config);
28+
29+
if (!array_key_exists('sources', $config)) {
30+
throw new \SimpleSAML_Error_Exception('The required "sources" config option was not found');
31+
}
32+
33+
$this->sources = $config['sources'];
34+
}
35+
36+
protected function login($username, $password)
37+
{
38+
Assert::string($username, 'username must be a string');
39+
Assert::string($password, 'password must be a string');
40+
41+
$lastError = false;
42+
43+
foreach ($this->sources as $authId) {
44+
$as = \SimpleSAML_Auth_Source::getById($authId);
45+
46+
if (null === $as) {
47+
throw new \SimpleSAML_Error_Exception("Invalid authentication source: $authId");
48+
}
49+
50+
if (!method_exists($as, 'login')) {
51+
\SimpleSAML\Logger::error('Could not use {$authId}, trying next');
52+
continue;
53+
}
54+
55+
try {
56+
return $as->login($username, $password);
57+
} catch (\SimpleSAML_Error_AuthSource $e) {
58+
\SimpleSAML\Logger::error("Could not connect to {$authId}, trying next");
59+
} catch (\SimpleSAML_Error_Error $e) {
60+
if ('WRONGUSERPASS' === $e->getErrorCode()) {
61+
\SimpleSAML\Logger::debug('Failed one source, trying next');
62+
} else {
63+
$lastError = $e;
64+
}
65+
}
66+
}
67+
68+
if ($lastError) {
69+
throw $lastError;
70+
}
71+
72+
throw new \SimpleSAML_Error_Error('WRONGUSERPASS');
73+
}
74+
}

phpunit.xml.dist

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ This file is part of the simplesamlphp-module-oidc
4+
~
5+
~ (c) Sergio Gómez <sergio@uco.es>
6+
~
7+
~ For the full copyright and license information, please view the LICENSE
8+
~ file that was distributed with this source code.
9+
-->
10+
11+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
12+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.4/phpunit.xsd"
13+
backupGlobals="false"
14+
colors="true"
15+
bootstrap="vendor/autoload.php"
16+
failOnRisky="true"
17+
failOnWarning="true"
18+
>
19+
<php>
20+
<ini name="error_reporting" value="-1" />
21+
</php>
22+
23+
<testsuites>
24+
<testsuite name="AuthChain SimpleSAMLphp Module Test Suite">
25+
<directory>./tests/</directory>
26+
</testsuite>
27+
</testsuites>
28+
29+
<filter>
30+
<whitelist>
31+
<directory>./lib</directory>
32+
</whitelist>
33+
</filter>
34+
35+
<logging>
36+
<log type="coverage-php" target="build/cov/phpunit.cov"/>
37+
</logging>
38+
</phpunit>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the simplesamlphp-module-authchain.
5+
*
6+
* Copyright (C) 2018 by Sergio Gómez <sergio@uco.es>
7+
*
8+
* This code was developed by Universidad de Córdoba (UCO https://www.uco.es)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\SimpleSAML\Modules\AuthChain\Auth\Source;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use SimpleSAML\Modules\AuthChain\Auth\Source\AuthChain;
18+
19+
class AuthChainTest extends TestCase
20+
{
21+
/**
22+
* @test
23+
*/
24+
public function it_does_chained_login()
25+
{
26+
\SimpleSAML_Configuration::setConfigDir(__DIR__.'/../../fixtures/config');
27+
28+
$authChain = new AuthChain([
29+
'AuthId' => 'chained',
30+
], [
31+
'sources' => ['dummy-as', 'success-as'],
32+
]);
33+
34+
$login = function ($username, $password) {
35+
return $this->login($username, $password);
36+
};
37+
$bindedAuthChain = $login->bindTo($authChain, $authChain);
38+
39+
$this->assertArraySubset(['uid' => ['username']], $bindedAuthChain('username', 'password'));
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function it_tries_all_auth_sources()
46+
{
47+
\SimpleSAML_Configuration::setConfigDir(__DIR__.'/../../fixtures/config');
48+
49+
$authChain = new AuthChain([
50+
'AuthId' => 'chained',
51+
], [
52+
'sources' => ['failure-as', 'success-as'],
53+
]);
54+
55+
$login = function ($username, $password) {
56+
return $this->login($username, $password);
57+
};
58+
$bindedAuthChain = $login->bindTo($authChain, $authChain);
59+
60+
$this->assertArraySubset(['uid' => ['username']], $bindedAuthChain('username', 'password'));
61+
}
62+
63+
/**
64+
* @test
65+
* @expectedException \SimpleSAML_Error_Error
66+
* @expectedExceptionMessage WRONGUSERPASS
67+
*/
68+
public function it_launch_exception_if_all_auth_sources_fail()
69+
{
70+
\SimpleSAML_Configuration::setConfigDir(__DIR__.'/../../fixtures/config');
71+
72+
$authChain = new AuthChain([
73+
'AuthId' => 'chained',
74+
], [
75+
'sources' => ['failure-as', 'failure-as'],
76+
]);
77+
78+
$login = function ($username, $password) {
79+
return $this->login($username, $password);
80+
};
81+
$bindedAuthChain = $login->bindTo($authChain, $authChain);
82+
$bindedAuthChain('username', 'password');
83+
}
84+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the simplesamlphp-module-authchain.
5+
*
6+
* Copyright (C) 2018 by Sergio Gómez <sergio@uco.es>
7+
*
8+
* This code was developed by Universidad de Córdoba (UCO https://www.uco.es)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\SimpleSAML\Modules\AuthChain\fixtures\Source;
15+
16+
class DummyAuthSource extends \SimpleSAML_Auth_Source
17+
{
18+
public function authenticate(&$state)
19+
{
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the simplesamlphp-module-authchain.
5+
*
6+
* Copyright (C) 2018 by Sergio Gómez <sergio@uco.es>
7+
*
8+
* This code was developed by Universidad de Córdoba (UCO https://www.uco.es)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\SimpleSAML\Modules\AuthChain\fixtures\Source;
15+
16+
class FailureAuthSource extends \sspmod_core_Auth_UserPassBase
17+
{
18+
protected function login($username, $password)
19+
{
20+
throw new \SimpleSAML_Error_Error('WRONGUSERPASS');
21+
}
22+
}

0 commit comments

Comments
 (0)