Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelcom committed Oct 3, 2018
2 parents 5e42d1a + 6eb1971 commit 67ba2bf
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 41 deletions.
10 changes: 10 additions & 0 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM php:7.2-apache

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

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . /var/www/html

WORKDIR /var/www/html
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendor/
composer.lock
phpunit.xml

.idea
20 changes: 15 additions & 5 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<?php

$finder = Symfony\CS\Finder\DefaultFinder::create()
$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude('tests')
->in(__DIR__);

return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->finder($finder);
return PhpCsFixer\Config::create()
->setUsingCache(false)
->setRules(array(
'@PSR2' => true,
'array_syntax' => [
'syntax' => 'short',
],
'binary_operator_spaces' => true,
'no_whitespace_in_blank_line' => true,
'ternary_operator_spaces' => true,
'cast_spaces' => true,
'trailing_comma_in_multiline_array' => true
))
->setFinder($finder);
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ language: php
sudo: false

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2

before_install:
- composer self-update
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## 1.1.0
- Provide Docker settings
- Merged PR #2 and #4
- Fixed issue #3
- Drop support of PHP 5.3
- Update contributors list
- Update lint options

## 1.0.1
- Improve code, add SensioLabs Insight badge, remove useless value

Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# WsdlToPhp WsSecurity
# WsSecurity

