Skip to content

Commit 732fb21

Browse files
authored
Merge pull request #6 from Setasign/2.0
Reworked guzzle implementation
2 parents 34598f2 + 781a04a commit 732fb21

File tree

14 files changed

+1000
-727
lines changed

14 files changed

+1000
-727
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
composer.phar
1+
/.idea/
2+
/composer.phar
23
/vendor/
34
/private/
45
/tests/functional/*.pdf

README.md

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SetaPDF-Signer component modules for the GlobalSign Digital Signing Service.
22

3-
This package offers modules for the [SetaPDF-Signer](https://www.setasign.com/signer) component that allow you to use the [Cloud-based Digital Signing Service](https://www.globalsign.com/en/digital-signatures/cloud/) by [GlobalSign](https://www.globalsign.com) to **digital sign and timestamp PDF documents in pure PHP**.
3+
This package offers modules for the [SetaPDF-Signer](https://www.setasign.com/signer) component that allow you
4+
to use the [Cloud-based Digital Signing Service](https://www.globalsign.com/en/digital-signatures/cloud/) by
5+
[GlobalSign](https://www.globalsign.com) to **digital sign and timestamp PDF documents in pure PHP**.
46

57
## Requirements
68

@@ -12,15 +14,37 @@ To use this package you need credentials for the GlobalSign Digital Signing Serv
1214

1315
See "GlobalSign-Digital-Signing-Service-Guide 1.3.pdf" (or newer) for details. Ask a GlobalSign contact for this document.
1416

15-
This package is developed and tested on PHP >= 7.1. Requirements of the [SetaPDF-Signer](https://www.setasign.com/signer) component can be found [here](https://manuals.setasign.com/setapdf-signer-manual/getting-started/#index-1).
17+
This package is developed and tested on PHP >= 7.1. Requirements of the [SetaPDF-Signer](https://www.setasign.com/signer)
18+
component can be found [here](https://manuals.setasign.com/setapdf-signer-manual/getting-started/#index-1).
19+
20+
We're using [PSR-17 (HTTP Factories)](https://www.php-fig.org/psr/psr-17/) and
21+
[PSR-18 (HTTP Client)](https://www.php-fig.org/psr/psr-18/) for the requests. So you'll need an implementation of
22+
these. We recommend using Guzzle.
23+
24+
### For PHP 7.1
25+
```
26+
"require" : {
27+
"guzzlehttp/guzzle": "^6.5",
28+
"http-interop/http-factory-guzzle": "^1.0",
29+
"mjelamanov/psr18-guzzle": "^1.3"
30+
}
31+
```
32+
33+
### For >= PHP 7.2
34+
```
35+
"require" : {
36+
"guzzlehttp/guzzle": "^7.0",
37+
"http-interop/http-factory-guzzle": "^1.0"
38+
}
39+
```
1640

1741
## Installation
1842
Add following to your composer.json:
1943

2044
```json
2145
{
2246
"require": {
23-
"setasign/seta-pdf-signer-addon-global-sign-dss": "^1.0"
47+
"setasign/seta-pdf-signer-addon-global-sign-dss": "^2.0"
2448
},
2549
"repositories": [
2650
{
@@ -31,10 +55,13 @@ Add following to your composer.json:
3155
}
3256
```
3357

34-
and execute `composer update`. You need to define the `repository` to evaluate the dependency to the [SetaPDF-Signer](https://www.setasign.com/signer) component (see [here](https://getcomposer.org/doc/faqs/why-can%27t-composer-load-repositories-recursively.md) for more details).
58+
and execute `composer update`. You need to define the `repository` to evaluate the dependency to the
59+
[SetaPDF-Signer](https://www.setasign.com/signer) component (see
60+
[here](https://getcomposer.org/doc/faqs/why-can%27t-composer-load-repositories-recursively.md) for more details).
3561

3662
### Evaluation version
37-
By default this packages depends on a licensed version of the [SetaPDF-Signer](https://www.setasign.com/signer) component. If you want to use it with an evaluation version please use following in your composer.json:
63+
By default, this packages depends on a licensed version of the [SetaPDF-Signer](https://www.setasign.com/signer) component.
64+
If you want to use it with an evaluation version please use following in your composer.json:
3865

3966
```json
4067
{
@@ -50,46 +77,42 @@ By default this packages depends on a licensed version of the [SetaPDF-Signer](h
5077
}
5178
```
5279

53-
### Without Composer
54-
55-
Make sure, that the [SetaPDF-Signer](https://www.setasign.com/signer) component is [installed](https://manuals.setasign.com/setapdf-core-manual/installation/#index-2) and its [autoloader is registered](https://manuals.setasign.com/setapdf-core-manual/getting-started/#index-1) correctly.
56-
57-
Then simply require the `src/autoload.php` file or register this package in your own PSR-4 compatible autoload implementation:
58-
59-
```php
60-
$loader = new \Example\Psr4AutoloaderClass;
61-
$loader->register();
62-
$loader->addNamespace('setasign\SetaPDF\Signer\Module\GlobalSign\Dss', 'path/to/src/');
63-
```
64-
6580
## Usage
6681

6782
All classes in this package are located in the namespace `setasign\SetaPDF\Signer\Module\GlobalSign\Dss`.
6883

6984
### The `Client` class
7085

71-
There's a simple `Client` class which wraps the [REST API](https://downloads.globalsign.com/acton/media/2674/digital-signing-service-api-documentation) into simple PHP methods. It handles the authentication, requests and responses internally. For the communication with the API it uses [Guzzle](http://guzzlephp.org/).
86+
There's a simple `Client` class which wraps the [REST API](https://downloads.globalsign.com/acton/media/2674/digital-signing-service-api-documentation)
87+
into simple PHP methods. It handles the authentication, requests and responses internally.
7288

73-
The constructor of this class requires 3 arguments:
89+
The constructor of this class requires the following arguments:
7490

75-
`$options` which are the [request options](http://docs.guzzlephp.org/en/stable/request-options.html) for Guzzle. To authenticate to the API endpoint it requires the `cert` (the client certificated issued by GlobalSign) and `ssl_key` (your private key) options. The `headers` and `body` options are set/overwritten internally.
76-
77-
`$apiKey` is your API key received from GlobalSign.
78-
79-
`$apiSecret` is the secret to your API key received from GlobalSign.
91+
- `$httpClient` PSR-18 HTTP Client implementation.
92+
- `$requestFactory` PSR-17 HTTP Factory implementation.
93+
- `$streamFactory` PSR-17 HTTP Factory implementation.
94+
- `$apiKey` is your API key received from GlobalSign.
95+
- `$apiSecret` is the secret to your API key received from GlobalSign.
8096

8197
A common creation could look like:
8298

8399
```php
84100
$options = [
101+
'http_errors' => false, // recommended for guzzle - because of PSR-18
85102
'cert' => 'path/to/tls-cert.pem',
86103
'ssl_key' => 'path/to/private/key.pem'
87104
];
88105

89106
$apiKey = 'xxxxxxxxxxxxxxxx';
90107
$apiSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
91108

92-
$client = new Dss\Client($options, $apiKey, $apiSecret);
109+
$httpClient = new GuzzleHttp\Client($options);
110+
// if you are using php 7.0 or 7.1
111+
//$httpClient = new Mjelamanov\GuzzlePsr18\Client($httpClient);
112+
$requestFactory = new Http\Factory\Guzzle\RequestFactory();
113+
$streamFactory = new Http\Factory\Guzzle\StreamFactory();
114+
115+
$client = new Dss\Client($httpClient, $requestFactory, $streamFactory, $apiKey, $apiSecret);
93116
```
94117

95118
You can use this instance to e.g. query general information:
@@ -100,43 +123,53 @@ $remainingSignatures = $client->getQuota(Dss\Client::TYPE_SIGNATURES);
100123
$signaturesCount = $client->getCount(Dss\Client::TYPE_SIGNATURES);
101124
```
102125

103-
To create a digital signature you need to create a signing certificate first which can be done with the `getIdentity()` method. The argument to this method can be an associative array as defined [here](https://downloads.globalsign.com/acton/media/2674/digital-signing-service-api-documentation#identity_post). The method will return an `Identity` instance which is nothing more than a data wrapper of the returned id, signing certificate and OCSP response.
126+
To create a digital signature you need to create a signing certificate first which can be done with the `getIdentity()`
127+
method. The argument to this method can be an associative array as defined
128+
[here](https://downloads.globalsign.com/acton/media/2674/digital-signing-service-api-documentation#identity_post).
129+
The method will return an `Identity` instance which is nothing more than a data wrapper of the returned id, signing
130+
certificate and OCSP response.
104131

105132
```php
106133
$identity = $client->getIdentity();
107134
```
108135

109-
This `Identity` needs to be forward to the signature module which internally passes it back to the `Dss\Client\sign()` method to get the final signature. It is also possible to use this method individually (just for completion):
136+
This `Identity` needs to be forward to the signature module which internally passes it back to the `Dss\Client\sign()`
137+
method to get the final signature. It is also possible to use this method individually (just for completion):
110138

111139
```php
112140
$signature = $client->sign($identity, hash('sha256', $data));
113141
```
114142

115143
### The `SignatureModule` class
116144

117-
This is the main signature module which can be used with the [SetaPDF-Signer](https://www.setasign.com/signer) component. Its constructor requires 4 arguments:
118-
119-
`$signer` is the instance of the `\SetaPDF_Signer` class to which the module is passed afterwards. Internally [`$signer->setAllowSignatureContentLengthChange(false)`](https://manuals.setasign.com/api-reference/setapdf/c/SetaPDF.Signer#method_setAllowSignatureContentLengthChange) is called to prohibit redundant signature requests.
120-
121-
`$client` needs to be the `Dss\Client` instance.
145+
This is the main signature module which can be used with the [SetaPDF-Signer](https://www.setasign.com/signer) component.
146+
Its constructor requires these arguments:
122147

123-
`$identity` a `Dss\Identity` instance.
124-
125-
`$module` needs to be a [CMS](https://manuals.setasign.com/api-reference/setapdf/c/SetaPDF.Signer.Signature.Module.Cms) or [PAdES](https://manuals.setasign.com/api-reference/setapdf/c/SetaPDF.Signer.Signature.Module.Pades) signature module instance. It is used internally to create the CMS container.
148+
- `$signer` is the instance of the `\SetaPDF_Signer` class to which the module is passed afterwards. Internally [`$signer->setAllowSignatureContentLengthChange(false)`](https://manuals.setasign.com/api-reference/setapdf/c/SetaPDF.Signer#method_setAllowSignatureContentLengthChange) is called to prohibit redundant signature requests.
149+
- `$client` needs to be the `Dss\Client` instance.
150+
- `$identity` a `Dss\Identity` instance.
151+
- `$module` needs to be a [CMS](https://manuals.setasign.com/api-reference/setapdf/c/SetaPDF.Signer.Signature.Module.Cms) or [PAdES](https://manuals.setasign.com/api-reference/setapdf/c/SetaPDF.Signer.Signature.Module.Pades) signature module instance. It is used internally to create the CMS container.
126152

127153
A simple complete signature process would look like this:
128154

129155
```php
130-
// setup the client and identity
156+
// set up the client and identity
131157
$options = [
158+
'http_errors' => false,
132159
'cert' => 'path/to/tls-cert.pem',
133160
'ssl_key' => 'path/to/private/key.pem'
134161
];
135162

136163
$apiKey = 'xxxxxxxxxxxxxxxx';
137164
$apiSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
138165

139-
$client = new Dss\Client($options, $apiKey, $apiSecret);
166+
$httpClient = new GuzzleHttp\Client($options);
167+
// if you are using php 7.0 or 7.1
168+
//$httpClient = new Mjelamanov\GuzzlePsr18\Client($httpClient);
169+
$requestFactory = new Http\Factory\Guzzle\RequestFactory();
170+
$streamFactory = new Http\Factory\Guzzle\StreamFactory();
171+
172+
$client = new Dss\Client($httpClient, $requestFactory, $streamFactory, $apiKey, $apiSecret);
140173
$identity = $client->getIdentity();
141174

142175
// now start the signature process
@@ -178,7 +211,8 @@ $signer->timestamp();
178211

179212
## Information about Tests
180213

181-
The test suite currently only comes with functional tests, which invoke **real service calls**! Keep in mind that these calls are deducted from your signature contingent and you should not execute these tests in an automated environment!
214+
The test suite currently only comes with functional tests, which invoke **real service calls**! Keep in mind that these
215+
calls are deducted from your signature contingent. You should not execute these tests in an automated environment!!
182216

183217
To execute the tests, you need to create a folder in the root of this package with the following file:
184218

@@ -201,4 +235,4 @@ return [
201235

202236
## License
203237

204-
This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
238+
This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

composer.json

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22
"name": "setasign/seta-pdf-signer-addon-global-sign-dss",
33
"description": "A SetaPDF-Signer component signature module for the GlobalSign Digital Signing Service.",
44
"type": "library",
5-
"require": {
6-
"php": "^7.1 || ^8.0",
7-
"guzzlehttp/guzzle": "^6.0",
8-
"setasign/setapdf-signer": "^2.31"
9-
},
10-
"require-dev": {
11-
"phpunit/phpunit": "^7.0"
12-
},
135
"license": "MIT",
146
"authors": [
157
{
168
"name": "Jan Slabon",
179
"email": "[email protected]"
1810
}
1911
],
20-
"repositories": [
21-
{
22-
"type": "composer",
23-
"url": "https://www.setasign.com/downloads/"
24-
}
25-
],
12+
"require": {
13+
"php": "^7.1 || ^8.0",
14+
"ext-json": "*",
15+
"psr/http-client": "^1.0",
16+
"psr/http-factory": "^1.0",
17+
"setasign/setapdf-signer": "^2.31"
18+
},
19+
"require-dev": {
20+
"ext-openssl": "*",
21+
"phpunit/phpunit": "^7.0",
22+
"guzzlehttp/guzzle": "^6.5",
23+
"http-interop/http-factory-guzzle": "^1.0",
24+
"mjelamanov/psr18-guzzle": "^1.3"
25+
},
26+
2627
"autoload": {
2728
"psr-4": {
2829
"setasign\\SetaPDF\\Signer\\Module\\GlobalSign\\Dss\\": "src/"
@@ -32,5 +33,12 @@
3233
"psr-4": {
3334
"setasign\\SetaPDF\\Signer\\Module\\GlobalSign\\Dss\\tests\\": "tests/"
3435
}
35-
}
36+
},
37+
38+
"repositories": [
39+
{
40+
"type": "composer",
41+
"url": "https://www.setasign.com/downloads/"
42+
}
43+
]
3644
}

0 commit comments

Comments
 (0)