Skip to content

Commit

Permalink
Merge branch 'release/2.0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelcom committed Sep 4, 2024
2 parents a01f4fb + 0258788 commit d689906
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 22 deletions.
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
46 changes: 46 additions & 0 deletions .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at contact@wsdltophp.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
32 changes: 32 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via pull requests on [Github].
Please make all pull requests to the `develop` branch, not the `master` branch.

## Before posting an issue

- If a command is failing, post the full output you get when running the command, with the `--verbose` argument

## Pull Requests

- **Create an issue** - Explain as detailed as possible the issue you encountered so we can understand the context of your pull request
- **[Symfony Coding Standard]** - The easiest way to apply the conventions is to run `composer lint`
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
- **Create feature branches** - Don't ask us to pull from your master branch.
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.

## Running Tests

``` bash
$ composer test
```

**Happy coding**!

[Github]: https://github.com/wsdltophp/wssecurity
[Symfony Coding Standard]: http://symfony.com/doc/current/contributing/code/standards.html
53 changes: 53 additions & 0 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: SonarCloud
on:
push:
branches:
- develop
- feature/*
- feat/*
pull_request:
types: [ opened, synchronize, reopened ]
jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup PHP with Xdebug
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
coverage: xdebug

- name: Install dependencies with composer
run: composer update --no-ansi --no-interaction --no-progress

- name: Generate coverage report with phpunit/phpunit
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 }}
coverage_path: coverage.xml
threshold_alert: 75
threshold_warning: 95

- name: Codecov analyze
uses: codecov/codecov-action@v3
with:
files: coverage.xml

- name: Fix phpunit files paths
run: sed -i 's@'$GITHUB_WORKSPACE/'@''@g' coverage.xml report.xml

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 2.0.6 - 2024/09/04
- PR #20, issue #19 - Attributes on Timestamp

## 2.0.5 - 2022/05/05
- Merged PR #15

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
7 changes: 1 addition & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php">
<coverage>
<include>
<directory>./</directory>
<directory>./src</directory>
</include>
<exclude>
<directory>./src/resources</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="full">
Expand Down
7 changes: 7 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sonar.projectKey=WsdlToPhp_WsSecurity
sonar.organization=wsdltophp
sonar.php.coverage.reportPaths=coverage.xml
sonar.php.tests.reportPath=report.xml

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
45 changes: 44 additions & 1 deletion tests/WsSecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* @internal
* @coversDefaultClass
* @coversDefaultClass \WsdlToPhp\WsSecurity\WsSecurity
*/
final class WsSecurityTest extends TestCase
{
Expand Down 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);
}
}

0 comments on commit d689906

Please sign in to comment.