> WsSecurity adds the WSSE authentication header to your SOAP request
[![License](https://poser.pugx.org/wsdltophp/wssecurity/license)](https://packagist.org/packages/wsdltophp/wssecurity)
[![Latest Stable Version](https://poser.pugx.org/wsdltophp/wssecurity/version.png)](https://packagist.org/packages/wsdltophp/wssecurity)
[![Build Status](https://api.travis-ci.org/WsdlToPhp/WsSecurity.svg)](https://travis-ci.org/WsdlToPhp/WsSecurity)
[![PHP 7 ready](http://php7ready.timesplinter.ch/WsdlToPhp/PackageGenerator/badge.svg)](https://travis-ci.org/WsdlToPhp/PackageGenerator)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/WsdlToPhp/WsSecurity/badges/quality-score.png)](https://scrutinizer-ci.com/g/WsdlToPhp/WsSecurity/)
[![Code Coverage](https://scrutinizer-ci.com/g/WsdlToPhp/WsSecurity/badges/coverage.png)](https://scrutinizer-ci.com/g/WsdlToPhp/WsSecurity/)
[![Total Downloads](https://poser.pugx.org/wsdltophp/wssecurity/downloads)](https://packagist.org/packages/wsdltophp/wssecurity)
[![Dependency Status](https://www.versioneye.com/user/projects/5615103ca19334001e000331/badge.svg?style=flat)](https://www.versioneye.com/user/projects/5615103ca19334001e000331)
[![StyleCI](https://styleci.io/repos/43811404/shield)](https://styleci.io/repos/43811404)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/1cc28292-0f49-47eb-b2ca-4bdd6c0223f1/mini.png)](https://insight.sensiolabs.com/projects/1cc28292-0f49-47eb-b2ca-4bdd6c0223f1)

Expand All @@ -15,7 +16,7 @@ This repository contains multiple classes that may be used indepdently but for n

The WsSecurity class provides a static method that takes the parameters that should suffice to create your Ws-Security Username Authentication header required in your SOAP request.

Concretly, you must include this repository in your project using composer (`composer require wsdltophp/wssecurity:dev-master`) then use it such as:
Concretly, you must include this repository in your project using composer (`composer require wsdltophp/wssecurity`) then use it such as:

```php
use WsdlToPhp\WsSecurity\WsSecurity;
Expand All @@ -32,7 +33,7 @@ $soapClient->__setSoapHeaders($soapHeader);
$soapClient->__soapCall('echoVoid', []);
```

The `WsSecurity::createWsSecuritySoapHeader` parameters are defined in this order `($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $returnSoapHeader = true, $mustunderstand = false, $actor = null, $usernameId = null)`:
The `WsSecurity::createWsSecuritySoapHeader` parameters are defined in this order `($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $returnSoapHeader = true, $mustunderstand = false, $actor = null, $usernameId = null, $addNonce = true)`:

- **$username**: your login/username
- **$password**: your password
Expand All @@ -43,11 +44,12 @@ 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

## Unit tests
You can run the unit tests with the following command at the root directory of this project:
```
$ phpunit
$ composer test
```

## Feedback
Expand Down
22 changes: 20 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,40 @@
"email": "[email protected]",
"homepage": "https://www.wsdltophp.com",
"role": "Owner"
},
{
"name": "Alexey Anisimov",
"email": "[email protected]",
"role": "Contributor"
},
{
"name": "Tom Vaughan",
"email": "[email protected]",
"role": "Contributor"
}
],
"support" : {
"email" : "[email protected]"
},
"require" : {
"php" : ">=5.3",
"php" : ">=5.4",
"ext-soap": "*"
},
"scripts": {
"test": "vendor/bin/phpunit",
"lint": "vendor/bin/php-cs-fixer fix"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^6.0",
"friendsofphp/php-cs-fixer": "^2.0"
},
"autoload" : {
"psr-4" : {
"WsdlToPhp\\WsSecurity\\" : "src",
"WsdlToPhp\\WsSecurity\\" : "src"
}
},
"autoload-dev" : {
"psr-4" : {
"WsdlToPhp\\WsSecurity\\Tests\\": "tests"
}
}
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.4'

services:
php:
build:
context: .
dockerfile: .docker/Dockerfile
volumes:
- .:/var/www/html:rw
container_name: ws_security
6 changes: 3 additions & 3 deletions src/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Element
* Array of attributes that must contains the element
* @var array
*/
protected $attributes = array();
protected $attributes = [];
/**
* The namespace the element belongs to
* @var string
Expand Down Expand Up @@ -73,7 +73,7 @@ class Element
* @param mixed $value
* @param array $attributes
*/
public function __construct($name, $namespace, $value = null, array $attributes = array())
public function __construct($name, $namespace, $value = null, array $attributes = [])
{
$this
->setName($name)
Expand Down Expand Up @@ -298,7 +298,7 @@ protected function appendValuesToElementToSend(array $values, \DOMElement $eleme
protected function appendAttributesToElementToSend(\DOMElement $element)
{
if ($this->hasAttributes()) {
foreach ($this->getAttributes() as $attributeName=>$attributeValue) {
foreach ($this->getAttributes() as $attributeName => $attributeValue) {
$element->setAttribute($attributeName, $attributeValue);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nonce.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Nonce extends Element
*/
public function __construct($nonce, $namespace = self::NS_WSSE)
{
parent::__construct(self::NAME, $namespace, self::encodeNonce($nonce), array(
parent::__construct(self::NAME, $namespace, self::encodeNonce($nonce), [
self::ATTRIBUTE_ENCODING_TYPE => self::NS_ENCODING,
));
]);
}
/**
* Encode Nonce value
Expand Down
4 changes: 2 additions & 2 deletions src/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function __construct($password, $typeValue = self::TYPE_PASSWORD_TEXT, $t
->setTypeValue($typeValue)
->setTimestampValue($timestampValue ? $timestampValue : time())
->setNonceValue(mt_rand());
parent::__construct(self::NAME, $namespace, $this->convertPassword($password), array(
parent::__construct(self::NAME, $namespace, $this->convertPassword($password), [
self::ATTRIBUTE_TYPE => $typeValue,
));
]);
}
/**
* Returns the converted form of the password accroding to the password typeValue
Expand Down
4 changes: 2 additions & 2 deletions src/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public function setTimestamp(Timestamp $timestamp)
*/
protected function __toSend($asDomElement = false)
{
$this->setValue(array(
$this->setValue([
$this->getUsernameToken(),
$this->getTimestamp(),
));
]);
return parent::__toSend($asDomElement);
}
}
4 changes: 2 additions & 2 deletions src/Timestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public function setExpires(Expires $expires)
*/
protected function __toSend($asDomElement = false)
{
$this->setValue(array(
$this->setValue([
$this->getCreated(),
$this->getExpires(),
));
]);
return parent::__toSend($asDomElement);
}
}
8 changes: 4 additions & 4 deletions src/UsernameToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class UsernameToken extends Element
*/
public function __construct($id = null, $namespace = self::NS_WSSE)
{
parent::__construct(self::NAME, $namespace, null, empty($id) ? array() : array(
parent::__construct(self::NAME, $namespace, null, empty($id) ? [] : [
sprintf('%s:%s', parent::NS_WSSU_NAME, self::ATTRIBUTE_ID) => $id,
));
]);
}
/**
* @return Username
Expand Down Expand Up @@ -117,12 +117,12 @@ public function setNonce($nonce)
*/
protected function __toSend($asDomElement = false)
{
$this->setValue(array(
$this->setValue([
$this->getUsername(),
$this->getPassword(),
$this->getCreated(),
$this->getNonce(),
));
]);
return parent::__toSend($asDomElement);
}
}
21 changes: 13 additions & 8 deletions src/WsSecurity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ class WsSecurity
* @param bool $mustunderstand
* @param string $actor
* @param string $usernameId
* @param bool $addNonce
*/
protected function __construct($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $mustunderstand = false, $actor = null, $usernameId = null)
protected function __construct($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $mustunderstand = false, $actor = null, $usernameId = null, $addNonce = true)
{
$this
->initSecurity($mustunderstand, $actor)
->setUsernameToken($username, $usernameId)
->setPassword($password, $passwordDigest, $addCreated)
->setNonce()
->setNonce($addNonce)
->setCreated($addCreated)
->setTimestamp($addCreated, $addExpires);
}
Expand Down Expand Up @@ -55,11 +56,12 @@ public function getSecurity()
* @param bool $mustunderstand
* @param string $actor
* @param string $usernameId
* @param bool $addNonce
* @return \SoapHeader|\SoapVar
*/
public static function createWsSecuritySoapHeader($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $returnSoapHeader = true, $mustunderstand = false, $actor = null, $usernameId = null)
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);
$self = new WsSecurity($username, $password, $passwordDigest, $addCreated, $addExpires, $mustunderstand, $actor, $usernameId, $addNonce);
if ($returnSoapHeader) {
if (!empty($actor)) {
return new \SoapHeader(Element::NS_WSSE, 'Security', new \SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustunderstand, $actor);
Expand Down Expand Up @@ -94,13 +96,16 @@ protected function setPassword($password, $passwordDigest = false, $addCreated =
return $this;
}
/**
* @param bool $addNonce
* @return WsSecurity
*/
protected function setNonce()
protected function setNonce($addNonce)
{
$nonceValue = $this->getPassword()->getNonceValue();
if (!empty($nonceValue)) {
$this->getUsernameToken()->setNonce(new Nonce($nonceValue));
if ($addNonce) {
$nonceValue = $this->getPassword()->getNonceValue();
if (!empty($nonceValue)) {
$this->getUsernameToken()->setNonce(new Nonce($nonceValue));
}
}
return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ abstract class TestCase extends PHPUnitFrameworkTestCase
{
public static function innerTrim($string)
{
return trim(preg_replace('/>\s*</', '><', str_replace(array(
return trim(preg_replace('/>\s*</', '><', str_replace([
"\r",
"\n",
"\t"
), '', $string)));
"\t",
], '', $string)));
}
public static function assertMatches($pattern, $string)
{
Expand Down
13 changes: 13 additions & 0 deletions tests/WsSecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,17 @@ public function testCreateWithUsernameId()
</wsse:UsernameToken>
</wsse:Security>'), $header->data->enc_value);
}
public function testCreateWithoutNonce()
{
$header = WsSecurity::createWsSecuritySoapHeader('foo', 'bar', false, 1459451824, 0, true, true, 'BAR', 'X90I3u8', false);
$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" SOAP-ENV:mustunderstand="1" SOAP-ENV:actor="BAR">
<wsse:UsernameToken wssu:Id="X90I3u8">
<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:UsernameToken>
</wsse:Security>'), $header->data->enc_value);
}
}

0 comments on commit 67ba2bf

Please sign in to comment.