@@ -161,6 +161,9 @@ class Auth
161161 * Initializes the SP SAML instance.
162162 *
163163 * @param array|null $settings Setting data
164+ *
165+ * @throws Exception
166+ * @throws Error
164167 */
165168 public function __construct (array $ settings = null )
166169 {
@@ -186,7 +189,7 @@ public function getSettings()
186189 */
187190 public function setStrict ($ value )
188191 {
189- if (! ( is_bool ($ value) )) {
192+ if (!is_bool ($ value )) {
190193 throw new Error (
191194 'Invalid value passed to setStrict() ' ,
192195 Error::SETTINGS_INVALID_SYNTAX
@@ -202,12 +205,13 @@ public function setStrict($value)
202205 * @param string|null $requestId The ID of the AuthNRequest sent by this SP to the IdP
203206 *
204207 * @throws Error
208+ * @throws ValidationError
205209 */
206210 public function processResponse ($ requestId = null )
207211 {
208212 $ this ->_errors = array ();
209213 $ this ->_lastError = $ this ->_lastErrorException = null ;
210- if (isset ($ _POST ) && isset ( $ _POST ['SAMLResponse ' ])) {
214+ if (isset ($ _POST ['SAMLResponse ' ])) {
211215 // AuthnResponse -- HTTP_POST Binding
212216 $ response = new Response ($ this ->_settings , $ _POST ['SAMLResponse ' ]);
213217 $ this ->_lastResponse = $ response ->getXMLDocument ();
@@ -255,7 +259,7 @@ public function processSLO($keepLocalSession = false, $requestId = null, $retrie
255259 {
256260 $ this ->_errors = array ();
257261 $ this ->_lastError = $ this ->_lastErrorException = null ;
258- if (isset ($ _GET ) && isset ( $ _GET ['SAMLResponse ' ])) {
262+ if (isset ($ _GET ['SAMLResponse ' ])) {
259263 $ logoutResponse = new LogoutResponse ($ this ->_settings , $ _GET ['SAMLResponse ' ]);
260264 $ this ->_lastResponse = $ logoutResponse ->getXML ();
261265 if (!$ logoutResponse ->isValid ($ requestId , $ retrieveParametersFromServer )) {
@@ -275,7 +279,7 @@ public function processSLO($keepLocalSession = false, $requestId = null, $retrie
275279 }
276280 }
277281 }
278- } else if (isset ($ _GET ) && isset ( $ _GET ['SAMLRequest ' ])) {
282+ } else if (isset ($ _GET ['SAMLRequest ' ])) {
279283 $ logoutRequest = new LogoutRequest ($ this ->_settings , $ _GET ['SAMLRequest ' ]);
280284 $ this ->_lastRequest = $ logoutRequest ->getXML ();
281285 if (!$ logoutRequest ->isValid ($ retrieveParametersFromServer )) {
@@ -500,6 +504,8 @@ public function getAttributeWithFriendlyName($friendlyName)
500504 * @param bool $setNameIdPolicy When true the AuthNRequest will set a nameIdPolicy element
501505 *
502506 * @return string|null If $stay is True, it return a string with the SLO URL + LogoutRequest + parameters
507+ *
508+ * @throws Error
503509 */
504510 public function login ($ returnTo = null , array $ parameters = array (), $ forceAuthn = false , $ isPassive = false , $ stay = false , $ setNameIdPolicy = true )
505511 {
@@ -632,33 +638,7 @@ public function getLastRequestID()
632638 */
633639 public function buildRequestSignature ($ samlRequest , $ relayState , $ signAlgorithm = XMLSecurityKey::RSA_SHA256 )
634640 {
635- $ key = $ this ->_settings ->getSPkey ();
636- if (empty ($ key )) {
637- throw new Error (
638- "Trying to sign the SAML Request but can't load the SP private key " ,
639- Error::PRIVATE_KEY_NOT_FOUND
640- );
641- }
642-
643- $ objKey = new XMLSecurityKey ($ signAlgorithm , array ('type ' => 'private ' ));
644- $ objKey ->loadKey ($ key , false );
645-
646- $ security = $ this ->_settings ->getSecurityData ();
647- if ($ security ['lowercaseUrlencoding ' ]) {
648- $ msg = 'SAMLRequest= ' .rawurlencode ($ samlRequest );
649- if (isset ($ relayState )) {
650- $ msg .= '&RelayState= ' .rawurlencode ($ relayState );
651- }
652- $ msg .= '&SigAlg= ' . rawurlencode ($ signAlgorithm );
653- } else {
654- $ msg = 'SAMLRequest= ' .urlencode ($ samlRequest );
655- if (isset ($ relayState )) {
656- $ msg .= '&RelayState= ' .urlencode ($ relayState );
657- }
658- $ msg .= '&SigAlg= ' . urlencode ($ signAlgorithm );
659- }
660- $ signature = $ objKey ->signData ($ msg );
661- return base64_encode ($ signature );
641+ return $ this ->buildMessageSignature ($ samlRequest , $ relayState , $ signAlgorithm , "SAMLRequest " );
662642 }
663643
664644 /**
@@ -674,27 +654,48 @@ public function buildRequestSignature($samlRequest, $relayState, $signAlgorithm
674654 * @throws Error
675655 */
676656 public function buildResponseSignature ($ samlResponse , $ relayState , $ signAlgorithm = XMLSecurityKey::RSA_SHA256 )
657+ {
658+ return $ this ->buildMessageSignature ($ samlResponse , $ relayState , $ signAlgorithm , "SAMLResponse " );
659+ }
660+
661+ /**
662+ * Generates the Signature for a SAML Message
663+ *
664+ * @param string $samlMessage The SAML Message
665+ * @param string $relayState The RelayState
666+ * @param string $signAlgorithm Signature algorithm method
667+ * @param string $type "SAMLRequest" or "SAMLResponse"
668+ *
669+ * @return string A base64 encoded signature
670+ *
671+ * @throws Exception
672+ * @throws Error
673+ */
674+ private function buildMessageSignature ($ samlMessage , $ relayState , $ signAlgorithm = XMLSecurityKey::RSA_SHA256 , $ type ="SAMLRequest " )
677675 {
678676 $ key = $ this ->_settings ->getSPkey ();
679677 if (empty ($ key )) {
680- throw new Error (
681- "Trying to sign the SAML Response but can't load the SP private key " ,
682- Error::PRIVATE_KEY_NOT_FOUND
683- );
678+ if ($ type == "SAMLRequest " ) {
679+ $ errorMsg = "Trying to sign the SAML Request but can't load the SP private key " ;
680+ } else {
681+ $ errorMsg = "Trying to sign the SAML Response but can't load the SP private key " ;
682+ }
683+
684+ throw new Error ($ errorMsg , Error::PRIVATE_KEY_NOT_FOUND );
684685 }
685686
686687 $ objKey = new XMLSecurityKey ($ signAlgorithm , array ('type ' => 'private ' ));
687688 $ objKey ->loadKey ($ key , false );
688689
689690 $ security = $ this ->_settings ->getSecurityData ();
690691 if ($ security ['lowercaseUrlencoding ' ]) {
691- $ msg = ' SAMLResponse = ' .rawurlencode ($ samlResponse );
692+ $ msg = $ type . ' = ' .rawurlencode ($ samlMessage );
692693 if (isset ($ relayState )) {
693694 $ msg .= '&RelayState= ' .rawurlencode ($ relayState );
694695 }
695696 $ msg .= '&SigAlg= ' . rawurlencode ($ signAlgorithm );
696697 } else {
697- $ msg = ' SAMLResponse = ' .urlencode ($ samlResponse );
698+ $ msg = $ type . ' = ' .urlencode ($ samlMessage );
698699 if (isset ($ relayState )) {
699700 $ msg .= '&RelayState= ' .urlencode ($ relayState );
700701 }
0 commit comments