1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\saml;
use SAML2\XML\saml\NameID;
use SimpleSAML\Logger;
/**
* Base filter for generating NameID values.
*
* @package SimpleSAMLphp
*/
abstract class BaseNameIDGenerator extends \SimpleSAML\Auth\ProcessingFilter
{
/**
* What NameQualifier should be used.
* Can be one of:
* - a string: The qualifier to use.
* - FALSE: Do not include a NameQualifier. This is the default.
* - TRUE: Use the IdP entity ID.
*
* @var string|bool
*/
private $nameQualifier;
/**
* What SPNameQualifier should be used.
* Can be one of:
* - a string: The qualifier to use.
* - FALSE: Do not include a SPNameQualifier.
* - TRUE: Use the SP entity ID. This is the default.
*
* @var string|bool
*/
private $spNameQualifier;
/**
* The format of this NameID.
*
* This property must be set by the subclass.
*
* @var string|null
*/
protected $format = null;
/**
* Initialize this filter, parse configuration.
*
* @param array $config Configuration information about this filter.
* @param mixed $reserved For future use.
*/
public function __construct($config, $reserved)
{
parent::__construct($config, $reserved);
assert(is_array($config));
if (isset($config['NameQualifier'])) {
$this->nameQualifier = $config['NameQualifier'];
} else {
$this->nameQualifier = false;
}
if (isset($config['SPNameQualifier'])) {
$this->spNameQualifier = $config['SPNameQualifier'];
} else {
$this->spNameQualifier = true;
}
}
/**
* Get the NameID value.
*
* @return string|null The NameID value.
*/
abstract protected function getValue(array &$state);
/**
* Generate transient NameID.
*
* @param array &$state The request state.
* @return void
*/
public function process(&$state)
{
assert(is_array($state));
assert(is_string($this->format));
$value = $this->getValue($state);
if ($value === null) {
return;
}
$nameId = new NameID();
$nameId->setValue($value);
$nameId->setFormat($this->format);
if ($this->nameQualifier === true) {
if (isset($state['IdPMetadata']['entityid'])) {
$nameId->setNameQualifier($state['IdPMetadata']['entityid']);
} else {
Logger::warning('No IdP entity ID, unable to set NameQualifier.');
}
} elseif (is_string($this->nameQualifier)) {
$nameId->setNameQualifier($this->nameQualifier);
}
if ($this->spNameQualifier === true) {
if (isset($state['SPMetadata']['entityid'])) {
$nameId->setSPNameQualifier($state['SPMetadata']['entityid']);
} else {
Logger::warning('No SP entity ID, unable to set SPNameQualifier.');
}
} elseif (is_string($this->spNameQualifier)) {
$nameId->setSPNameQualifier($this->spNameQualifier);
}
$state['saml:NameID'][$this->format] = $nameId;
}
}
|