Skip to content

Commit ff175a1

Browse files
committed
fix(signed-request): removing unstable from public
Signed-off-by: Maxence Lange <[email protected]>
1 parent 5ac1e9a commit ff175a1

File tree

5 files changed

+40
-71
lines changed

5 files changed

+40
-71
lines changed

lib/private/OCM/OCMSignatoryManager.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,13 @@ private function generateKeyId(): string {
144144
*/
145145
public function getRemoteSignatory(string $remote): ?Signatory {
146146
try {
147-
return $this->getRemoteSignatoryFromHost($remote);
147+
$ocmProvider = $this->ocmDiscoveryService->discover($remote, true);
148+
$signatory = $ocmProvider->getSignatory();
149+
$signatory?->setSignatoryType(SignatoryType::TRUSTED);
150+
return $signatory;
148151
} catch (OCMProviderException $e) {
149152
$this->logger->warning('fail to get remote signatory', ['exception' => $e, 'remote' => $remote]);
150153
return null;
151154
}
152155
}
153-
154-
/**
155-
* As host is enough to generate signatory using OCMDiscoveryService
156-
*
157-
* @param string $host
158-
*
159-
* @return Signatory|null
160-
* @throws OCMProviderException on fail to discover ocm services
161-
* @since 31.0.0
162-
*/
163-
public function getRemoteSignatoryFromHost(string $host): ?Signatory {
164-
$ocmProvider = $this->ocmDiscoveryService->discover($host, true);
165-
$signatory = $ocmProvider->getSignatory();
166-
$signatory?->setSignatoryType(SignatoryType::TRUSTED);
167-
return $signatory;
168-
}
169156
}

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

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public function __construct(
5353
$this->verifyHeaders();
5454
$this->extractSignatureHeader();
5555
$this->reconstructSignatureData();
56+
57+
try {
58+
// we set origin based on the keyId defined in the Signature header of the request
59+
$this->setOrigin(Signatory::extractIdentityFromUri($this->getSigningElement('keyId')));
60+
} catch (IdentityNotFoundException $e) {
61+
throw new IncomingRequestException($e->getMessage());
62+
}
5663
}
5764

