dracoder/trusted-timestamping
Installation
composer require dracoder/trusted-timestamping
Add to config/app.php under providers:
Dracoder\TrustedTimestamping\TrustedTimestampingServiceProvider::class,
Publish Config
php artisan vendor:publish --provider="Dracoder\TrustedTimestamping\TrustedTimestampingServiceProvider"
Configure config/trusted-timestamping.php with your timestamping service (e.g., tsa URL, credentials).
First Use Case: Timestamp a File
use Dracoder\TrustedTimestamping\Facades\TrustedTimestamping;
$filePath = storage_path('app/example.pdf');
$timestampToken = TrustedTimestamping::timestamp($filePath);
// $timestampToken now contains the signed timestamp response.
Timestamping Files
$token = TrustedTimestamping::timestamp($filePath);
$filePaths = [storage_path('file1.pdf'), storage_path('file2.pdf')];
foreach ($filePaths as $file) {
$token = TrustedTimestamping::timestamp($file);
// Store $token in DB or filesystem.
}
Verifying Timestamps
$isValid = TrustedTimestamping::verify($timestampToken, $filePath);
Integration with Storage
Storage::disk('local')->path() to get file paths for timestamping.$file = Storage::disk('s3')->get('remote-file.pdf');
$localPath = tempnam(sys_get_temp_dir(), 'tt_');
file_put_contents($localPath, $file);
$token = TrustedTimestamping::timestamp($localPath);
unlink($localPath); // Cleanup
Event-Based Timestamping
HasFile events:
use Illuminate\Support\Facades\Event;
use Dracoder\TrustedTimestamping\Facades\TrustedTimestamping;
Event::listen('eloquent.saved: App\Models\Document', function ($model) {
if ($model->file_path) {
$token = TrustedTimestamping::timestamp($model->file_path);
$model->timestamp_token = $token;
$model->save();
}
});
Queueing Timestamp Jobs
use Dracoder\TrustedTimestamping\Jobs\TimestampFile;
dispatch(new TimestampFile($filePath))->onQueue('timestamping');
File Size Limits
try {
$token = TrustedTimestamping::timestamp($filePath);
} catch (\Dracoder\TrustedTimestamping\Exceptions\FileTooLargeException $e) {
// Split file or notify user.
}
Network Dependencies
TrustedTimestamping::setRetryPolicy(3); // Retry 3 times.
Token Storage
// Use a text column in DB or compress before storing.
$compressedToken = gzcompress($timestampToken);
Clock Skew
$isValid = TrustedTimestamping::verify($token, $filePath, 60); // 60-second tolerance.
Enable Logging
Configure config/trusted-timestamping.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Mocking for Tests
Use the MockTimestampService in tests:
$this->app->bind(
\Dracoder\TrustedTimestamping\Contracts\TimestampService::class,
\Dracoder\TrustedTimestamping\MockTimestampService::class
);
Custom Timestamp Services
Implement Dracoder\TrustedTimestamping\Contracts\TimestampService for alternative providers (e.g., local HSM):
class CustomTimestampService implements TimestampService {
public function timestamp(string $filePath): string {
// Custom logic.
}
}
Pre/Post-Processing Hooks Extend the service provider to add hooks:
// In a custom service provider:
$this->app->afterResolving(\Dracoder\TrustedTimestamping\Facades\TrustedTimestamping::class, function ($service) {
$service->extend(function ($token) {
// Modify token before returning.
return $token;
});
});
Custom Verification Rules Override verification logic in a macro:
TrustedTimestamping::macro('verifyWithTolerance', function ($token, $filePath, $tolerance = 60) {
return $this->verify($token, $filePath, $tolerance);
});
HTTPS Requirement
Ensure config/trusted-timestamping.php uses HTTPS for TSA endpoints to avoid mixed-content warnings.
Caching Tokens Cache verified tokens to avoid repeated TSA calls:
Cache::remember("tt_{$filePath}", now()->addHours(24), function () use ($filePath) {
return TrustedTimestamping::timestamp($filePath);
});
Environment-Specific TSAs Use environment variables for dynamic TSA URLs:
'tsa_url' => env('TSA_URL', 'https://default-tsa.example.com'),
How can I help you explore Laravel packages today?