Skip to content

Commit a75ce2b

Browse files
author
Dani Pfeil
committed
add new feature simplesamlphp
1 parent 7b5aa4c commit a75ce2b

9 files changed

+3492
-358
lines changed

Classes/Enum/ServiceProviderType.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
class ServiceProviderType
2323
{
2424
const APACHE_SHIBBOLETH = 1;
25+
const SIMPLESAMLPHP = 2;
2526
}
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
4+
namespace DanielPfeil\Samlauthentication\Simplesamlphp;
5+
6+
7+
class ServiceProvider implements \DanielPfeil\ServiceProviderAuthenticator\ServiceProvider
8+
{
9+
public function getApplicationID(): ?string
10+
{
11+
// TODO: Implement getApplicationID() method.
12+
}
13+
14+
public function getSessionId(): ?string
15+
{
16+
// TODO: Implement getSessionId() method.
17+
}
18+
19+
public function getIdentityProvider(): ?string
20+
{
21+
// TODO: Implement getIdentityProvider() method.
22+
}
23+
24+
public function getAuthenticationInstant(): ?string
25+
{
26+
// TODO: Implement getAuthenticationInstant() method.
27+
}
28+
29+
public function getAuthenticationMethod(): ?string
30+
{
31+
// TODO: Implement getAuthenticationMethod() method.
32+
}
33+
34+
public function getAuthenticationContextClass(): ?string
35+
{
36+
// TODO: Implement getAuthenticationContextClass() method.
37+
}
38+
39+
public function getSessionIndex(): ?string
40+
{
41+
// TODO: Implement getSessionIndex() method.
42+
}
43+
44+
public function getPrefix(): ?string
45+
{
46+
// TODO: Implement getPrefix() method.
47+
}
48+
49+
public function getCookieName(): ?string
50+
{
51+
// TODO: Implement getCookieName() method.
52+
}
53+
54+
public function isSessionExisting(): bool
55+
{
56+
// TODO: Implement isSessionExisting() method.
57+
}
58+
59+
public function getField(string $fieldName): ?string
60+
{
61+
// TODO: Implement getField() method.
62+
}
63+
64+
public function getPrefixedField(string $fieldName, bool $useShortPrefix = true): ?string
65+
{
66+
// TODO: Implement getPrefixedField() method.
67+
}
68+
69+
}

