Skip to content

Commit 23c09dc

Browse files
author
Frank Tamás
committed
Format code to PSR-2
1 parent 79f46e9 commit 23c09dc

2 files changed

Lines changed: 149 additions & 157 deletions

File tree

Lines changed: 90 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,97 @@
11
<?php
22

33
/**
4-
* Authproc filter to generate a persistent NameID.
4+
* Authproc filter to generate a persistent NameID using the same algorithm as Shibboleth IdP does.
55
*
6-
* @package simpleSAMLphp
76
* @version $Id$
87
*/
9-
class sspmod_shib2idpnameid_Auth_Process_PersistentNameID extends sspmod_saml_BaseNameIDGenerator {
10-
11-
/**
12-
* Which attribute contains the unique identifier of the user.
13-
*
14-
* @var string
15-
*/
16-
private $attribute;
17-
18-
19-
/**
20-
* Initialize this filter, parse configuration.
21-
*
22-
* @param array $config Configuration information about this filter.
23-
* @param mixed $reserved For future use.
24-
*/
25-
public function __construct($config, $reserved) {
26-
parent::__construct($config, $reserved);
27-
assert('is_array($config)');
28-
29-
$this->format = SAML2_Const::NAMEID_PERSISTENT;
30-
31-
if (!isset($config['attribute'])) {
32-
throw new SimpleSAML_Error_Exception('PersistentNameID: Missing required option \'attribute\'.');
33-
}
34-
$this->attribute = $config['attribute'];
35-
}
36-
37-
38-
/**
39-
* Get the NameID value.
40-
*
41-
* @return string|NULL The NameID value.
42-
*/
43-
protected function getValue(array &$state) {
44-
45-
if (!isset($state['Destination']['entityid'])) {
46-
SimpleSAML_Logger::warning('No SP entity ID - not generating persistent NameID.');
47-
return NULL;
48-
}
49-
$spEntityId = $state['Destination']['entityid'];
50-
51-
if (!isset($state['Source']['entityid'])) {
52-
SimpleSAML_Logger::warning('No IdP entity ID - not generating persistent NameID.');
53-
return NULL;
54-
}
55-
$idpEntityId = $state['Source']['entityid'];
56-
57-
if (!isset($state['Attributes'][$this->attribute]) || count($state['Attributes'][$this->attribute]) === 0) {
58-
SimpleSAML_Logger::warning('Missing attribute ' . var_export($this->attribute, TRUE) . ' on user - not generating persistent NameID.');
59-
return NULL;
60-
}
61-
if (count($state['Attributes'][$this->attribute]) > 1) {
62-
SimpleSAML_Logger::warning('More than one value in attribute ' . var_export($this->attribute, TRUE) . ' on user - not generating persistent NameID.');
63-
return NULL;
64-
}
65-
$uid = array_values($state['Attributes'][$this->attribute]); /* Just in case the first index is no longer 0. */
66-
$uid = $uid[0];
67-
68-
$secretSalt = SimpleSAML_Utilities::getSecretSalt();
69-
70-
$uidData = $spEntityId . '!' . $uid . '!' . $secretSalt;
71-
#$uidData .= strlen($idpEntityId) . ':' . $idpEntityId;
72-
#$uidData .= strlen($spEntityId) . ':' . $spEntityId;
73-
#$uidData .= strlen($uid) . ':' . $uid;
74-
#$uidData .= $secretSalt;
75-
76-
$uid = base64_encode( hash ('sha1', $uidData, true ) );
77-
78-
//$uid = hash('sha1', $uidData);
79-
80-
/* Convert the targeted ID to a SAML 2.0 name identifier element. */
81-
$nameId = array(
82-
'Format' => SAML2_Const::NAMEID_PERSISTENT,
83-
'Value' => $uid,
84-
);
85-
86-
if (isset($state['Source']['entityid'])) {
87-
$nameId['NameQualifier'] = $state['Source']['entityid'];
88-
}
89-
if (isset($state['Destination']['entityid'])) {
90-
$nameId['SPNameQualifier'] = $state['Destination']['entityid'];
91-
}
92-
93-
$doc = new DOMDocument();
94-
$root = $doc->createElement('root');
95-
$doc->appendChild($root);
96-
97-
SAML2_Utils::addNameId($root, $nameId);
98-
$uid = $doc->saveXML($root->firstChild);
99-
100-
$state['Attributes']['eduPersonTargetedID'] = array($uid);
101-
}
102-
8+
class sspmod_shib2idpnameid_Auth_Process_PersistentNameID extends sspmod_saml_BaseNameIDGenerator
9+
{
10+
/**
11+
* Which attribute contains the unique identifier of the user.
12+
*
13+
* @var string
14+
*/
15+
private $attribute;
16+
17+
/**
18+
* Initialize this filter, parse configuration.
19+
*
20+
* @param array $config Configuration information about this filter.
21+
* @param mixed $reserved For future use.
22+
*/
23+
public function __construct($config, $reserved)
24+
{
25+
parent::__construct($config, $reserved);
26+
assert('is_array($config)');
27+
28+
$this->format = SAML2_Const::NAMEID_PERSISTENT;
29+
30+
if (!isset($config['attribute'])) {
31+
throw new SimpleSAML_Error_Exception('PersistentNameID: Missing required option \'attribute\'.');
32+
}
33+
$this->attribute = $config['attribute'];
34+
}
35+
36+
/**
37+
* Get the NameID value.
38+
*
39+
* @return string|NULL The NameID value.
40+
*/
41+
protected function getValue(array &$state)
42+
{
43+
if (!isset($state['Destination']['entityid'])) {
44+
SimpleSAML_Logger::warning('No SP entity ID - not generating persistent NameID.');
45+
46+
return;
47+
}
48+
$spEntityId = $state['Destination']['entityid'];
49+
50+
if (!isset($state['Source']['entityid'])) {
51+
SimpleSAML_Logger::warning('No IdP entity ID - not generating persistent NameID.');
52+
53+
return;
54+
}
55+
$idpEntityId = $state['Source']['entityid'];
56+
57+
if (!isset($state['Attributes'][$this->attribute]) || count($state['Attributes'][$this->attribute]) === 0) {
58+
SimpleSAML_Logger::warning('Missing attribute '.var_export($this->attribute, true).' on user - not generating persistent NameID.');
59+
60+
return;
61+
}
62+
if (count($state['Attributes'][$this->attribute]) > 1) {
63+
SimpleSAML_Logger::warning('More than one value in attribute '.var_export($this->attribute, true).' on user - not generating persistent NameID.');
64+
65+
return;
66+
}
67+
$uid = array_values($state['Attributes'][$this->attribute]); /* Just in case the first index is no longer 0. */
68+
$uid = $uid[0];
69+
70+
$secretSalt = SimpleSAML_Utilities::getSecretSalt();
71+
72+
$uidData = $spEntityId.'!'.$uid.'!'.$secretSalt;
73+
$uid = base64_encode(hash('sha1', $uidData, true));
74+
75+
// Convert the targeted ID to a SAML 2.0 name identifier element.
76+
$nameId = array(
77+
'Format' => SAML2_Const::NAMEID_PERSISTENT,
78+
'Value' => $uid,
79+
);
80+
81+
if (isset($state['Source']['entityid'])) {
82+
$nameId['NameQualifier'] = $state['Source']['entityid'];
83+
}
84+
if (isset($state['Destination']['entityid'])) {
85+
$nameId['SPNameQualifier'] = $state['Destination']['entityid'];
86+
}
87+
88+
$doc = new DOMDocument();
89+
$root = $doc->createElement('root');
90+
$doc->appendChild($root);
91+
92+
SAML2_Utils::addNameId($root, $nameId);
93+
$uid = $doc->saveXML($root->firstChild);
94+
95+
$state['Attributes']['eduPersonTargetedID'] = array($uid);
96+
}
10397
}

lib/Auth/Process/PersistentNameID2TargetedID.php

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,75 @@
33
/**
44
* Authproc filter to create the eduPersonTargetedID attribute from the persistent NameID.
55
*
6-
* @package simpleSAMLphp
76
* @version $Id$
87
*/
9-
class sspmod_saml_Auth_Process_PersistentNameID2TargetedID extends SimpleSAML_Auth_ProcessingFilter {
8+
class sspmod_saml_Auth_Process_PersistentNameID2TargetedID extends SimpleSAML_Auth_ProcessingFilter
9+
{
10+
/**
11+
* The attribute we should save the NameID in.
12+
*
13+
* @var string
14+
*/
15+
private $attribute;
1016

11-
/**
12-
* The attribute we should save the NameID in.
13-
*
14-
* @var string
15-
*/
16-
private $attribute;
17+
/**
18+
* Whether we should insert it as an saml:NameID element.
19+
*
20+
* @var bool
21+
*/
22+
private $nameId;
1723

24+
/**
25+
* Initialize this filter, parse configuration.
26+
*
27+
* @param array $config Configuration information about this filter.
28+
* @param mixed $reserved For future use.
29+
*/
30+
public function __construct($config, $reserved)
31+
{
32+
parent::__construct($config, $reserved);
33+
assert('is_array($config)');
1834

19-
/**
20-
* Whether we should insert it as an saml:NameID element.
21-
*
22-
* @var boolean
23-
*/
24-
private $nameId;
35+
if (isset($config['attribute'])) {
36+
$this->attribute = (string) $config['attribute'];
37+
} else {
38+
$this->attribute = 'eduPersonTargetedID';
39+
}
2540

41+
if (isset($config['nameId'])) {
42+
$this->nameId = (bool) $config['nameId'];
43+
} else {
44+
$this->nameId = true;
45+
}
46+
}
2647

27-
/**
28-
* Initialize this filter, parse configuration.
29-
*
30-
* @param array $config Configuration information about this filter.
31-
* @param mixed $reserved For future use.
32-
*/
33-
public function __construct($config, $reserved) {
34-
parent::__construct($config, $reserved);
35-
assert('is_array($config)');
48+
/**
49+
* Store a NameID to attribute.
50+
*
51+
* @param array &$state The request state.
52+
*/
53+
public function process(&$state)
54+
{
55+
assert('is_array($state)');
3656

37-
if (isset($config['attribute'])) {
38-
$this->attribute = (string)$config['attribute'];
39-
} else {
40-
$this->attribute = 'eduPersonTargetedID';
41-
}
57+
if (!isset($state['saml:NameID'][SAML2_Const::NAMEID_PERSISTENT])) {
58+
SimpleSAML_Logger::warning('Unable to generate eduPersonTargetedID because no persistent NameID was available.');
4259

43-
if (isset($config['nameId'])) {
44-
$this->nameId = (bool)$config['nameId'];
45-
} else {
46-
$this->nameId = TRUE;
47-
}
48-
}
60+
return;
61+
}
4962

63+
$nameID = $state['saml:NameID'][SAML2_Const::NAMEID_PERSISTENT];
5064

51-
/**
52-
* Store a NameID to attribute.
53-
*
54-
* @param array &$state The request state.
55-
*/
56-
public function process(&$state) {
57-
assert('is_array($state)');
58-
59-
if (!isset($state['saml:NameID'][SAML2_Const::NAMEID_PERSISTENT])) {
60-
SimpleSAML_Logger::warning('Unable to generate eduPersonTargetedID because no persistent NameID was available.');
61-
return;
62-
}
63-
64-
$nameID = $state['saml:NameID'][SAML2_Const::NAMEID_PERSISTENT];
65-
66-
if ($this->nameId) {
67-
$doc = new DOMDocument();
68-
$root = $doc->createElement('root');
69-
$doc->appendChild($root);
70-
SAML2_Utils::addNameId($root, $nameID);
71-
$value = $doc->saveXML($root->firstChild);
72-
} else {
73-
$value = $nameID['Value'];
74-
}
75-
76-
$state['Attributes'][$this->attribute] = array($value);
77-
}
65+
if ($this->nameId) {
66+
$doc = new DOMDocument();
67+
$root = $doc->createElement('root');
68+
$doc->appendChild($root);
69+
SAML2_Utils::addNameId($root, $nameID);
70+
$value = $doc->saveXML($root->firstChild);
71+
} else {
72+
$value = $nameID['Value'];
73+
}
7874

75+
$state['Attributes'][$this->attribute] = array($value);
76+
}
7977
}

0 commit comments

Comments
 (0)