Skip to content

Commit 5814b15

Browse files
committed
feat: enhance license verification with detailed failure reasons
1 parent 1a5249a commit 5814b15

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

src/Http/Middleware/LicenseCheck.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ public function handle(Request $request, Closure $next)
2929
], 403);
3030
}
3131

32-
$this->addWarningAlert();
32+
$this->addWarningAlert($result->getReason());
3333
}
3434

3535
return $next($request);
3636
}
3737

38-
private function addWarningAlert()
38+
private function addWarningAlert($reason = null)
3939
{
4040
FilamentView::registerRenderHook(
4141
PanelsRenderHook::BODY_START,
42-
function () {
43-
$alert = Alert::make(fn () => (string) __('inspirecms::messages.invalid_license'))
42+
function () use ($reason) {
43+
$alert = Alert::make(fn () => str_contains($reason, 'expired') ? 'License has expired. Please renew your license.' : __('inspirecms::messages.invalid_license'))
4444
->type('warn')
4545
->size('sm')
4646
->withAttributes([

src/Licensing/LicenseManager.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function canUpgrade(): bool
4242
if (($verificationResult = $this->cache()->get($cacheKey)) && $verificationResult instanceof LicenseVerificationResult) {
4343
$data = $verificationResult->getData();
4444

45-
return ($data['custom_data']['can_upgrade'] ?? false) === true;
45+
$pvSlug = data_get($data, 'meta.product_variant_slug', '');
46+
47+
return is_string($pvSlug) && $pvSlug == 'free';
4648
}
4749

4850
} catch (\Throwable $th) {
@@ -72,7 +74,7 @@ public function verify()
7274
return $offlineResult;
7375
}
7476

75-
$failedMessage = null;
77+
$failedReason = null;
7678

7779
try {
7880

@@ -82,6 +84,7 @@ public function verify()
8284
$response = Http::timeout(self::REQUEST_TIMEOUT)->post($this->fetchActionPath('validate'), $payload);
8385

8486
if ($response->successful()) {
87+
8588
$data = $response->json();
8689

8790
if ($data['valid'] === true) {
@@ -100,7 +103,7 @@ public function verify()
100103

101104
return $result;
102105
} else {
103-
$failedMessage = $data['reason'] ?? null;
106+
$failedReason = $data['reason'] ?? null;
104107
}
105108
}
106109

@@ -111,9 +114,8 @@ public function verify()
111114
}
112115

113116
return LicenseVerificationResult::failureOnline(
114-
str('License verification failed')->finish(
115-
$failedMessage ? ": {$failedMessage}" : ''
116-
)->toString(),
117+
message: 'License verification failed',
118+
reason: $failedReason,
117119
);
118120
}
119121

@@ -140,7 +142,7 @@ public function getSupportEmail(): ?string
140142
protected function verifyOffline()
141143
{
142144
if (! $this->usingLicenseKeyFile()) {
143-
return LicenseVerificationResult::failureOffline('License file not found');
145+
return LicenseVerificationResult::failureOffline(message: 'License file not found');
144146
}
145147

146148
try {
@@ -153,7 +155,7 @@ protected function verifyOffline()
153155

154156
logger()->warning('Failed to read license file', ['exception' => $th]);
155157

156-
return LicenseVerificationResult::failureOffline('Failed to read license file');
158+
return LicenseVerificationResult::failureOffline(message: 'Failed to read license file');
157159

158160
}
159161
}
@@ -162,22 +164,22 @@ protected function dataVerification($licenseData): ?LicenseVerificationResult
162164
{
163165
// Verify the license key is the same
164166
if ($licenseData['license_key'] !== $this->getLicenseKey()) {
165-
return LicenseVerificationResult::failureOffline('The license key in the file does not match the configured license key');
167+
return LicenseVerificationResult::failureOffline(reason: 'The license key in the file does not match the configured license key');
166168
}
167169

168170
// Verify the data is for the current domain
169171
if ($licenseData['domain'] !== $this->getCurrentDomain()) {
170-
return LicenseVerificationResult::failureOffline('License file does not match the current domain');
172+
return LicenseVerificationResult::failureOffline(reason: 'domain_mismatch');
171173
}
172174

173175
// Verify the license is not expired
174176
if (Carbon::parse($licenseData['expiry_date'])->isPast(Carbon::now('UTC'))) {
175-
return LicenseVerificationResult::failureOffline('License expired');
177+
return LicenseVerificationResult::failureOffline(reason: 'expired');
176178
}
177179

178180
// Verify the checksum
179181
if ($licenseData['checksum'] !== $this->calculateChecksum(Arr::except($licenseData, 'checksum'))) {
180-
return LicenseVerificationResult::failureOffline('License file verification failed due to checksum mismatch');
182+
return LicenseVerificationResult::failureOffline(reason: 'checksum mismatch');
181183
}
182184

183185
return null;

src/Licensing/LicenseVerificationResult.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ class LicenseVerificationResult
77
/**
88
* @param bool $isSuccess Whether the license verification was successful
99
* @param string $message The verification message or error details
10+
* @param ?string $reason Optional reason for failure, if applicable
1011
* @param bool $isOnline Whether the license verification was performed online
1112
*/
1213
public function __construct(
1314
protected bool $isSuccess,
1415
protected string $message = '',
16+
protected ?string $reason = null,
1517
protected bool $isOnline = false,
1618
/** @var ?array */
17-
protected $data = null
19+
protected $data = null,
1820
) {
1921
if (! is_array($data)) {
2022
unset($this->data);
@@ -42,36 +44,41 @@ public function getData(): ?array
4244
return $this->data;
4345
}
4446

47+
public function getReason(): ?string
48+
{
49+
return $this->reason;
50+
}
51+
4552
/**
4653
* Create a successful online verification result.
4754
*/
4855
public static function successOnline(?string $message = null, $data = null): static
4956
{
50-
return new static(true, $message ?? 'License is valid (online)', true, $data);
57+
return new static(isSuccess: true, message: $message ?? 'License is valid (online)', isOnline: true, data: $data);
5158
}
5259

5360
/**
5461
* Create a successful offline verification result.
5562
*/
5663
public static function successOffline(?string $message = null, $data = null): static
5764
{
58-
return new static(true, $message ?? 'License is valid (offline)', false, $data);
65+
return new static(isSuccess: true, message: $message ?? 'License is valid (offline)', isOnline: false, data: $data);
5966
}
6067

6168
/**
6269
* Create a failed online verification result.
6370
*/
64-
public static function failureOnline(string $message, $data = null): static
71+
public static function failureOnline(?string $message = null, $reason = null, $data = null): static
6572
{
66-
return new static(false, $message, true, $data);
73+
return new static(isSuccess: false, message: $message ?? 'License verification failed (online)', reason: $reason, isOnline: true, data: $data);
6774
}
6875

6976
/**
7077
* Create a failed offline verification result.
7178
*/
72-
public static function failureOffline(string $message, $data = null): static
79+
public static function failureOffline(?string $message = null, $reason = null, $data = null): static
7380
{
74-
return new static(false, $message, false, $data);
81+
return new static(isSuccess: false, message: $message ?? 'License verification failed (offline)', reason: $reason, isOnline: false, data: $data);
7582
}
7683

7784
/**

0 commit comments

Comments
 (0)