Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelcom committed Jan 28, 2019
2 parents 67ba2bf + a29207e commit 1b59b88
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 1.2.0
- Merged PR #5
- Fix minor typo

## 1.1.0
- Provide Docker settings
- Merged PR #2 and #4
Expand Down
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"name": "Tom Vaughan",
"email": "[email protected]",
"role": "Contributor"
},
{
"name": "NtlBldrv",
"role": "Contributor"
}
],
"support" : {
Expand All @@ -31,11 +35,11 @@
},
"scripts": {
"test": "vendor/bin/phpunit",
"lint": "vendor/bin/php-cs-fixer fix"
"lint": "vendor/bin/php-cs-fixer fix --ansi --diff --verbose"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^6.0",
"friendsofphp/php-cs-fixer": "^2.0"
"friendsofphp/php-cs-fixer": "^2.0",
"phpunit/phpunit": "^4.8 || ^6.0"
},
"autoload" : {
"psr-4" : {
Expand All @@ -46,6 +50,9 @@
"psr-4" : {
"WsdlToPhp\\WsSecurity\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true
}
}

22 changes: 15 additions & 7 deletions src/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ class Security extends Element
* Element attribute mustunderstand name
* @var string
*/
const ATTRIBUTE_MUST_UNDERSTAND = 'SOAP-ENV:mustunderstand';
const ATTRIBUTE_MUST_UNDERSTAND = ':mustunderstand';
/**
* Element attribute mustunderstand name
* @var string
*/
const ATTRIBUTE_ACTOR = 'SOAP-ENV:actor';
const ATTRIBUTE_ACTOR = ':actor';
/**
* Envelop namespace
* @var string
*/
const ENV_NAMESPACE = 'SOAP-ENV';
/**
* UsernameToken element
* @var UsernameToken
Expand All @@ -29,23 +34,26 @@ class Security extends Element
* @var Timestamp
*/
protected $timestamp;

/**
* Constructor for Nonce element
* @param bool $mustunderstand
*
* @param bool $mustunderstand
* @param string $actor
* @param string $envelopeNamespace
* @param string $namespace the namespace
*/
public function __construct($mustunderstand = false, $actor = null, $namespace = self::NS_WSSE)
public function __construct($mustunderstand = false, $actor = null, $namespace = self::NS_WSSE, $envelopeNamespace = self::ENV_NAMESPACE)
{
parent::__construct(self::NAME, $namespace);
/**
* Sets attributes
*/
if ($mustunderstand === true) {
$this->setAttribute(self::ATTRIBUTE_MUST_UNDERSTAND, $mustunderstand);
$this->setAttribute($envelopeNamespace. self::ATTRIBUTE_MUST_UNDERSTAND, $mustunderstand);
}
if (!empty($actor)) {
$this->setAttribute(self::ATTRIBUTE_ACTOR, $actor);
$this->setAttribute($envelopeNamespace . self::ATTRIBUTE_ACTOR, $actor);
}
}
/**
Expand Down Expand Up @@ -83,7 +91,7 @@ public function setTimestamp(Timestamp $timestamp)
/**
* Overrides methods in order to set the values
* @param bool $asDomElement returns elements as a DOMElement or as a string
* @return string
* @return string|\DOMElement
*/
protected function __toSend($asDomElement = false)
{
Expand Down
69 changes: 50 additions & 19 deletions src/WsSecurity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,50 @@ class WsSecurity
* @var Security
*/
protected $security;

/**
* @param string $username
* @param string $password
* @param bool $passwordDigest
* @param int $addCreated
* @param int $addExpires
* @param bool $mustunderstand
* @param bool $passwordDigest
* @param int $addCreated
* @param int $addExpires
* @param bool $mustunderstand
* @param string $actor
* @param string $usernameId
* @param bool $addNonce
* @param bool $addNonce
* @param string $envelopeNamespace
*/
protected function __construct($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $mustunderstand = false, $actor = null, $usernameId = null, $addNonce = true)
{
protected function __construct(
$username,
$password,
$passwordDigest = false,
$addCreated = 0,
$addExpires = 0,
$mustunderstand = false,
$actor = null,
$usernameId = null,
$addNonce = true,
$envelopeNamespace = Security::ENV_NAMESPACE
) {
$this
->initSecurity($mustunderstand, $actor)
->initSecurity($mustunderstand, $actor, $envelopeNamespace)
->setUsernameToken($username, $usernameId)
->setPassword($password, $passwordDigest, $addCreated)
->setNonce($addNonce)
->setCreated($addCreated)
->setTimestamp($addCreated, $addExpires);
}

/**
* @param bool $mustunderstand
* @param bool $mustunderstand
* @param string $actor
* @param string $envelopeNamespace
*
* @return WsSecurity
*/
protected function initSecurity($mustunderstand = false, $actor = null)
protected function initSecurity($mustunderstand = false, $actor = null, $envelopeNamespace = Security::ENV_NAMESPACE)
{
$this->security = new Security($mustunderstand, $actor);
$this->security = new Security($mustunderstand, $actor, Security::NS_WSSE, $envelopeNamespace);
return $this;
}
/**
Expand All @@ -46,22 +61,38 @@ public function getSecurity()
{
return $this->security;
}

/**
* Create the SoapHeader object to send as SoapHeader in the SOAP request.
*
* @param string $username
* @param string $password
* @param bool $passwordDigest
* @param int $addCreated
* @param int $addExpires
* @param bool $mustunderstand
* @param bool $passwordDigest
* @param int $addCreated
* @param int $addExpires
* @param bool $returnSoapHeader
* @param bool $mustunderstand
* @param string $actor
* @param string $usernameId
* @param bool $addNonce
* @param bool $addNonce
* @param string $envelopeNamespace
*
* @return \SoapHeader|\SoapVar
*/
public static function createWsSecuritySoapHeader($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $returnSoapHeader = true, $mustunderstand = false, $actor = null, $usernameId = null, $addNonce = true)
{
$self = new WsSecurity($username, $password, $passwordDigest, $addCreated, $addExpires, $mustunderstand, $actor, $usernameId, $addNonce);
public static function createWsSecuritySoapHeader(
$username,
$password,
$passwordDigest = false,
$addCreated = 0,
$addExpires = 0,
$returnSoapHeader = true,
$mustunderstand = false,
$actor = null,
$usernameId = null,
$addNonce = true,
$envelopeNamespace = Security::ENV_NAMESPACE
) {
$self = new WsSecurity($username, $password, $passwordDigest, $addCreated, $addExpires, $mustunderstand, $actor, $usernameId, $addNonce, $envelopeNamespace);
if ($returnSoapHeader) {
if (!empty($actor)) {
return new \SoapHeader(Element::NS_WSSE, 'Security', new \SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustunderstand, $actor);
Expand Down
28 changes: 28 additions & 0 deletions tests/WsSecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,32 @@ public function testCreateWithoutNonce()
</wsse:UsernameToken>
</wsse:Security>'), $header->data->enc_value);
}

public function testCreateWithEnvelopeNamespace()
{
$header = WsSecurity::createWsSecuritySoapHeader(
'foo',
'bar',
false,
1459451824,
0,
true,
true,
'BAR',
null,
true,
'env'
);

$this->assertInstanceOf('\SoapHeader', $header);
$this->assertMatches(self::innerTrim('
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" env:mustunderstand="1" env:actor="BAR">
<wsse:UsernameToken>
<wsse:Username>foo</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">bar</wsse:Password>
<wssu:Created xmlns:wssu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wssu:Created>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">([a-zA-Z0-9=]*)</wsse:Nonce>
</wsse:UsernameToken>
</wsse:Security>'), $header->data->enc_value);
}
}

0 comments on commit 1b59b88

Please sign in to comment.