ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
HTTPArtifact.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SAML2;
4 
11 
20 class HTTPArtifact extends Binding
21 {
25  private $spMetadata;
26 
34  public function getRedirectURL(Message $message)
35  {
36  $store = SimpleSAML_Store::getInstance();
37  if ($store === false) {
38  throw new \Exception('Unable to send artifact without a datastore configured.');
39  }
40 
41  $generatedId = pack('H*', ((string) SimpleSAML_Utilities::stringToHex(SimpleSAML_Utilities::generateRandomBytes(20))));
42  $artifact = base64_encode("\x00\x04\x00\x00" . sha1($message->getIssuer(), true) . $generatedId) ;
43  $artifactData = $message->toUnsignedXML();
44  $artifactDataString = $artifactData->ownerDocument->saveXML($artifactData);
45 
46  $store->set('artifact', $artifact, $artifactDataString, Temporal::getTime() + 15*60);
47 
48  $params = array(
49  'SAMLart' => $artifact,
50  );
51  $relayState = $message->getRelayState();
52  if ($relayState !== null) {
53  $params['RelayState'] = $relayState;
54  }
55 
57  }
58 
66  public function send(Message $message)
67  {
68  $destination = $this->getRedirectURL($message);
69  Utils::getContainer()->redirect($destination);
70  }
71 
80  public function receive()
81  {
82  if (array_key_exists('SAMLart', $_REQUEST)) {
83  $artifact = base64_decode($_REQUEST['SAMLart']);
84  $endpointIndex = bin2hex(substr($artifact, 2, 2));
85  $sourceId = bin2hex(substr($artifact, 4, 20));
86  } else {
87  throw new \Exception('Missing SAMLart parameter.');
88  }
89 
91 
92  $idpMetadata = $metadataHandler->getMetaDataConfigForSha1($sourceId, 'saml20-idp-remote');
93 
94  if ($idpMetadata === null) {
95  throw new \Exception('No metadata found for remote provider with SHA1 ID: ' . var_export($sourceId, true));
96  }
97 
98  $endpoint = null;
99  foreach ($idpMetadata->getEndpoints('ArtifactResolutionService') as $ep) {
100  if ($ep['index'] === hexdec($endpointIndex)) {
101  $endpoint = $ep;
102  break;
103  }
104  }
105 
106  if ($endpoint === null) {
107  throw new \Exception('No ArtifactResolutionService with the correct index.');
108  }
109 
110  Utils::getContainer()->getLogger()->debug("ArtifactResolutionService endpoint being used is := " . $endpoint['Location']);
111 
112  //Construct the ArtifactResolve Request
113  $ar = new ArtifactResolve();
114 
115  /* Set the request attributes */
116 
117  $ar->setIssuer($this->spMetadata->getString('entityid'));
118  $ar->setArtifact($_REQUEST['SAMLart']);
119  $ar->setDestination($endpoint['Location']);
120 
121  /* Sign the request */
122  \sspmod_saml_Message::addSign($this->spMetadata, $idpMetadata, $ar); // Shoaib - moved from the SOAPClient.
123 
124  $soap = new SOAPClient();
125 
126  // Send message through SoapClient
128  $artifactResponse = $soap->send($ar, $this->spMetadata);
129 
130  if (!$artifactResponse->isSuccess()) {
131  throw new \Exception('Received error from ArtifactResolutionService.');
132  }
133 
134  $xml = $artifactResponse->getAny();
135  if ($xml === null) {
136  /* Empty ArtifactResponse - possibly because of Artifact replay? */
137 
138  return null;
139  }
140 
141  $samlResponse = Message::fromXML($xml);
142  $samlResponse->addValidator(array(get_class($this), 'validateSignature'), $artifactResponse);
143 
144  if (isset($_REQUEST['RelayState'])) {
145  $samlResponse->setRelayState($_REQUEST['RelayState']);
146  }
147 
148  return $samlResponse;
149  }
150 
155  {
156  $this->spMetadata = $sp;
157  }
158 
167  {
168  return $message->validate($key);
169  }
170 }
$params
Definition: disable.php:11
static getMetadataHandler()
This function retrieves the current instance of the metadata handler.
getRelayState()
Retrieve the RelayState associated with this message.
Definition: Message.php:411
$endpoint
$destination
getIssuer()
Retrieve the issuer if this message.
Definition: Message.php:375
if(! $oauthconfig->getBoolean('getUserInfo.enable', FALSE)) $store
Definition: getUserInfo.php:11
Base class for all SAML 2 messages.
Definition: Message.php:18
$xml
Definition: metadata.php:240
catch(Exception $e) $message
if(!array_key_exists(sspmod_authfacebook_Auth_Source_Facebook::AUTHID, $state)) $sourceId
Definition: linkback.php:20
getRedirectURL(Message $message)
Create the redirect URL for a message.
static generateRandomBytes($length)
Definition: Utilities.php:359
$relayState
static validateSignature(ArtifactResponse $message, XMLSecurityKey $key)
A validator which returns true if the ArtifactResponse was signed with the given key.
setSPMetadata(SimpleSAML_Configuration $sp)
static addSign(SimpleSAML_Configuration $srcMetadata, SimpleSAML_Configuration $dstMetadata, \SAML2\SignedElement $element)
Add signature key and sender certificate to an element (Message or Assertion).
Definition: Message.php:20
Create styles array
The data for the language used.
validate(XMLSecurityKey $key)
Validate this message against a public key.
Definition: Message.php:248
toUnsignedXML()
Convert this message to an unsigned XML document.
Definition: Message.php:435
static stringToHex($bytes)
Definition: Utilities.php:370
send(Message $message)
Send a SAML 2 message using the HTTP-Redirect binding.
$idpMetadata
getDestination()
Retrieve the destination of this message.
Definition: Message.php:323
$key
Definition: croninfo.php:18
static addURLparameter($url, $parameters)
Definition: Utilities.php:99