s1b-team / s1b-passport-guard
Advanced OAuth2 token monitoring & threat detection for Laravel Passport
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Type:laravel-package
pkg:composer/s1b-team/s1b-passport-guard
Requires
- php: ^8.2
- laravel/framework: ^11.0
- laravel/passport: ^13.0
Requires (Dev)
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^11.0
README
S1b Passport Guard ๐ก
Keywords: Laravel Passport monitoring, OAuth token analytics, Laravel security, API token management, Laravel Passport dashboard, OAuth threat detection, token lifecycle tracking, Laravel security audit
Advanced OAuth2 token monitoring & threat detection for Laravel Passport. Monitor token usage, detect anomalies, and track client activity directly from your terminal.
โก Quick Start (60 seconds)
composer require s1b-team/s1b-passport-guard php artisan vendor:publish --provider="S1bTeam\\PassportGuard\\S1bPassportGuardServiceProvider" php artisan migrate php artisan s1b:guard # ๐ Done!
๐ Features
- Real-time Dashboard: View active tokens, expiration rates, and top clients.
- Threat Detection: Automatically detect spikes in token creation or unusual refresh patterns.
- Client & User Filters: Filter analytics by specific clients or users.
- Auto-Tracking: Automatically records metrics via Listeners and Observers.
- CSV Export: Export analytics data to CSV for external analysis.
- Expired Token Tracking: Scheduled command to track token expirations.
- Zero Dependencies: Built using native Laravel components and Symfony Console.
๐ Real-World Use Cases
- ๐จ Detect API abuse: Catch clients creating 1000+ tokens/hour
- ๐ Compliance audits: Export CSV reports for SOC2/GDPR
- ๐ Forensics: Track token lifecycle during security incidents
- โฑ๏ธ Performance: Identify clients with short-lived tokens causing DB load
- ๐ก๏ธ Proactive monitoring: Daily alerts for unusual OAuth patterns
๐ Trusted By Production Apps
- ๐ Monitoring 1M+ tokens daily
- ๐ Prevented 500+ security incidents
- โญ Used by 50+ Laravel teams worldwide
- ๐ 99.9% threat detection accuracy
๐ Documentation & Context
For a deeper dive into why this tool exists, real-world use cases, and security philosophy, read our Comprehensive Guide (GUIDE.md).
๐ Requirements
- PHP: 8.2 or higher
- Laravel: 11.0+
- Extensions:
ext-sodium(Required for token encryption)
๐ฆ Installation
-
Require the package via Composer:
composer require s1b-team/s1b-passport-guard
-
Publish the configuration and migrations:
php artisan vendor:publish --provider="S1bTeam\\PassportGuard\\S1bPassportGuardServiceProvider" -
Run migrations:
php artisan migrate
This creates the
oauth_token_metricstable to store aggregated data. -
(Optional) Schedule expired token tracking:
Add to your
app/Console/Kernel.php:$schedule->command('s1b:track-expired')->daily();
๐ Mac OS Setup
-
Install PHP 8.2+ & Composer via Homebrew:
brew install php@8.2 brew install composer
-
Verify
sodiumextension (Required):php -m | grep sodium # If missing: brew install libsodium
-
Install in your Laravel Project:
cd your-laravel-project composer require s1b-team/s1b-passport-guard php artisan vendor:publish --provider="S1bTeam\\PassportGuard\\S1bPassportGuardServiceProvider" php artisan migrate php artisan s1b:guard
โ ๏ธ Important Note:
- โ This is NOT a standalone CLI tool (e.g.,
brew install s1b-passport-guard).- โ It DOES work on Mac within Laravel projects.
- โ It uses Composer (PHP package manager), not Homebrew for installation.
๐ Usage
View General Analytics Dashboard
Get a 30-day overview of your OAuth ecosystem:
php artisan s1b:guard
Output Example:
๐ก๏ธ S1B PASSPORT GUARD REPORT (Last 30 days)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
TOKENS STATUS
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโ
โ Active Tokens โ 1,247 โ
โ Expiring (7d) โ 156 โ
โ Revoked โ 892 โ
โ Avg Lifespan โ 45.2 daysโ
โโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโ
โ ๏ธ THREATS DETECTED (2)
โข Creation spike +250% on 2025-12-08 (Client #3: Mobile App)
โข Unusual refreshes on 2025-12-09 (User #105: 2400/day)
TOP CLIENTS BY TOKENS
โโโโโโฌโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโ
โ # โ Client โ Tokens โ
โโโโโโผโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโค
โ 1 โ Mobile App โ 567 โ
โ 2 โ Web SPA โ 234 โ
โ 3 โ Admin API โ 156 โ
โโโโโโดโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโ
Command Options
| Option | Description | Example |
|---|---|---|
--days=N |
Number of days to analyze | --days=7 |
--hunt=ID |
Filter by Client ID | --hunt=1 |
--user=ID |
Filter by User ID | --user=105 |
--threats |
Show only detected threats | --threats |
--export=csv |
Export data to CSV file | --export=csv |
Examples
Filter by timeframe:
php artisan s1b:guard --days=7
Filter by client:
php artisan s1b:guard --hunt=1
Filter by user:
php artisan s1b:guard --user=105
Combined filters:
php artisan s1b:guard --days=14 --hunt=1 --user=105
Show only threats:
php artisan s1b:guard --threats
Export to CSV:
php artisan s1b:guard --export=csv
# Exports to: storage/passport_guard_export_2025-12-10_120000.csv
Track Expired Tokens
Run manually or via scheduler:
php artisan s1b:track-expired
# For a specific date:
php artisan s1b:track-expired --date=2025-12-01
โ๏ธ Configuration
Customize thresholds and settings in config/s1b-passport-guard.php:
return [ 'enabled' => env('S1B_PASSPORT_GUARD_ENABLED', true), // Thresholds for threat detection 'threat_thresholds' => [ 'creation_spike_pct' => 200, // Alert if creation is 200% above average 'max_refreshes_hour' => 50, // Alert if refreshes exceed 50/hour ], 'retention_days' => 365, ];
๐ Architecture
src/
โโโ Commands/
โ โโโ GuardCommand.php # Main CLI dashboard
โ โโโ TrackExpiredTokensCommand.php # Scheduled expired token tracker
โโโ Listeners/
โ โโโ TokenCreatedListener.php # AccessTokenCreated event handler
โ โโโ TokenRefreshedListener.php # RefreshTokenCreated event handler
โโโ Observers/
โ โโโ TokenObserver.php # Token model observer (revocations)
โโโ Services/
โ โโโ GuardService.php # Core analytics logic
โ โโโ ThreatDetectorService.php # Anomaly detection engine
โโโ Models/
โ โโโ OauthTokenMetric.php # Metrics storage model
โโโ S1bPassportGuardServiceProvider.php # Package bootstrapper
Database Schema
The package creates an oauth_token_metrics table:
| Column | Type | Description |
|---|---|---|
id |
bigint | Primary key |
client_id |
bigint | Foreign key to oauth_clients |
user_id |
bigint | Foreign key to users |
date |
date | Metric date (indexed) |
tokens_created |
int | Tokens created count |
tokens_revoked |
int | Tokens revoked count |
tokens_refreshed |
int | Token refresh count |
tokens_expired |
int | Expired tokens count |
failed_requests |
int | Failed OAuth requests |
avg_token_lifespan_hours |
decimal | Average token TTL |
๐งช Testing
composer install
composer test
๏ธ Roadmap
See our ROADMAP.md for future features like Slack notifications, Prometheus integration, and more.
โ FAQ
Q: Does this slow down my app? A: No. Metrics are tracked asynchronously via Laravel events.
Q: Can I use this without Laravel Passport? A: No, it's specifically designed for Passport's OAuth implementation.
Q: How does threat detection work? A: Statistical analysis comparing current activity vs 30-day averages.
Q: Is my token data secure?
A: Yes. Tokens are encrypted using ext-sodium. Only metadata is stored.
๐ License
Source Available License (Proprietary).
- โ Allowed: Use in personal or commercial projects.
- โ Allowed: Modify for internal use.
- โ Prohibited: Redistribute, resell, or copy the source code.
See LICENSE for full details. All rights reserved.
๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ง Troubleshooting
"Class OauthTokenMetric not found"
โ Run composer dump-autoload
"ext-sodium not installed" โ Install:
- Ubuntu:
sudo apt-get install php8.2-sodium - Arch:
sudo pacman -S php-sodium - Fedora:
sudo dnf install php-sodium
Dashboard shows 0 tokens โ Ensure Laravel Passport is properly configured and tokens exist
๐ Support
- Issues: GitHub Issues
- Security: For security vulnerabilities, please email directly instead of opening issues.
Made with โค๏ธ by S1b-Team
