Skip to content

Commit 0500408

Browse files
committed
refact: Self-sign local and free licenses
1 parent c1e2d0b commit 0500408

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

src/Cms/License.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function isComplete(): bool
164164
$this->domain !== null &&
165165
$this->email !== null &&
166166
$this->order !== null &&
167-
($this->signature !== null || $this->isFreeAndLocal()) &&
167+
$this->signature !== null &&
168168
$this->hasValidEmailAddress() === true &&
169169
$this->type() !== LicenseType::Invalid
170170
) {
@@ -272,22 +272,21 @@ public function isOnCorrectDomain(): bool
272272
*/
273273
public function isSigned(): bool
274274
{
275-
// locally self-signed licenses do not need a signature
276-
if ($this->isFreeAndLocal() === true) {
277-
return true;
278-
}
279-
280275
if ($this->signature === null) {
281276
return false;
282277
}
283278

279+
$data = json_encode($this->signatureData());
280+
$signature = hex2bin($this->signature);
281+
282+
if ($this->isFreeAndLocal() === true) {
283+
return hash('sha256', $data) === $signature;
284+
}
285+
284286
// get the public key
285287
$pubKey = F::read($this->kirby->root('kirby') . '/kirby.pub');
286288

287289
// verify the license signature
288-
$data = json_encode($this->signatureData());
289-
$signature = hex2bin($this->signature);
290-
291290
return openssl_verify($data, $signature, $pubKey, 'RSA-SHA256') === 1;
292291
}
293292

@@ -406,14 +405,14 @@ public function register(): static
406405
}
407406

408407
if ($this->isFreeAndLocal() === true) {
409-
$response = [
408+
$response = $this->selfsign([
410409
'activation' => date('Y-m-d H:i:s'),
410+
'code' => $this->code,
411411
'date' => date('Y-m-d H:i:s'),
412412
'domain' => $this->domain,
413-
'code' => $this->code,
413+
'email' => hash('sha256', $this->email . static::SALT),
414414
'order' => '12345678',
415-
'email' => $this->email
416-
];
415+
]);
417416
}
418417

419418
// @codeCoverageIgnoreStart
@@ -496,6 +495,19 @@ public function save(): bool
496495
);
497496
}
498497

498+
/**
499+
* Self-signs a license file where registration
500+
* will not communicate with the license hub
501+
*/
502+
protected function selfsign(array $payload): array
503+
{
504+
return [
505+
...$payload,
506+
'email' => $payload['email'],
507+
'signature' => bin2hex(hash('sha256', json_encode($payload)))
508+
];
509+
}
510+
499511
/**
500512
* Returns the signature if available
501513
*/

tests/Cms/System/LicenseTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -292,21 +292,6 @@ public function testIsOnCorrectDomain(): void
292292
$this->assertTrue($license->isOnCorrectDomain());
293293
}
294294

295-
public function testIsSignedFreeAndLocal(): void
296-
{
297-
$this->app->clone([
298-
'server' => [
299-
'REMOTE_ADDR' => '127.0.0.1',
300-
]
301-
]);
302-
303-
$license = new License(
304-
code: LicenseType::Free->prefix()
305-
);
306-
307-
$this->assertTrue($license->isSigned());
308-
}
309-
310295
public function testLabel(): void
311296
{
312297
$license = new License();

0 commit comments

Comments
 (0)