|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Authproc filter to generate a persistent NameID. |
| 4 | + * Authproc filter to generate a persistent NameID using the same algorithm as Shibboleth IdP does. |
5 | 5 | * |
6 | | - * @package simpleSAMLphp |
7 | 6 | * @version $Id$ |
8 | 7 | */ |
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 | + } |
103 | 97 | } |
0 commit comments