An open API service indexing awesome lists of open source software.

https://github.com/ind4skylivey/s1bpassport-guard

Advanced OAuth2 token monitoring, threat detection & forensics for Laravel Passport. Track tokens, detect brute-force attacks, analyze client activity from CLI.
https://github.com/ind4skylivey/s1bpassport-guard

analitics api-security audit authentication cli cybersecurity devops forensics laravel laravel-package laravel-passport monitoring outh2 php red-team security symfony-console threat-detection token-management

Last synced: 5 months ago
JSON representation

Advanced OAuth2 token monitoring, threat detection & forensics for Laravel Passport. Track tokens, detect brute-force attacks, analyze client activity from CLI.

Awesome Lists containing this project

README

          

![S1b Passport Guard Banner](.github/assets/banner.png)

# S1b Passport Guard ๐Ÿ›ก

![Laravel](https://img.shields.io/badge/Laravel-11.x-red)
![PHP](https://img.shields.io/badge/PHP-8.2+-blue)
![License](https://img.shields.io/badge/license-MIT-green)
![OAuth](https://img.shields.io/badge/OAuth-2.0-orange)
[![Latest Version](https://img.shields.io/packagist/v/s1b-team/s1b-passport-guard)](https://packagist.org/packages/s1b-team/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)

```bash
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)](GUIDE.md)**.

## ๐Ÿ“‹ Requirements

- **PHP:** 8.2 or higher
- **Laravel:** 11.0+
- **Extensions:** `ext-sodium` (Required for token encryption)

## ๐Ÿ“ฆ Installation

1. **Require the package via Composer:**

```bash
composer require s1b-team/s1b-passport-guard
```

2. **Publish the configuration and migrations:**

```bash
php artisan vendor:publish --provider="S1bTeam\\PassportGuard\\S1bPassportGuardServiceProvider"
```

3. **Run migrations:**

```bash
php artisan migrate
```

_This creates the `oauth_token_metrics` table to store aggregated data._

4. **(Optional) Schedule expired token tracking:**

Add to your `app/Console/Kernel.php`:

```php
$schedule->command('s1b:track-expired')->daily();
```

### ๐ŸŽ Mac OS Setup

1. **Install PHP 8.2+ & Composer via Homebrew:**

```bash
brew install php@8.2
brew install composer
```

2. **Verify `sodium` extension (Required):**

```bash
php -m | grep sodium
# If missing: brew install libsodium
```

3. **Install in your Laravel Project:**

```bash
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:

```bash
php artisan s1b:guard
```

**Output Example:**

```text
๐Ÿ›ก๏ธ 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:**

```bash
php artisan s1b:guard --days=7
```

**Filter by client:**

```bash
php artisan s1b:guard --hunt=1
```

**Filter by user:**

```bash
php artisan s1b:guard --user=105
```

**Combined filters:**

```bash
php artisan s1b:guard --days=14 --hunt=1 --user=105
```

**Show only threats:**

```bash
php artisan s1b:guard --threats
```

**Export to CSV:**

```bash
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:

```bash
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`:

```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

```bash
composer install
composer test
```

## ๏ธ Roadmap

See our [ROADMAP.md](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](LICENSE) for full details. All rights reserved.

## ๐Ÿค Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. 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](https://github.com/s1b-team/s1b-passport-guard/issues)
- **Security:** For security vulnerabilities, please email directly instead of opening issues.

---

Made with โค๏ธ by [S1b-Team](https://github.com/s1b-team)