Download the PHP package sokkian/simpleauth without Composer
On this page you can find all versions of the php package sokkian/simpleauth. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sokkian/simpleauth
More information about sokkian/simpleauth
Files in sokkian/simpleauth
Package simpleauth
Short Description A lightweight, secure PHP library for passwordless authentication using magic links
License MIT
Homepage https://github.com/sokkian/simpleauth
Informations about the package simpleauth
SimpleAuth
A lightweight, secure PHP library for passwordless authentication using magic links (one-time login tokens).
Project Status
⚠️ Learning/educational project - not actively maintained. Feel free to fork and adapt it for your needs.
Features
- Passwordless authentication: Secure login via email magic links
- Replay protection: One-time use tokens with nonce validation
- Clock skew tolerance: Configurable grace period for time synchronization
- Immutable result objects: Type-safe error handling without exceptions
- Internationalization: Built-in support for multiple languages
- Zero dependencies: Pure PHP with no external libraries required
- PSR-4 compatible: Easy integration via autoloading
Requirements
- PHP 7.4 or higher
- PDO extension with MySQL support
- MySQL 5.7+ or MariaDB 10.2+
Installation
Via Composer (Recommended)
Manual Installation
- Download or clone this repository
-
Copy the
src/directory to your project: - Create an autoloader file in your project root:
autoload.php:
Database Setup
Run the SQL schema to create required tables:
Or manually create tables:
Quick Start
1. Generate a Magic Link
2. Verify the Token
Complete Testing Example
Here's a minimal working example to test the installation:
test-login.php (Request magic link)
test-verify.php (Verify token)
test-logout.php (Clear session)
Testing steps:
- Insert a test user:
INSERT INTO users (email, name) VALUES ('[email protected]', 'Test User'); - Open
test-login.phpin your browser - Enter
[email protected] - Click the magic link displayed
- Verify you're logged in
API Reference
Token Class
__construct(PDO $db)
Create a new token generator.
Parameters:
$db- PDO database connection
generate(int $user_id, int $ttlSeconds = 900): string
Generate a new magic link token.
Parameters:
$user_id- User ID from users table$ttlSeconds- Time to live in seconds (default: 900 = 15 minutes)
Returns: string - The generated token
Example:
cleanup(int $retentionWeeks = 4): array
Delete expired tokens and nonces.
Parameters:
$retentionWeeks- Keep records for this many weeks after expiration
Returns: array with keys:
tokens_deleted- Number of tokens deletednonces_deleted- Number of nonces deleted
Example:
Verifier Class
__construct(PDO $db, int $clockSkewSeconds = 120)
Create a new token verifier.
Parameters:
$db- PDO database connection$clockSkewSeconds- Clock skew tolerance in seconds (default: 120)
verify(string $token): Result
Verify a magic link token.
Parameters:
$token- The token to verify
Returns: Result object
Example:
verifyFromUrl(string $url, string $paramName = 't'): Result
Extract and verify token from URL.
Parameters:
$url- Complete URL with token parameter$paramName- Query parameter name (default: 't')
Returns: Result object
Result Class
Constants (Error Codes)
| Constant | Value | Description |
|---|---|---|
TOKEN_NOT_FOUND |
token_not_found |
Token doesn't exist in database |
TOKEN_EXPIRED |
token_expired |
Token has expired |
TOKEN_ALREADY_USED |
token_already_used |
Replay attack detected |
MISSING_TOKEN |
missing_token |
No token provided in URL |
Methods
isOk(): bool
Returns true if verification succeeded.
isFailed(): bool
Returns true if verification failed.
getReason(): ?string
Returns error code (null if success).
getData(): ?array
Returns success data array (null if failed).
getUserId(): ?int
Returns authenticated user ID (null if failed).
Internationalization
SimpleAuth includes translations for error messages in multiple languages.
Supported locales:
en_US- English (United States)es_ES- Spanish (Spain)it_IT- Italian (Italy)
Customizing messages:
Messages are stored in src/locales/{locale}.php. To override messages in your application:
- Create directory:
src/App/locales/simpleauth/ - Create locale file:
src/App/locales/simpleauth/es_ES.php - Override specific messages:
See src/locales/README.md for available message IDs.
Security Best Practices
- Always use HTTPS for magic link URLs
- Short TTL: Keep token lifetime short (5-15 minutes recommended)
- Rate limiting: Limit magic link requests per email/IP
- Email validation: Verify email ownership before generating tokens
- Cleanup regularly: Run
cleanup()daily via cron job - Monitor nonces: Alert on unusual replay attack attempts
- Secure sessions: Use secure session configuration after authentication
Maintenance
Cleanup Cron Job
Add to your crontab to run daily cleanup:
cleanup.php:
Troubleshooting
Problem: Token always shows as "not found"
Solution: Check that the token is being passed correctly in the URL parameter
Problem: Token shows as "expired" immediately
Solution: Check server time synchronization. Increase clockSkewSeconds if needed.
Problem: "Token already used" on first attempt
Solution: Check for duplicate requests. Ensure the token isn't being consumed multiple times.
Problem: Database errors
Solution: Verify all tables are created and foreign keys are properly set up.
Problem: Autoloader not working (manual installation)
Solution: Verify autoload.php is in the project root and the src/ path is correct.
License
MIT License - see LICENSE file for details.
Forking and Using This Project
This project is a learning exercise and not actively maintained. You are encouraged to:
- Fork this repository and adapt it for your own projects
- Modify the code to fit your specific requirements
- Use it as a reference for understanding passwordless authentication
- Build upon it and create your own improved versions
If you create something interesting based on this work, feel free to share it (but not required).
Support
For questions, review the documentation above or fork the project to experiment. Limited support available at [email protected].
Changelog
1.0.0 (2025-01-15)
- Initial release
- Magic link token generation
- Token verification with replay protection
- Multi-language support (en_US, es_ES, it_IT)
- Clock skew tolerance
- PSR-4 autoloading
All versions of simpleauth with dependencies
ext-pdo Version *
ext-pdo_mysql Version *