diff --git a/.docker/Dockerfile b/.docker/Dockerfile index ab0f992..b13757e 100644 --- a/.docker/Dockerfile +++ b/.docker/Dockerfile @@ -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/ diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..f565eec --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -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/ diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..8e1d544 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -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 diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml new file mode 100644 index 0000000..93af044 --- /dev/null +++ b/.github/workflows/sonarcloud.yml @@ -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 }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 930b982..5fea2f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 1bac380..b85201b 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 61f7ad5..2fa134f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,13 +2,8 @@ - ./ + ./src - - ./src/resources - ./tests - ./vendor - diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..5550c75 --- /dev/null +++ b/sonar-project.properties @@ -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/ diff --git a/src/Password.php b/src/Password.php index 7e25111..dad3edb 100644 --- a/src/Password.php +++ b/src/Password.php @@ -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()) ; diff --git a/src/Timestamp.php b/src/Timestamp.php index ad5d59a..a013c8d 100644 --- a/src/Timestamp.php +++ b/src/Timestamp.php @@ -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) { diff --git a/src/WsSecurity.php b/src/WsSecurity.php index 85ff1c4..467353d 100644 --- a/src/WsSecurity.php +++ b/src/WsSecurity.php @@ -11,7 +11,7 @@ class WsSecurity { protected Security $security; - protected function __construct( + public function __construct( string $username, string $password, bool $passwordDigest = false, @@ -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 @@ -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; } diff --git a/tests/WsSecurityTest.php b/tests/WsSecurityTest.php index 7f3dd35..4b32692 100755 --- a/tests/WsSecurityTest.php +++ b/tests/WsSecurityTest.php @@ -9,7 +9,7 @@ /** * @internal - * @coversDefaultClass + * @coversDefaultClass \WsdlToPhp\WsSecurity\WsSecurity */ final class WsSecurityTest extends TestCase { @@ -44,6 +44,9 @@ public function testCreateWithoutExpiresIn() 2016-03-31T19:17:04Z ([a-zA-Z0-9=]*) + + 2016-03-31T19:17:04Z + '), $header->data->enc_value); } @@ -59,6 +62,9 @@ public function testCreateWithMustUnderstand() 2016-03-31T19:17:04Z ([a-zA-Z0-9=]*) + + 2016-03-31T19:17:04Z + '), $header->data->enc_value); } @@ -74,6 +80,9 @@ public function testCreateWithMustUnderstandAndActor() 2016-03-31T19:17:04Z ([a-zA-Z0-9=]*) + + 2016-03-31T19:17:04Z + '), $header->data->enc_value); } @@ -89,6 +98,9 @@ public function testCreateSoapVar() 2016-03-31T19:17:04Z ([a-zA-Z0-9=]*) + + 2016-03-31T19:17:04Z + '), $header->enc_value); } @@ -104,6 +116,9 @@ public function testCreateWithPasswordDigest() 2016-03-31T19:17:04Z ([a-zA-Z0-9=]*) + + 2016-03-31T19:17:04Z + '), $header->enc_value); } @@ -119,6 +134,9 @@ public function testCreateWithUsernameId() 2016-03-31T19:17:04Z ([a-zA-Z0-9=]*) + + 2016-03-31T19:17:04Z + '), $header->data->enc_value); } @@ -133,6 +151,28 @@ public function testCreateWithoutNonce() bar 2016-03-31T19:17:04Z + + 2016-03-31T19:17:04Z + + '), $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(' + + + foo + bar + 2016-03-31T19:17:04Z + + + 2016-03-31T19:17:04Z + '), $header->data->enc_value); } @@ -161,6 +201,9 @@ public function testCreateWithEnvelopeNamespace() 2016-03-31T19:17:04Z ([a-zA-Z0-9=]*) + + 2016-03-31T19:17:04Z + '), $header->data->enc_value); } }