Skip to content

Commit f8dbf3e

Browse files
committed
Added an option to sign using SAD in addition to OTP
1 parent 9c6aa1b commit f8dbf3e

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

src/Module.php

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ class Module implements ModuleInterface, DictionaryInterface, DocumentInterface
3333
protected $certificateId;
3434

3535
/**
36+
* A Time based One Time Pin (TOTP) generated by a compatible app such as Google Authenticator, Microsoft Authenticator etc'.
3637
* @var null|string
3738
*/
3839
protected $otp;
3940

41+
/**
42+
* The Signing Activation Data (SAD) token obtained in an earlier authorise request.
43+
* @var null|string
44+
*/
45+
protected $sad;
46+
47+
/**
48+
* Signing method to be used, either 'otp' or 'sad'
49+
* Default: otp
50+
* @var string
51+
*/
52+
protected $signingMethod = 'otp';
53+
4054
/**
4155
* @var string
4256
*/
@@ -80,6 +94,26 @@ public function setOtp(string $otp): void
8094
$this->otp = $otp;
8195
}
8296

97+
public function setSad(string $sad): void
98+
{
99+
$this->sad = $sad;
100+
}
101+
102+
/**
103+
* Sets the desired signing method to use on createSignature method
104+
*/
105+
public function setSigningMethod($method)
106+
{
107+
$possible = ['otp','sad'];
108+
109+
if ( ! in_array($method, $possible ) )
110+
{
111+
throw new InvalidArgumentException('Invalid signing method! you can only choose beteen: ' . implode(',', $possible));
112+
}
113+
114+
$this->signingMethod = $method;
115+
}
116+
83117
public function getCertificate()
84118
{
85119
$padesModule = $this->_getPadesModule();
@@ -92,10 +126,6 @@ public function getCertificate()
92126

93127
public function createSignature(SetaPDF_Core_Reader_FilePath $tmpPath)
94128
{
95-
if ($this->otp === null) {
96-
throw new \BadMethodCallException('Missing otp!');
97-
}
98-
99129
// ensure certificate
100130
$certificate = $this->getCertificate();
101131
if ($certificate === null) {
@@ -107,12 +137,34 @@ public function createSignature(SetaPDF_Core_Reader_FilePath $tmpPath)
107137
$padesDigest = $padesModule->getDigest();
108138

109139
$hashData = hash($padesDigest, $padesModule->getDataToSign($tmpPath), true);
110-
$signatureValue = $this->client->signWithOtp(
111-
$this->certificateId,
112-
$this->signingAlgorithm,
113-
$hashData,
114-
$this->otp
115-
);
140+
141+
if ( $this->signingMethod == 'sad' ) {
142+
// Sign using SAD
143+
if ($this->sad === null) {
144+
throw new \BadMethodCallException('Missing SAD! Did you perform /authorize request before?');
145+
}
146+
147+
$signatureValue = $this->client->signWithSad(
148+
$this->certificateId,
149+
$this->signingAlgorithm,
150+
$hashData,
151+
$this->sad
152+
);
153+
}
154+
else {
155+
// Sign using OTP
156+
if ($this->otp === null) {
157+
throw new \BadMethodCallException('Missing OTP!');
158+
}
159+
160+
$signatureValue = $this->client->signWithOtp(
161+
$this->certificateId,
162+
$this->signingAlgorithm,
163+
$hashData,
164+
$this->otp
165+
);
166+
}
167+
116168

117169
if (\in_array($this->signingAlgorithm, ['ES256', 'ES384', 'ES512'], true)) {
118170
// THIS NEEDS TO BE USED TO FIX EC SIGNATURES

0 commit comments

Comments
 (0)