-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.php
112 lines (92 loc) · 3.91 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
use setasign\SetaPDF\Signer\Module\CSC\Client;
use setasign\SetaPDF\Signer\Module\CSC\ClientException;
use setasign\SetaPDF\Signer\Module\CSC\Module;
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
require_once(__DIR__ . '/../vendor/autoload.php');
session_start();
if (!file_exists('settings.php')) {
echo 'The settings.php file is missing. See settings.php.dist for an example.';
die();
}
$settings = require 'settings.php';
$apiUri = $settings['apiUri'];
$fileToSign = __DIR__ . '/assets/Laboratory-Report.pdf';
$resultPath = 'signed.pdf';
// to create or update your access token you have to call generate-token.php first
if (!isset($_SESSION['accessToken']['access_token'])) {
echo 'Missing access token! <a href="generate-token.php">Login here</a>';
die();
}
// check if the access token is still valid
if (!isset($_SESSION['accessToken']['expires']) || $_SESSION['accessToken']['expires'] < time()) {
echo 'Access token is expired! <a href="generate-token.php">Renew here</a>';
die();
}
$accessToken = $_SESSION['accessToken']['access_token'];
$httpClient = new GuzzleHttp\Client();
$httpClient = new Mjelamanov\GuzzlePsr18\Client($httpClient);
$requestFactory = new Http\Factory\Guzzle\RequestFactory();
$streamFactory = new Http\Factory\Guzzle\StreamFactory();
$client = new Client($apiUri, $httpClient, $requestFactory, $streamFactory);
echo '<pre>';
$credentialIds = ($client->credentialsList($accessToken))['credentialIDs'];
var_dump($credentialIds);
// we just use the first credential on the list
$credentialId = $credentialIds[0];
// fetch all information regarding your credential id like the certificates
$credentialInfo = $client->credentialsInfo($accessToken, $credentialId, 'chain', true, true);
var_dump($credentialInfo);
echo '</pre>';
if ($credentialInfo['authMode'] === 'oauth2code') {
echo 'The selected credentialId does only support oauth2code authentification.'
. ' A synchronous sign request is not possible - take a look at the <a href="async-demo.php">async-demo</a> instead.';
die();
}
$certificates = $credentialInfo['cert']['certificates'];
// INFO: YOU SHOULD CACHE THE DATA IN $credentialInfo FOR LESS API REQUESTS
// the first certificate is always the signing certificate
$certificate = array_shift($certificates);
$algorithm = $credentialInfo['key']['algo'][0];
$module = new Module($accessToken, $client);
$module->setCredentialId($credentialId);
$module->setSignatureAlgorithmOid($algorithm);
$module->setCertificate($certificate);
$module->setExtraCertificates($certificates);
if ($credentialInfo['authMode'] === 'explicit' && !isset($_GET['otp']) && !isset($_GET['pin'])) {
// you should check the OTP and/or PIN entry in $credentialInfo for how to setup authentication exactly
echo 'Please enter OTP or PIN:';
echo '<form><input type="text" name="otp" placeholder="OTP"> or <input type="text" name="pin" placeholder="PIN">';
echo '<input type="submit"/></form>';
die();
}
if (isset($_GET['otp'])) {
$module->setOtp($_GET['otp']);
}
if (isset($_GET['pin'])) {
$module->setPin($_GET['pin']);
}
// create a writer instance
$writer = new SetaPDF_Core_Writer_File($resultPath);
// create the document instance
$document = SetaPDF_Core_Document::loadByFilename($fileToSign, $writer);
// create the signer instance
$signer = new SetaPDF_Signer($document);
try {
$signer->sign($module);
} catch (ClientException $e) {
echo 'An error occured:';
echo $e->getMessage() . ': ' . $e->getResponse()->getBody();
echo '<br/><a href="?">restart</a>';
die();
} catch (\Exception $e) {
echo 'An error occured:';
echo $e->getMessage();
echo '<br/><a href="?">restart</a>';
die();
}
echo '<a href="data:application/pdf;base64,' . base64_encode(file_get_contents($resultPath)) . '" ' .
'download="' . basename($resultPath) . '">download</a> | <a href="?">restart</a><br />';