setNameId() public method

The NameId must be in the format accepted by \SAML2\Utils::addNameId().
See also: SAML2\Utils::addNameId()
public setNameId ( array | null $nameId )
$nameId array | null The name identifier of the assertion.
 public static function handleLoginRequest(IPerson $Person)
 {
     try {
         $binding = Binding::getCurrentBinding();
     } catch (Exception $e) {
         return static::throwUnauthorizedError('Cannot obtain SAML2 binding');
     }
     $request = $binding->receive();
     // build response
     $response = new Response();
     $response->setInResponseTo($request->getId());
     $response->setRelayState($request->getRelayState());
     $response->setDestination($request->getAssertionConsumerServiceURL());
     // build assertion
     $assertion = new Assertion();
     $assertion->setIssuer(static::$issuer);
     $assertion->setSessionIndex(ContainerSingleton::getInstance()->generateId());
     $assertion->setNotBefore(time() - 30);
     $assertion->setNotOnOrAfter(time() + 300);
     $assertion->setAuthnContext(SAML2_Constants::AC_PASSWORD);
     // build subject confirmation
     $sc = new SubjectConfirmation();
     $sc->Method = SAML2_Constants::CM_BEARER;
     $sc->SubjectConfirmationData = new SubjectConfirmationData();
     $sc->SubjectConfirmationData->NotOnOrAfter = $assertion->getNotOnOrAfter();
     $sc->SubjectConfirmationData->Recipient = $request->getAssertionConsumerServiceURL();
     $sc->SubjectConfirmationData->InResponseTo = $request->getId();
     $assertion->setSubjectConfirmation([$sc]);
     // set NameID
     $assertion->setNameId(['Format' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress', 'Value' => $Person->Username . '@' . static::$issuer]);
     // set additional attributes
     $assertion->setAttributes(['User.Email' => [$Person->Email], 'User.Username' => [$Person->Username]]);
     // attach assertion to response
     $response->setAssertions([$assertion]);
     // create signature
     $privateKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, ['type' => 'private']);
     $privateKey->loadKey(static::$privateKey);
     $response->setSignatureKey($privateKey);
     $response->setCertificates([static::$certificate]);
     // prepare response
     $responseXML = $response->toSignedXML();
     $responseString = $responseXML->ownerDocument->saveXML($responseXML);
     // dump response and quit
     #        header('Content-Type: text/xml');
     #        die($responseString);
     // send response
     $responseBinding = new HTTPPost();
     $responseBinding->send($response);
 }
Esempio n. 2
0
 /**
  * Test NameID Encryption and Decryption.
  */
 public function testNameIdEncryption()
 {
     // Create an assertion
     $assertion = new Assertion();
     $assertion->setIssuer('testIssuer');
     $assertion->setValidAudiences(array('audience1', 'audience2'));
     $assertion->setAuthnContext('someAuthnContext');
     $assertion->setNameId(array("Value" => "just_a_basic_identifier", "Format" => "urn:oasis:names:tc:SAML:2.0:nameid-format:transient"));
     $this->assertFalse($assertion->isNameIdEncrypted());
     $publicKey = CertificatesMock::getPublicKey();
     $assertion->encryptNameId($publicKey);
     $this->assertTrue($assertion->isNameIdEncrypted());
     // Marshall it to a \DOMElement
     $assertionElement = $assertion->toXML()->ownerDocument->saveXML();
     $assertionToVerify = new Assertion(DOMDocumentFactory::fromString($assertionElement)->firstChild);
     $this->assertTrue($assertionToVerify->isNameIdEncrypted());
     $privateKey = CertificatesMock::getPrivateKey();
     $assertionToVerify->decryptNameId($privateKey);
     $this->assertFalse($assertionToVerify->isNameIdEncrypted());
     $nameID = $assertionToVerify->getNameID();
     $this->assertEquals('just_a_basic_identifier', $nameID['Value']);
     $this->assertEquals('urn:oasis:names:tc:SAML:2.0:nameid-format:transient', $nameID['Format']);
 }