5865
/**
@@ -66,21 +73,22 @@ public function __construct(
6673
* @throws SignatureNotFoundException
6774
*/
6875
private function verifyHeaders(): void {
76+
if ($this->request->getHeader('Signature') === '') {
77+
throw new SignatureNotFoundException('missing Signature in header');
78+
}
79+
6980
// confirm presence of date, content-length, digest and Signature
70-
$date = $this->getRequest()->getHeader('date');
81+
$date = $this->request->getHeader('date');
7182
if ($date === '') {
72-
throw new SignatureNotFoundException('missing date in header');
83+
throw new IncomingRequestException('missing date in header');
7384
}
74-
$contentLength = $this->getRequest()->getHeader('content-length');
85+
$contentLength = $this->request->getHeader('content-length');
7586
if ($contentLength === '') {
76-
throw new SignatureNotFoundException('missing content-length in header');
87+
throw new IncomingRequestException('missing content-length in header');
7788
}
78-
$digest = $this->getRequest()->getHeader('digest');
89+
$digest = $this->request->getHeader('digest');
7990
if ($digest === '') {
80-
throw new SignatureNotFoundException('missing digest in header');
81-
}
82-
if ($this->getRequest()->getHeader('Signature') === '') {
83-
throw new SignatureNotFoundException('missing Signature in header');
91+
throw new IncomingRequestException('missing digest in header');
8492
}
8593

8694
// confirm date
@@ -113,7 +121,7 @@ private function verifyHeaders(): void {
113121
*/
114122
private function extractSignatureHeader(): void {
115123
$details = [];
116-
foreach (explode(',', $this->getRequest()->getHeader('Signature')) as $entry) {
124+
foreach (explode(',', $this->request->getHeader('Signature')) as $entry) {
117125
if ($entry === '' || !strpos($entry, '=')) {
118126
continue;
119127
}
@@ -139,6 +147,8 @@ private function extractSignatureHeader(): void {
139147
}
140148

141149
/**
150+
* reconstruct signature data based on signature's metadata stored in the 'Signature' header
151+
*
142152
* @throws SignatureException
143153
* @throws SignatureElementNotFoundException
144154
*/
@@ -178,27 +188,6 @@ public function getRequest(): IRequest {
178188
return $this->request;
179189
}
180190

181-
/**
182-
* @inheritDoc
183-
*
184-
* @param Signatory $signatory
185-
*
186-
* @return $this
187-
* @throws IdentityNotFoundException
188-
* @throws IncomingRequestException
189-
* @throws SignatoryException
190-
* @since 31.0.0
191-
*/
192-
public function setSignatory(Signatory $signatory): self {
193-
$identity = \OCP\Server::get(ISignatureManager::class)->extractIdentityFromUri($signatory->getKeyId());
194-
if ($identity !== $this->getOrigin()) {
195-
throw new SignatoryException('keyId from provider is different from the one from signed request');
196-
}
197-
198-
parent::setSignatory($signatory);
199-
return $this;
200-
}
201-
202191
/**
203192
* @inheritDoc
204193
*

lib/private/Security/Signature/SignatureManager.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ public function getIncomingSignedRequest(
102102

103103
// generate IncomingSignedRequest based on body and request
104104
$signedRequest = new IncomingSignedRequest($body, $this->request, $options);
105-
try {
106-
// we set origin based on the keyId defined in the Signature header of the request
107-
$signedRequest->setOrigin($this->extractIdentityFromUri($signedRequest->getSigningElement('keyId')));
108-
} catch (IdentityNotFoundException $e) {
109-
throw new IncomingRequestException($e->getMessage());
110-
}
111105

112106
try {
113107
// confirm the validity of content and identity of the incoming request

lib/public/OCM/IOCMProvider.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace OCP\OCM;
1010

1111
use JsonSerializable;
12-
use NCU\Security\Signature\Model\Signatory;
1312
use OCP\OCM\Exceptions\OCMArgumentException;
1413
use OCP\OCM\Exceptions\OCMProviderException;
1514

@@ -120,21 +119,21 @@ public function getResourceTypes(): array;
120119
*/
121120
public function extractProtocolEntry(string $resourceName, string $protocol): string;
122121

123-
/**
124-
* store signatory (public/private key pair) to sign outgoing/incoming request
125-
*
126-
* @param Signatory $signatory
127-
* @since 31.0.0
128-
*/
129-
public function setSignatory(Signatory $signatory): void;
130-
131-
/**
132-
* signatory (public/private key pair) used to sign outgoing/incoming request
133-
*
134-
* @return Signatory|null returns null if no Signatory available
135-
* @since 31.0.0
136-
*/
137-
public function getSignatory(): ?Signatory;
122+
// /**
123+
// * store signatory (public/private key pair) to sign outgoing/incoming request
124+
// *
125+
// * @param Signatory $signatory
126+
// * @experimental 31.0.0
127+
// */
128+
// public function setSignatory(Signatory $signatory): void;
129+
130+
// /**
131+
// * signatory (public/private key pair) used to sign outgoing/incoming request
132+
// *
133+
// * @return Signatory|null returns null if no Signatory available
134+
// * @experimental 31.0.0
135+
// */
136+
// public function getSignatory(): ?Signatory;
138137

139138
/**
140139
* import data from an array

lib/unstable/Security/Signature/Model/Signatory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function jsonSerialize(): array {
174174
*
175175
* @return string
176176
* @throws IdentityNotFoundException if identity cannot be extracted
177-
* @since 31.0.0
177+
* @experimental 31.0.0
178178
*/
179179
public static function extractIdentityFromUri(string $uri): string {
180180
$identity = parse_url($uri, PHP_URL_HOST);

0 commit comments

Comments
 (0)