|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2018 SURFnet bv |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/* Installation: copy this file to the "modules/saml/lib/Auth/Source/" directory of your SimpleSAMLphp installation |
| 20 | + Usage: |
| 21 | + - In authsourcesphp use "DebugSP:SP" where you would otherwise use "saml:SP" |
| 22 | + - In the call to AuthSimple::requireAuth($params), AuthSimple::login($params) set 'saml:AssertionConsumerServiceURL' |
| 23 | + and 'DebugSP:extraPOSTvars' to the desired values. |
| 24 | + E.g.: |
| 25 | + $params=array( |
| 26 | + 'DebugSP:AssertionConsumerServiceURL' => 'https://...', |
| 27 | + 'DebugSP:extraPOSTvars' => array( |
| 28 | + 'SomePOSTvariable' => 'SomeValue', |
| 29 | + 'AnotherPOSTvariable' => 'AnotherValue' |
| 30 | + ), |
| 31 | + ); |
| 32 | + $as->login($params); |
| 33 | +*/ |
| 34 | + |
| 35 | +// Extend from the SimpleSAMLphp SAML 2.0 authentication source "saml:SP" |
| 36 | +class sspmod_DebugSP_Auth_Source_SP extends sspmod_saml_Auth_Source_SP { |
| 37 | + |
| 38 | + public function __construct($info, $config) { |
| 39 | + parent::__construct($info, $config); |
| 40 | + } |
| 41 | + |
| 42 | + public function sendSAML2AuthnRequest(array &$state, \SAML2\Binding $binding, \SAML2\AuthnRequest $ar) { |
| 43 | + |
| 44 | + if ( isset( $state['DebugSP:AssertionConsumerServiceURL'] ) ) { |
| 45 | + // Set the AssertionConsumerServiceURL in the AuthnRequest |
| 46 | + $ar->setAssertionConsumerServiceURL( $state['DebugSP:AssertionConsumerServiceURL'] ); |
| 47 | + } |
| 48 | + |
| 49 | + if ($binding instanceof \SAML2\HTTPPost) { |
| 50 | + // replicate \SAML2\HTTPPost::send(Message $message) so we can set additional POST variables |
| 51 | + $destination = $ar->getDestination(); |
| 52 | + $relayState = $ar->getRelayState(); |
| 53 | + $post = array(); |
| 54 | + |
| 55 | + // Set extra POST variables |
| 56 | + if (isset($state['DebugSP:extraPOSTvars'])) { |
| 57 | + assert(is_array($state['DebugSP:extraPOSTvars']), 'DebugSP:extraPOSTvars must be array()'); |
| 58 | + foreach ($state['DebugSP:extraPOSTvars'] as $key => $value) { |
| 59 | + $post[$key] = $value; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Create SAMLRequest |
| 64 | + $msgStr = $ar->toSignedXML(); |
| 65 | + $msgStr = $msgStr->ownerDocument->saveXML($msgStr); |
| 66 | + |
| 67 | + \SAML2\Utils::getContainer()->debugMessage($msgStr, 'out'); |
| 68 | + |
| 69 | + $post['SAMLRequest'] = base64_encode($msgStr); |
| 70 | + |
| 71 | + if ($relayState !== null) { |
| 72 | + $post['RelayState'] = $relayState; |
| 73 | + } |
| 74 | + |
| 75 | + \SAML2\Utils::getContainer()->postRedirect($destination, $post); |
| 76 | + |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Use partent implementation |
| 81 | + parent::sendSAML2AuthnRequest($state, $binding, $ar); |
| 82 | + } |
| 83 | +} |
0 commit comments