Skip to content

Commit 8faa63e

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

File tree

7 files changed

+58
-75
lines changed

7 files changed

+58
-75
lines changed

apps/cloud_federation_api/lib/Capabilities.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function getCapabilities() {
7575
// Adding a public key to the ocm discovery
7676
try {
7777
if (!$this->appConfig->getValueBool('core', OCMSignatoryManager::APPCONFIG_SIGN_DISABLED, lazy: true)) {
78+
/**
79+
* @experimental 31.0.0
80+
* @psalm-suppress UndefinedInterfaceMethod
81+
*/
7882
$this->provider->setSignatory($this->ocmSignatoryManager->getLocalSignatory());
7983
} else {
8084
$this->logger->debug('ocm public key feature disabled');

lib/private/OCM/Model/OCMProvider.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ private function looksValid(): bool {
210210
* enabled: bool,
211211
* apiVersion: '1.0-proposal1',
212212
* endPoint: string,
213-
* publicKey: Signatory|null,
213+
* publicKey: array{
214+
* keyId: string,
215+
* publicKeyPem: string
216+
* },
214217
* resourceTypes: list<array{
215218
* name: string,
216219
* shareTypes: list<string>,
@@ -230,7 +233,7 @@ public function jsonSerialize(): array {
230233
'apiVersion' => '1.0-proposal1', // deprecated, but keep it to stay compatible with old version
231234
'version' => $this->getApiVersion(), // informative but real version
232235
'endPoint' => $this->getEndPoint(),
233-
'publicKey' => $this->getSignatory(),
236+
'publicKey' => $this->getSignatory()->jsonSerialize(),
234237
'resourceTypes' => $resourceTypes
235238
];
236239
}

lib/private/OCM/OCMSignatoryManager.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,17 @@ 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+
/**
149+
* @experimental 31.0.0
150+
* @psalm-suppress UndefinedInterfaceMethod
151+
*/
152+
$signatory = $ocmProvider->getSignatory();
153+
$signatory?->setSignatoryType(SignatoryType::TRUSTED);
154+
return $signatory;
148155
} catch (OCMProviderException $e) {
149156
$this->logger->warning('fail to get remote signatory', ['exception' => $e, 'remote' => $remote]);
150157
return null;
151158
}
152159
}
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-
}
169160
}

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

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use NCU\Security\Signature\Exceptions\IdentityNotFoundException;
1414
use NCU\Security\Signature\Exceptions\IncomingRequestException;
1515
use NCU\Security\Signature\Exceptions\InvalidSignatureException;
16-
use NCU\Security\Signature\Exceptions\SignatoryException;
1716
use NCU\Security\Signature\Exceptions\SignatoryNotFoundException;
1817
use NCU\Security\Signature\Exceptions\SignatureElementNotFoundException;
1918
use NCU\Security\Signature\Exceptions\SignatureException;
@@ -53,6 +52,13 @@ public function __construct(
5352
$this->verifyHeaders();
5453
$this->extractSignatureHeader();
5554
$this->reconstructSignatureData();
55+
56+
try {
57+
// we set origin based on the keyId defined in the Signature header of the request
58+
$this->setOrigin(Signatory::extractIdentityFromUri($this->getSigningElement('keyId')));
59+
} catch (IdentityNotFoundException $e) {
60+
throw new IncomingRequestException($e->getMessage());
61+
}
5662
}
5763

5864
/**
@@ -66,21 +72,22 @@ public function __construct(
6672
* @throws SignatureNotFoundException
6773
*/
6874
private function verifyHeaders(): void {
75+
if ($this->request->getHeader('Signature') === '') {
76+
throw new SignatureNotFoundException('missing Signature in header');
77+
}
78+
6979
// confirm presence of date, content-length, digest and Signature
70-
$date = $this->getRequest()->getHeader('date');
80+
$date = $this->request->getHeader('date');
7181
if ($date === '') {
72-
throw new SignatureNotFoundException('missing date in header');
82+
throw new IncomingRequestException('missing date in header');
7383
}
74-
$contentLength = $this->getRequest()->getHeader('content-length');
84+
$contentLength = $this->request->getHeader('content-length');
7585
if ($contentLength === '') {
76-
throw new SignatureNotFoundException('missing content-length in header');
86+
throw new IncomingRequestException('missing content-length in header');
7787
}
78-
$digest = $this->getRequest()->getHeader('digest');
88+
$digest = $this->request->getHeader('digest');
7989
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');
90+
throw new IncomingRequestException('missing digest in header');
8491
}
8592

8693
// confirm date
@@ -113,7 +120,7 @@ private function verifyHeaders(): void {
113120
*/
114121
private function extractSignatureHeader(): void {
115122
$details = [];
116-
foreach (explode(',', $this->getRequest()->getHeader('Signature')) as $entry) {
123+
foreach (explode(',', $this->request->getHeader('Signature')) as $entry) {
117124
if ($entry === '' || !strpos($entry, '=')) {
118125
continue;
119126
}
@@ -139,6 +146,8 @@ private function extractSignatureHeader(): void {
139146
}
140147

141148
/**
149+
* reconstruct signature data based on signature's metadata stored in the 'Signature' header
150+
*
142151
* @throws SignatureException
143152
* @throws SignatureElementNotFoundException
144153
*/
@@ -178,27 +187,6 @@ public function getRequest(): IRequest {
178187
return $this->request;
179188
}
180189

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-
202190
/**
203191
* @inheritDoc
204192
*

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: 19 additions & 17 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
@@ -152,7 +151,10 @@ public function import(array $data): static;
152151
* enabled: bool,
153152
* apiVersion: '1.0-proposal1',
154153
* endPoint: string,
155-
* publicKey: Signatory|null,
154+
* publicKey: array{
155+
* keyId: string,
156+
* publicKeyPem: string
157+
* },
156158
* resourceTypes: list<array{
157159
* name: string,
158160
* shareTypes: list<string>,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public function getSignatoryStatus(): SignatoryStatus {
154154
*/
155155
public function setMetaValue(string $key, string|int|float|bool|array $value): void {
156156
$this->metadata[$key] = $value;
157+
$this->setter('metadata', [$this->metadata]);
157158
}
158159

159160
/**
@@ -174,7 +175,7 @@ public function jsonSerialize(): array {
174175
*
175176
* @return string
176177
* @throws IdentityNotFoundException if identity cannot be extracted
177-
* @since 31.0.0
178+
* @experimental 31.0.0
178179
*/
179180
public static function extractIdentityFromUri(string $uri): string {
180181
$identity = parse_url($uri, PHP_URL_HOST);

0 commit comments

Comments
 (0)