|
| 1 | +# Verify - Architecture Documentation |
| 2 | + |
| 3 | +Verify is the License management system used by Lychee to validate license keys. It provides a flexible architecture for validating licenses through either hash-based validation or cryptographic signature validation. |
| 4 | + |
| 5 | +## Core Architecture |
| 6 | + |
| 7 | +### Overall Structure |
| 8 | + |
| 9 | +``` |
| 10 | +src/ |
| 11 | +├── Contract/ # Core interfaces and enums |
| 12 | +│ ├── Status.php # License status types |
| 13 | +│ ├── ValidatorInterface.php # Validator contract |
| 14 | +│ └── VerifyInterface.php # Core verification contract |
| 15 | +├── Exceptions/ # Custom exceptions |
| 16 | +│ ├── BaseVerifyException.php |
| 17 | +│ └── SupporterOnlyOperationException.php |
| 18 | +├── Facades/ # Laravel facades |
| 19 | +│ └── VerifyFacade.php |
| 20 | +├── Http/ # HTTP components |
| 21 | +│ └── Middleware/ # Middleware for route protection |
| 22 | +├── Validators/ # License validation implementations |
| 23 | +│ ├── ValidateHash.php # Hash-based validation |
| 24 | +│ └── ValidateSignature.php # Cryptographic signature validation |
| 25 | +├── Verify.php # Main verification class |
| 26 | +└── VerifyServiceProvider.php # Laravel service provider |
| 27 | +``` |
| 28 | + |
| 29 | +### Component Description |
| 30 | + |
| 31 | +#### 1. Status System |
| 32 | + |
| 33 | +The license status is represented by the `Status` enum, which defines three levels: |
| 34 | + |
| 35 | +- `FREE_EDITION` - Basic features, no license required |
| 36 | +- `SUPPORTER_EDITION` - Standard supporter features |
| 37 | +- `PLUS_EDITION` - Premium features with extended capabilities |
| 38 | + |
| 39 | +#### 2. Validation Mechanism |
| 40 | + |
| 41 | +The plugin implements a strategy pattern for license validation through the `ValidatorInterface`: |
| 42 | + |
| 43 | +```php |
| 44 | +interface ValidatorInterface |
| 45 | +{ |
| 46 | + public function validate(string $verifiable, string $license): bool; |
| 47 | + public function grant(): Status; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +Two concrete validators are provided: |
| 52 | + |
| 53 | +1. **ValidateHash** - Uses password hashing to validate a static license key |
| 54 | + - Returns `SUPPORTER_EDITION` status when valid |
| 55 | + - Simple validation mechanism for standard supporters |
| 56 | + |
| 57 | +2. **ValidateSignature** - Uses cryptographic signatures for validation |
| 58 | + - Returns `PLUS_EDITION` status when valid |
| 59 | + - More secure mechanism for premium users |
| 60 | + - Uses asymmetric cryptography (ECDSA) for verification |
| 61 | + |
| 62 | +#### 3. Core Verify Class |
| 63 | + |
| 64 | +The `Verify` class serves as the central hub and implements `VerifyInterface`. It: |
| 65 | + |
| 66 | +- Coordinates between different validators |
| 67 | +- Provides convenience methods for checking license status |
| 68 | +- Handles authorization logic |
| 69 | +- Provides conditional execution based on license status |
| 70 | + |
| 71 | +#### 4. Service Provider & Integration |
| 72 | + |
| 73 | +The `VerifyServiceProvider` handles Laravel integration by: |
| 74 | +- Registering the Verify service in the Laravel container |
| 75 | +- Merging configuration files |
| 76 | +- Making the service available through the facade |
| 77 | + |
| 78 | +#### 5. Facade |
| 79 | + |
| 80 | +The `VerifyFacade` provides a static interface to access the Verify functionality, following the Laravel facade pattern. |
| 81 | + |
| 82 | +## Usage Examples |
| 83 | + |
| 84 | +### Basic Verification |
| 85 | + |
| 86 | +```php |
| 87 | +// Check if the user is a supporter |
| 88 | +if ($verify->is_supporter()) { |
| 89 | + // Provide supporter features |
| 90 | +} |
| 91 | + |
| 92 | +// Check if the user has premium status |
| 93 | +if ($verify->is_plus()) { |
| 94 | + // Provide premium features |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### Authorization |
| 99 | + |
| 100 | +```php |
| 101 | +// Will throw SupporterOnlyOperationException if not a supporter |
| 102 | +$verify->authorize(); |
| 103 | + |
| 104 | +// Will throw exception if not a plus user |
| 105 | +$verify->authorize(Status::PLUS_EDITION); |
| 106 | +``` |
| 107 | + |
| 108 | +### Conditional Execution |
| 109 | + |
| 110 | +```php |
| 111 | +$result = $verify->when( |
| 112 | + fn() => 'Supporter feature enabled!', |
| 113 | + fn() => 'Please upgrade to supporter edition.', |
| 114 | + Status::SUPPORTER_EDITION |
| 115 | +); |
| 116 | +``` |
| 117 | + |
| 118 | +## Configuration |
| 119 | + |
| 120 | +Configuration is stored in `config/verify.php`, which includes: |
| 121 | + |
| 122 | +- Validation configuration for integrity checks |
| 123 | +- Public keys and hash settings (in the actual implementation) |
| 124 | + |
| 125 | +## Database Integration |
| 126 | + |
| 127 | +The plugin uses the database to store: |
| 128 | +- License keys |
| 129 | +- User emails associated with licenses |
| 130 | + |
| 131 | +These are stored in the `configs` table with dedicated migration files. |
| 132 | + |
| 133 | +## Security Considerations |
| 134 | + |
| 135 | +The plugin implements multiple layers of security: |
| 136 | +1. Hash-based validation for supporter licenses |
| 137 | +2. Cryptographic signature validation for premium licenses |
| 138 | +3. Server-side validation to prevent client-side tampering |
| 139 | +4. Integrity checking of core verification files |
| 140 | + |
| 141 | +## Extension Points |
| 142 | + |
| 143 | +The architecture allows extending the system by: |
| 144 | +1. Creating new validators implementing `ValidatorInterface` |
| 145 | +2. Adding additional status levels to the `Status` enum |
| 146 | +3. Creating custom middleware for specific route protection |
| 147 | + |
| 148 | +## Integrity Validation |
| 149 | + |
| 150 | +The system includes a self-validation mechanism that verifies the integrity of core verification files to detect tampering: |
| 151 | + |
| 152 | +```php |
| 153 | +if ($verify->validate()) { |
| 154 | + // System is intact |
| 155 | +} else { |
| 156 | + // Core files have been tampered with |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +This system compares file checksums against expected values to ensure the verification system itself hasn't been compromised. |
0 commit comments