Skip to content

Commit 15b7228

Browse files
committed
fix(signatory): details on interfaces
Signed-off-by: Maxence Lange <[email protected]>
1 parent 4df3155 commit 15b7228

File tree

7 files changed

+49
-50
lines changed

7 files changed

+49
-50
lines changed

lib/private/Security/Signature/Model/IncomingSignedRequest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OC\Security\Signature\Model;
1010

1111
use JsonSerializable;
12+
use NCU\Security\Signature\Enum\DigestAlgorithm;
1213
use NCU\Security\Signature\Enum\SignatureAlgorithm;
1314
use NCU\Security\Signature\Exceptions\IdentityNotFoundException;
1415
use NCU\Security\Signature\Exceptions\IncomingRequestException;
@@ -22,6 +23,7 @@
2223
use NCU\Security\Signature\Model\Signatory;
2324
use OC\Security\Signature\SignatureManager;
2425
use OCP\IRequest;
26+
use ValueError;
2527

2628
/**
2729
* @inheritDoc
@@ -107,6 +109,12 @@ private function verifyHeaders(): void {
107109
}
108110

109111
// confirm digest value, based on body
112+
[$algo, ] = explode('=', $digest);
113+
try {
114+
$this->setDigestAlgorithm(DigestAlgorithm::from($algo));
115+
} catch (ValueError) {
116+
throw new IncomingRequestException('unknown digest algorithm');
117+
}
110118
if ($digest !== $this->getDigest()) {
111119
throw new IncomingRequestException('invalid value for digest in header');
112120
}
@@ -188,15 +196,14 @@ public function getRequest(): IRequest {
188196
}
189197

190198
/**
191-
* @inheritDoc
199+
* set the hostname at the source of the request,
200+
* based on the keyId defined in the signature header.
192201
*
193202
* @param string $origin
194-
* @return IIncomingSignedRequest
195203
* @since 31.0.0
196204
*/
197-
public function setOrigin(string $origin): IIncomingSignedRequest {
205+
private function setOrigin(string $origin): void {
198206
$this->origin = $origin;
199-
return $this;
200207
}
201208

202209
/**

lib/private/Security/Signature/Model/SignedRequest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ public function getBody(): string {
4444
}
4545

4646
/**
47-
* @inheritDoc
47+
* set algorithm used to generate digest
4848
*
4949
* @param DigestAlgorithm $algorithm
5050
*
5151
* @return self
5252
* @since 31.0.0
5353
*/
54-
public function setDigestAlgorithm(DigestAlgorithm $algorithm): self {
54+
protected function setDigestAlgorithm(DigestAlgorithm $algorithm): self {
55+
$this->digestAlgorithm = $algorithm;
5556
return $this;
5657
}
5758

@@ -119,14 +120,14 @@ public function getSigningElement(string $key): string { // getSignatureDetail /
119120
}
120121

121122
/**
122-
* @inheritDoc
123+
* store data used to generate signature
123124
*
124125
* @param array $data
125126
*
126127
* @return self
127128
* @since 31.0.0
128129
*/
129-
public function setSignatureData(array $data): self {
130+
protected function setSignatureData(array $data): self {
130131
$this->signatureData = $data;
131132
return $this;
132133
}
@@ -142,14 +143,14 @@ public function getSignatureData(): array {
142143
}
143144

144145
/**
145-
* @inheritDoc
146+
* set the signed version of the signature
146147
*
147148
* @param string $signature
148149
*
149150
* @return self
150151
* @since 31.0.0
151152
*/
152-
public function setSignature(string $signature): self {
153+
protected function setSignature(string $signature): self {
153154
$this->signature = $signature;
154155
return $this;
155156
}

lib/unstable/Security/Signature/IIncomingSignedRequest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
* model wrapping an actual incoming request, adding details about the signature and the
1818
* authenticity of the origin of the request.
1919
*
20+
* This interface must not be implemented in your application but
21+
* instead obtained from {@see ISignatureManager::getIncomingSignedRequest}.
22+
*
23+
* ```php
24+
* $signedRequest = $this->signatureManager->getIncomingSignedRequest($mySignatoryManager);
25+
* ```
26+
*
2027
* @see ISignatureManager for details on signature
2128
* @experimental 31.0.0
2229
*/
@@ -29,16 +36,6 @@ interface IIncomingSignedRequest extends ISignedRequest {
2936
*/
3037
public function getRequest(): IRequest;
3138

32-
/**
33-
* set the hostname at the source of the request,
34-
* based on the keyId defined in the signature header.
35-
*
36-
* @param string $origin
37-
* @return IIncomingSignedRequest
38-
* @experimental 31.0.0
39-
*/
40-
public function setOrigin(string $origin): IIncomingSignedRequest;
41-
4239
/**
4340
* get the hostname at the source of the base request.
4441
* based on the keyId defined in the signature header.

lib/unstable/Security/Signature/IOutgoingSignedRequest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
/**
1616
* extends ISignedRequest to add info requested at the generation of the signature
1717
*
18+
* This interface must not be implemented in your application but
19+
* instead obtained from {@see ISignatureManager::getIncomingSignedRequest}.
20+
*
21+
* ```php
22+
* $signedRequest = $this->signatureManager->getIncomingSignedRequest($mySignatoryManager);
23+
* ```
24+
*
1825
* @see ISignatureManager for details on signature
1926
* @experimental 31.0.0
2027
*/

lib/unstable/Security/Signature/ISignatoryManager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* - signing outgoing request
1616
* - confirm the authenticity of incoming signed request.
1717
*
18+
* This interface must be implemented to generate a `SignatoryManager` to
19+
* be used with {@see ISignatureManager}
20+
*
1821
* @experimental 31.0.0
1922
*/
2023
interface ISignatoryManager {

lib/unstable/Security/Signature/ISignatureManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
* listed in 'headers' and their value. Some elements (content-length date digest host) are mandatory
4242
* to ensure authenticity override protection.
4343
*
44+
* This interface can be used to inject {@see SignatureManager} in your code:
45+
*
46+
* ```php
47+
* public function __construct(
48+
* private ISignatureManager $signatureManager,
49+
* ) {}
50+
* ```
51+
*
52+
* instead obtained from {@see ISignatureManager::getIncomingSignedRequest}.
53+
*
4454
* @experimental 31.0.0
4555
*/
4656
interface ISignatureManager {

lib/unstable/Security/Signature/ISignedRequest.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
* - to confirm authenticity of a signed incoming request
2020
* - to sign an outgoing request
2121
*
22+
* This interface must not be implemented in your application:
23+
* @see IIncomingSignedRequest
24+
* @see IOutgoingSignedRequest
25+
*
2226
* @experimental 31.0.0
2327
*/
2428
interface ISignedRequest {
@@ -30,16 +34,6 @@ interface ISignedRequest {
3034
*/
3135
public function getBody(): string;
3236

33-
/**
34-
* set algorithm used to generate digest
35-
*
36-
* @param DigestAlgorithm $algorithm
37-
*
38-
* @return self
39-
* @experimental 31.0.0
40-
*/
41-
public function setDigestAlgorithm(DigestAlgorithm $algorithm): self;
42-
4337
/**
4438
* get algorithm used to generate digest
4539
*
@@ -83,16 +77,6 @@ public function getSigningElements(): array;
8377
*/
8478
public function getSigningElement(string $key): string;
8579

86-
/**
87-
* store data used to generate signature
88-
*
89-
* @param array $data
90-
*
91-
* @return self
92-
* @experimental 31.0.0
93-
*/
94-
public function setSignatureData(array $data): self;
95-
9680
/**
9781
* returns data used to generate signature
9882
*
@@ -101,16 +85,6 @@ public function setSignatureData(array $data): self;
10185
*/
10286
public function getSignatureData(): array;
10387

104-
/**
105-
* set the signed version of the signature
106-
*
107-
* @param string $signature
108-
*
109-
* @return self
110-
* @experimental 31.0.0
111-
*/
112-
public function setSignature(string $signature): self;
113-
11488
/**
11589
* get the signed version of the signature
11690
*

0 commit comments

Comments
 (0)