Classes/Utility/FactoryUtility.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public static function getSAMLUtility(Serviceprovider $serviceprovider): ?SamlUt
4343
if ($serviceprovider->getType() === ServiceProviderType::APACHE_SHIBBOLETH) {
4444
//apache shibd
4545
return new ApacheSamlUtility();
46+
} else if(ServiceProviderType::SIMPLESAMLPHP) {
47+
return new SimpleSAMLphpUtility();
4648
}
4749
return null;
4850
}
@@ -188,7 +190,7 @@ private static function getFieldMappingObjectStorageForTableMapping(Tablemapping
188190
->from('tx_samlauthentication_domain_model_fieldmapping')
189191
->execute()->fetchAll();
190192

191-
foreach ($fieldMappingsArray as $key => $value) {
193+
foreach ($fieldMappingsArray as $value) {
192194
$fieldMapping = self::getFieldMappingFromArray($value);
193195
$objectStorage->attach($fieldMapping);
194196
}

Classes/Utility/ServiceProviderUtility.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
use DanielPfeil\Samlauthentication\Domain\Model\Serviceprovider;
2323
use DanielPfeil\Samlauthentication\Enum\ServiceProviderType;
24+
use SimpleSAML\Session;
25+
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
2426

2527
class ServiceProviderUtility
2628
{
@@ -64,10 +66,15 @@ final public function getActive(array $serviceProviders): array
6466

6567
final private function getIdp(String $prefix, int $type): ?String
6668
{
67-
$index = $prefix . "Shib-Identity-Provider";
6869
if ($type === ServiceProviderType::APACHE_SHIBBOLETH && isset($_SERVER[$index])) {
70+
$index = $prefix . "Shib-Identity-Provider";
6971
return $_SERVER[$index];
7072
}
73+
else if($type === ServiceProviderType::SIMPLESAMLPHP){
74+
$as = new \SimpleSAML\Auth\Simple('default-sp');
75+
$as->requireAuth();
76+
return $as->getAuthDataArray()["saml:sp:IdP"];
77+
}
7178
return null;
7279
}
7380
}
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
4+
namespace DanielPfeil\Samlauthentication\Utility;
5+
6+
7+
use DanielPfeil\Samlauthentication\Domain\Model\Fieldmapping;
8+
use DanielPfeil\Samlauthentication\Domain\Model\FieldValue;
9+
use DanielPfeil\Samlauthentication\Domain\Model\Serviceprovider;
10+
use DanielPfeil\Samlauthentication\Domain\Model\Tablemapping;
11+
use TYPO3\CMS\Core\Database\ConnectionPool;
12+
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
13+
use TYPO3\CMS\Core\Utility\GeneralUtility;
14+
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
15+
16+
class SimpleSAMLphpUtility implements SamlUtility
17+
{
18+
public function getData(): array
19+
{
20+
// TODO: Implement getData() method.
21+
}
22+
23+
public function isSessionExisting(): bool
24+
{
25+
// TODO: Implement isSessionExisting() method.
26+
}
27+
28+
public function getUserData(Serviceprovider $serviceprovider): array
29+
{
30+
$result = [];
31+
32+
/**
33+
* @var $tablemapping Tablemapping
34+
*/
35+
foreach ($serviceprovider->getTablemapping() as $tablemapping) {
36+
$result[$tablemapping->getTable()] = $this->getDataForTableMapping(
37+
$tablemapping,
38+
$serviceprovider->getPrefix()
39+
);
40+
}
41+
42+
return $result;
43+
}
44+
45+
public function saveUserData(Serviceprovider $serviceprovider): bool
46+
{
47+
$tableMappings = $serviceprovider->getTablemapping();
48+
49+
/**
50+
* @var Tablemapping $tableMapping
51+
*/
52+
foreach ($tableMappings as $tableMapping) {
53+
$data = $this->getDataForTableMapping($tableMapping, $serviceprovider->getPrefix());
54+
55+
/**
56+
* @var QueryBuilder $queryBuilderFeUsers
57+
*/
58+
$queryBuilderFeUsers = GeneralUtility::makeInstance(ConnectionPool::class)
59+
->getQueryBuilderForTable($tableMapping->getTable());
60+
61+
$values = [
62+
"tstamp" => time(),
63+
"pid" => $serviceprovider->getDestinationpid(),
64+
];
65+
66+
foreach ($data as $field) {
67+
if ($field->getValue() != null) {
68+
$index = $field->getField();
69+
$values[$index] = $field->getValue();
70+
}
71+
}
72+
73+
$findUser = $queryBuilderFeUsers
74+
->count('*')
75+
->from($tableMapping->getTable());
76+
/**
77+
* @var Fieldmapping $field
78+
*/
79+
foreach ($tableMapping->getFields() as $field) {
80+
if ($field->isIdentifier()) {
81+
$predicate = $queryBuilderFeUsers->expr()->eq(
82+
$field->getField(),
83+
$queryBuilderFeUsers->createNamedParameter($values[$field->getField()])
84+
);
85+
86+
$findUser->andWhere($predicate);
87+
}
88+
}
89+
90+
$userExists = $findUser->execute()->fetch()['COUNT(*)'];
91+
if ($userExists === 0) {
92+
$result = $queryBuilderFeUsers->insert($tableMapping->getTable())
93+
->values($values)
94+
->execute();
95+
} else {
96+
//todo implement update
97+
}
98+
}
99+
//todo make check
100+
return true;
101+
}
102+
103+
public function getGroup(): array
104+
{
105+
// TODO: Implement getGroup() method.
106+
}
107+
108+
public function getUserGroups($user)
109+
{
110+
// TODO: Implement getUserGroups() method.
111+
}
112+
113+
private function getDataForTableMapping(Tablemapping $tablemapping, ?string $prefix): array
114+
{
115+
$as = new \SimpleSAML\Auth\Simple('default-sp');
116+
$as->requireAuth();
117+
$attributes = $as->getAttributes();
118+
119+
$result = [];
120+
121+
foreach ($tablemapping->getFields() as $field) {
122+
$fieldValue = new FieldValue();
123+
$fieldValue->setField($field->getField());
124+
$fieldValue->setForeignField($field->getForeignField());
125+
126+
$key = $prefix . $fieldValue->getForeignField();
127+
if (array_key_exists($key, $attributes)) {
128+
$value = $attributes[$key];
129+
if (is_array($value))
130+
$value = $value[0];
131+
132+
$fieldValue->setValue($value);
133+
} else {
134+
if ($field->hasFallback()) {
135+
$fieldValue->setValue($field->getDefaultvalue());
136+
}
137+
}
138+
139+
$result[$field->getField()] = $fieldValue;
140+
}
141+
142+
return $result;
143+
}
144+
}

Configuration/TCA/tx_samlauthentication_domain_model_serviceprovider.php

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
'renderType' => 'selectSingle',
6767
'items' => [
6868
['Apache2 Shibboleth SP', 1],
69+
['SimpleSAMLphp SP', 2],
6970
],
7071
'minitems' => 1,
7172
'maxitems' => 1,

composer.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@
1010
"serviceprovider"
1111
],
1212
"license": "GPL-3.0-only",
13-
"version": "3.0.1",
13+
"version": "3.1.1",
1414
"require": {
1515
"typo3/minimal": ">=9",
16-
"daniel-pfeil/serviceprovider-apache-shib": "^1.1.0"
16+
"daniel-pfeil/serviceprovider-apache-shib": "^1.1.0",
17+
"simplesamlphp/simplesamlphp": "^1.19"
1718
},
1819
"require-dev": {
1920
"squizlabs/php_codesniffer": "*",
2021
"phpspec/phpspec": "*",
2122
"leanphp/phpspec-code-coverage": "*"
2223
},
24+
"extra": {
25+
"typo3/cms": {
26+
"extension-key": "introduction"
27+
}
28+
},
2329
"autoload": {
2430
"psr-4": {
2531
"DanielPfeil\\Samlauthentication\\": "Classes/"

0 commit comments

Comments
 (0)