Skip to content

issue #19 - provide a way to set TimeStamp attribute #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FROM splitbrain/phpfarm:jessie
FROM php:7.4-apache

RUN apt-get update && apt-get install -y git zip
RUN apt-get update \
&& apt-get install -y libxml2-dev git zip \
&& docker-php-ext-install soap

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . /var/www/
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v2
with:
Expand All @@ -29,6 +31,7 @@ jobs:
run: vendor/bin/phpunit --coverage-clover coverage.xml --log-junit report.xml

- name: Monitor coverage
if: false
uses: slavcodev/coverage-monitor-action@v1
with:
github_token: ${{ secrets.SECRET_GITHUB_TOKEN }}
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ The `WsSecurity::createWsSecuritySoapHeader` parameters are defined in this orde
- **$mustunderstand**: classic option of the [\SoapClient](http://php.net/manual/en/soapclient.soapclient.php) class
- **$actor**: classic option of the [\SoapClient](http://php.net/manual/en/soapclient.soapclient.php) class
- **$usernameId**: the id to attach to the UsernameToken element, optional
- **$addNonce**: _true_ by default, if true, it adds the nonce element to the header, if false it does not add the nonce element to the header
- **$addNonce**: _true_ by default, if true, it adds the nonce element to the header, if false it does not add the nonce element to the header

## Alternative usage ##
Create an instance of the Security class
```php
use WsdlToPhp\WsSecurity\WsSecurity;

$wsSecurity = new WsSecurity('login', 'password', true, /*$addCreated*/ time());

// access its properties to alter them
$wsSecurity->getSecurity()->getTimestamp()->setAttribute('wsu:Id', 'AnyRequestValue');

// Get the SoapHeader
$header = $security->getSoapHeader($returnSoapHeader = true, $mustunderstand = false, $actor = null);
```


## Testing using [Docker](https://www.docker.com/)
Thanks to the [Docker image](https://hub.docker.com/r/splitbrain/phpfarm) of [phpfarm](https://github.com/fpoirotte/phpfarm), tests can be run locally under *any* PHP version using the cli:
Expand All @@ -58,9 +73,9 @@ $ docker-compose up -d --build
You then have a container named `ws_security` in which you can run `composer` commands and `php cli` commands such as:
```bash
# install deps in container (using update ensure it does use the composer.lock file if there is any)
$ docker exec -it ws_security php-7.4 /usr/bin/composer update
$ docker exec -it ws_security php /usr/bin/composer update
# run tests in container
$ docker exec -it ws_security php-7.4 -dmemory_limit=-1 vendor/bin/phpunit
$ docker exec -it ws_security php -dmemory_limit=-1 vendor/bin/phpunit
```

## FAQ
Expand Down
11 changes: 2 additions & 9 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,5 @@ sonar.organization=wsdltophp
sonar.php.coverage.reportPaths=coverage.xml
sonar.php.tests.reportPath=report.xml

# This is the name and version displayed in the SonarCloud UI.
#sonar.projectName=WsSecurity
#sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
#sonar.sources=.

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
sonar.sources=src/
sonar.tests=tests/
2 changes: 1 addition & 1 deletion src/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(string $password, string $typeValue = self::TYPE_PAS
{
$this
->setTypeValue($typeValue)
->setTimestampValue($timestampValue ? $timestampValue : time())
->setTimestampValue($timestampValue ?: time())
->setNonceValue((string) mt_rand())
;

Expand Down
4 changes: 2 additions & 2 deletions src/Timestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class Timestamp extends Element
{
public const NAME = 'Timestamp';

protected ?Created $created;
protected ?Created $created = null;

protected ?Expires $expires;
protected ?Expires $expires = null;

public function __construct(string $namespace = self::NS_WSU)
{
Expand Down
26 changes: 19 additions & 7 deletions src/WsSecurity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WsSecurity
{
protected Security $security;

protected function __construct(
public function __construct(
string $username,
string $password,
bool $passwordDigest = false,
Expand Down Expand Up @@ -55,15 +55,21 @@ public static function createWsSecuritySoapHeader(
string $envelopeNamespace = Security::ENV_NAMESPACE
) {
$self = new WsSecurity($username, $password, $passwordDigest, $addCreated, $addExpires, $mustUnderstand, $actor, $usernameId, $addNonce, $envelopeNamespace);

return $self->getSoapHeader($returnSoapHeader, $mustUnderstand, $actor);
}

public function getSoapHeader(bool $returnSoapHeader = true, bool $mustUnderstand = false, ?string $actor = null): object
{
if ($returnSoapHeader) {
if (!empty($actor)) {
return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand, $actor);
return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($this->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand, $actor);
}

return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand);
return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($this->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand);
}

return new SoapVar($self->getSecurity()->toSend(), XSD_ANYXML);
return new SoapVar($this->getSecurity()->toSend(), XSD_ANYXML);
}

protected function initSecurity(bool $mustUnderstand = false, ?string $actor = null, string $envelopeNamespace = Security::ENV_NAMESPACE): self
Expand Down Expand Up @@ -115,12 +121,18 @@ protected function setCreated(int $addCreated): self
protected function setTimestamp(int $addCreated = 0, int $addExpires = 0): self
{
$timestampValue = $this->getPassword()->getTimestampValue();
if ($addCreated && $addExpires && $timestampValue) {
$timestamp = new Timestamp();
if (!$timestampValue || (0 === $addCreated && 0 === $addExpires)) {
return $this;
}

$timestamp = new Timestamp();
if (0 < $addCreated) {
$timestamp->setCreated(new Created($timestampValue));
}
if (0 < $addExpires) {
$timestamp->setExpires(new Expires($timestampValue, $addExpires));
$this->security->setTimestamp($timestamp);
}
$this->security->setTimestamp($timestamp);

return $this;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/WsSecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function testCreateWithoutExpiresIn()
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wsu: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>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2016-03-31T19:17:04Z</wsu:Created>
</wsu:Timestamp>
</wsse:Security>'), $header->data->enc_value);
}

Expand All @@ -59,6 +62,9 @@ public function testCreateWithMustUnderstand()
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wsu: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>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2016-03-31T19:17:04Z</wsu:Created>
</wsu:Timestamp>
</wsse:Security>'), $header->data->enc_value);
}

Expand All @@ -74,6 +80,9 @@ public function testCreateWithMustUnderstandAndActor()
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wsu: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>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2016-03-31T19:17:04Z</wsu:Created>
</wsu:Timestamp>
</wsse:Security>'), $header->data->enc_value);
}

Expand All @@ -89,6 +98,9 @@ public function testCreateSoapVar()
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wsu: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>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2016-03-31T19:17:04Z</wsu:Created>
</wsu:Timestamp>
</wsse:Security>'), $header->enc_value);
}

Expand All @@ -104,6 +116,9 @@ public function testCreateWithPasswordDigest()
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wsu: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>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2016-03-31T19:17:04Z</wsu:Created>
</wsu:Timestamp>
</wsse:Security>'), $header->enc_value);
}

Expand All @@ -119,6 +134,9 @@ public function testCreateWithUsernameId()
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wsu: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>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2016-03-31T19:17:04Z</wsu:Created>
</wsu:Timestamp>
</wsse:Security>'), $header->data->enc_value);
}

Expand All @@ -133,6 +151,28 @@ public function testCreateWithoutNonce()
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">bar</wsse:Password>
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wsu:Created>
</wsse:UsernameToken>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2016-03-31T19:17:04Z</wsu:Created>
</wsu:Timestamp>
</wsse:Security>'), $header->data->enc_value);
}

public function testWithTimestampAttribute()
{
$security = new WsSecurity('foo', 'bar', false, 1459451824, 0, false, null, null, false);
$security->getSecurity()->getTimestamp()->setAttribute('wsu:Id', 'Timestamp-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX');
$header = $security->getSoapHeader();
$this->assertInstanceOf(SoapHeader::class, $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">
<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>
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wsu:Created>
</wsse:UsernameToken>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="Timestamp-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">
<wsu:Created>2016-03-31T19:17:04Z</wsu:Created>
</wsu:Timestamp>
</wsse:Security>'), $header->data->enc_value);
}

Expand Down Expand Up @@ -161,6 +201,9 @@ public function testCreateWithEnvelopeNamespace()
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2016-03-31T19:17:04Z</wsu: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>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2016-03-31T19:17:04Z</wsu:Created>
</wsu:Timestamp>
</wsse:Security>'), $header->data->enc_value);
}
}
Loading