44
55namespace SimpleSAML \Module \saml \Auth \Process ;
66
7- use SimpleSAML \{Auth , Logger };
7+ use SimpleSAML \{Auth , Logger , Utils };
88use SimpleSAML \Assert \Assert ;
99use SimpleSAML \SAML2 \Constants as C ;
1010use SimpleSAML \SAML2 \Exception \ProtocolViolationException ;
1111
1212use function array_key_exists ;
1313use function explode ;
14+ use function hash_hmac ;
1415use function preg_match ;
1516use function strpos ;
1617use function strtolower ;
@@ -85,6 +86,18 @@ class SubjectID extends Auth\ProcessingFilter
8586 */
8687 protected string $ scopeAttribute ;
8788
89+ /**
90+ * Whether the unique part of the subject id must be hashed
91+ *
92+ * @var bool
93+ */
94+ private bool $ hashed = false ;
95+
96+ /**
97+ * @var \SimpleSAML\Utils\Config
98+ */
99+ protected Utils \Config $ configUtils ;
100+
88101 /**
89102 * @var \SimpleSAML\Logger|string
90103 * @psalm-var \SimpleSAML\Logger|class-string
@@ -109,6 +122,13 @@ public function __construct(array &$config, $reserved)
109122
110123 $ this ->identifyingAttribute = $ config ['identifyingAttribute ' ];
111124 $ this ->scopeAttribute = $ config ['scopeAttribute ' ];
125+
126+ if (array_key_exists ('hashed ' , $ config )) {
127+ Assert::boolean ($ config ['hashed ' ]);
128+ $ this ->hashed = $ config ['hashed ' ];
129+ }
130+
131+ $ this ->configUtils = new Utils \Config ();
112132 }
113133
114134
@@ -127,7 +147,12 @@ public function process(array &$state): void
127147 return ;
128148 }
129149
130- $ value = strtolower ($ userID . '@ ' . $ scope );
150+ if ($ this ->hashed === true ) {
151+ $ value = strtolower ($ this ->calculateHash ($ userID ) . '@ ' . $ scope );
152+ } else {
153+ $ value = strtolower ($ userID . '@ ' . $ scope );
154+ }
155+
131156 $ this ->validateGeneratedIdentifier ($ value );
132157
133158 $ state ['Attributes ' ][C::ATTR_SUBJECT_ID ] = [$ value ];
@@ -232,6 +257,16 @@ protected function validateGeneratedIdentifier(string $value): void
232257 }
233258
234259
260+ /**
261+ * Calculate the hash for the unique part of the identifier.
262+ */
263+ protected function calculateHash (string $ input ): string
264+ {
265+ $ salt = $ this ->configUtils ->getSecretSalt ();
266+ return hash_hmac ('sha256 ' , $ input , $ salt , false );
267+ }
268+
269+
235270 /**
236271 * Inject the \SimpleSAML\Logger dependency.
237272 *
@@ -241,4 +276,15 @@ public function setLogger(Logger $logger): void
241276 {
242277 $ this ->logger = $ logger ;
243278 }
279+
280+
281+ /**
282+ * Inject the \SimpleSAML\Utils\Config dependency.
283+ *
284+ * @param \SimpleSAML\Utils\Config $configUtils
285+ */
286+ public function setConfigUtils (Utils \Config $ configUtils ): void
287+ {
288+ $ this ->configUtils = $ configUtils ;
289+ }
244290}
0 commit comments