minor syntax tweaks
This commit is contained in:
parent
0f89376fa8
commit
c7577ea492
17
README.md
17
README.md
@ -206,21 +206,6 @@ rateLimit(int $maxAttempts = 60, int $decayMinutes = 1): callable
|
|||||||
verifyCsrf(): callable
|
verifyCsrf(): callable
|
||||||
```
|
```
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
- **High Performance**: Tree-based router with O(1) lookup complexity
|
|
||||||
- **Minimal Overhead**: Lightweight design with essential features only
|
|
||||||
- **Method Chaining**: Fluent API for response building
|
|
||||||
- **Middleware Support**: Pre/post request processing
|
|
||||||
- **Route Groups**: Organize routes with shared prefixes and middleware
|
|
||||||
- **Input Validation**: Comprehensive validation with built-in rules
|
|
||||||
- **Authentication**: Session-based auth with remember tokens
|
|
||||||
- **Error Handling**: Custom error pages with debug support
|
|
||||||
- **CSRF Protection**: Built-in CSRF token generation and validation
|
|
||||||
- **Flash Messages**: One-time notifications between requests
|
|
||||||
- **Rate Limiting**: Configurable rate limiting per user/IP
|
|
||||||
- **Content Negotiation**: Automatic JSON/HTML response selection
|
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
- **[EXAMPLES.md](EXAMPLES.md)** - Comprehensive examples and usage patterns
|
- **[EXAMPLES.md](EXAMPLES.md)** - Comprehensive examples and usage patterns
|
||||||
@ -231,4 +216,4 @@ verifyCsrf(): callable
|
|||||||
|
|
||||||
- PHP 8.1+ (uses enums and modern syntax)
|
- PHP 8.1+ (uses enums and modern syntax)
|
||||||
- No external dependencies
|
- No external dependencies
|
||||||
- Works with any PHP web server
|
- Works with any PHP web server
|
||||||
|
|||||||
@ -12,9 +12,9 @@ class Auth
|
|||||||
private ?User $user = null;
|
private ?User $user = null;
|
||||||
private array $config;
|
private array $config;
|
||||||
|
|
||||||
const SESSION_KEY = 'auth_user_data';
|
public const string SESSION_KEY = 'auth_user_data';
|
||||||
const REMEMBER_COOKIE = 'remember_token';
|
public const string REMEMBER_COOKIE = 'remember_token';
|
||||||
const REMEMBER_DURATION = 2592000; // 30 days in seconds
|
public const int REMEMBER_DURATION = 2592000; // 30 days in seconds
|
||||||
|
|
||||||
public function __construct(Session $session, ?Cookies $cookie = null, array $config = [])
|
public function __construct(Session $session, ?Cookies $cookie = null, array $config = [])
|
||||||
{
|
{
|
||||||
|
|||||||
@ -111,20 +111,16 @@ class AuthMiddleware
|
|||||||
public function rateLimit(int $maxAttempts = 60, int $decayMinutes = 1): callable
|
public function rateLimit(int $maxAttempts = 60, int $decayMinutes = 1): callable
|
||||||
{
|
{
|
||||||
return function(Context $context, callable $next) use ($maxAttempts, $decayMinutes) {
|
return function(Context $context, callable $next) use ($maxAttempts, $decayMinutes) {
|
||||||
if ($this->auth->guest()) {
|
$identifier = $this->auth->guest() ? $context->request->ip() : 'user:' . $this->auth->id();
|
||||||
$identifier = $context->request->ip();
|
|
||||||
} else {
|
|
||||||
$identifier = 'user:' . $this->auth->id();
|
|
||||||
}
|
|
||||||
|
|
||||||
$key = 'rate_limit:' . $identifier . ':' . $context->request->path;
|
$key = 'rate_limit:' . $identifier . ':' . $context->request->path;
|
||||||
$attempts = $context->session->get($key, 0);
|
$attempts = $context->session->get($key, 0);
|
||||||
$resetTime = $context->session->get($key . ':reset', 0);
|
$resetTime = $context->session->get("$key:reset", 0);
|
||||||
|
|
||||||
// Reset counter if decay time has passed
|
// Reset counter if decay time has passed
|
||||||
if (time() > $resetTime) {
|
if (time() > $resetTime) {
|
||||||
$attempts = 0;
|
$attempts = 0;
|
||||||
$context->session->set($key . ':reset', time() + ($decayMinutes * 60));
|
$context->session->set("$key:reset", time() + $decayMinutes * 60);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($attempts >= $maxAttempts) {
|
if ($attempts >= $maxAttempts) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user