summaryrefslogtreecommitdiffstats
path: root/modules/saml/lib/BaseNameIDGenerator.php
blob: 24d6d209d26bb9e36d448c0dc383cc7eb8d8b5b7 (plain)
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
<?php

/**
 * Base filter for generating NameID values.
 *
 * @package SimpleSAMLphp
 */
abstract class sspmod_saml_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 initialized the subclass.
	 *
	 * @var string
	 */
	protected $format;


	/**
	 * 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.
	 */
	public function process(&$state) {
		assert('is_array($state)');
		assert('is_string($this->format)');

		$value = $this->getValue($state);
		if ($value === NULL) {
			return;
		}

		$nameId = array('Value' => $value);

		if ($this->nameQualifier === TRUE) {
			if (isset($state['IdPMetadata']['entityid'])) {
				$nameId['NameQualifier'] = $state['IdPMetadata']['entityid'];
			} else {
				SimpleSAML\Logger::warning('No IdP entity ID, unable to set NameQualifier.');
			}
		} elseif (is_string($this->nameQualifier)) {
			$nameId['NameQualifier'] = $this->nameQualifier;
		}

		if ($this->spNameQualifier === TRUE) {
			if (isset($state['SPMetadata']['entityid'])) {
				$nameId['SPNameQualifier'] = $state['SPMetadata']['entityid'];
			} else {
				SimpleSAML\Logger::warning('No SP entity ID, unable to set SPNameQualifier.');
			}
		} elseif (is_string($this->spNameQualifier)) {
			$nameId['SPNameQualifier'] = $this->spNameQualifier;
		}

		$state['saml:NameID'][$this->format] = $nameId;
	}

}