public function addUserToken($userName, $password = null, $passwordDigest = false) { if ($passwordDigest && empty($password)) { throw new \Exception("Cannot calculate the digest without a password"); } $security = $this->locateSecurityHeader(); $token = $this->soapDoc->createElementNS(WSSE::WSSENS, WSSE::WSSEPFX . ':UsernameToken'); $security->insertBefore($token, $security->firstChild); $username = $this->soapDoc->createElementNS(WSSE::WSSENS, WSSE::WSSEPFX . ':Username', $userName); $token->appendChild($username); /* Generate nonce - create a 256 bit session key to be used */ $objKey = new XMLSecurityKey(XMLSecurityKey::AES256_CBC); $nonce = $objKey->generateSessionKey(); unset($objKey); $createdate = gmdate("Y-m-d\\TH:i:s") . 'Z'; if ($password) { $passType = WSSE::WSUNAME . '#PasswordText'; if ($passwordDigest) { $password = base64_encode(sha1($nonce . $createdate . $password, true)); $passType = WSSE::WSUNAME . '#PasswordDigest'; } $passwordNode = $this->soapDoc->createElementNS(WSSE::WSSENS, WSSE::WSSEPFX . ':Password', $password); $token->appendChild($passwordNode); $passwordNode->setAttribute('Type', $passType); } $nonceNode = $this->soapDoc->createElementNS(WSSE::WSSENS, WSSE::WSSEPFX . ':Nonce', base64_encode($nonce)); $token->appendChild($nonceNode); $created = $this->soapDoc->createElementNS(WSSE::WSUNS, WSSE::WSUPFX . ':Created', $createdate); $token->appendChild($created); }