jdz/authentication
Composer 安装命令:
composer require jdz/authentication
包简介
Basic Authentication Library
关键字:
README 文档
README
Simple authentication library with support for multiple authentication connectors.
Features
- Multiple authentication connectors support
- Type-safe authentication status enum
- PDO-based database authentication
- Basic authentication connector
- Extensible connector interface
- Comprehensive test suite
- Automatic password hashing
- Secure password verification
Installation
composer require jdz/authentication
Requirements
- PHP 8.1 or higher
Quick Start
use JDZ\Authentication\Authentication; use JDZ\Authentication\Connector\BasicConnector; $auth = new Authentication(); $auth->addConnector(new BasicConnector('admin', 'secret123')); $result = $auth->authenticate([ 'identifier' => 'admin', 'password' => 'secret123', ]); if ($result->isSuccess()) { echo "Welcome, " . $result->getUsername(); } else { echo "Error: " . $result->getMessage(); }
Examples
All examples can be run directly from the command line:
Note: Examples 03 and 05 require PDO SQLite extension. If not available, you can:
- Enable
pdo_sqlitein yourphp.ini, OR - Modify the examples to use MySQL/PostgreSQL (see Database Setup Notes below)
Check available PDO drivers: php -m | grep -i pdo
See the examples directory for detailed examples:
01-basic-authentication.php- Basic authentication02-multiple-connectors.php- Multiple authentication connectors03-database-authentication.php- Database authentication with PDO (requires PDO SQLite)04-error-handling.php- Error handling with exceptions05-advanced-mysql.php- Advanced MySQL authentication (requires PDO SQLite or MySQL)
Run example:
php examples/01-basic-authentication.php
01-basic-authentication.php
Basic Authentication with BasicConnector
Demonstrates:
- Creating a basic authentication instance
- Using BasicConnector for simple identifier/password authentication
- Testing various authentication scenarios (valid, invalid, missing credentials)
- Checking authentication status and error messages
Use Case: Simple applications with hardcoded or configuration-based credentials.
02-multiple-connectors.php
Multiple Authentication Connectors
Demonstrates:
- Adding multiple connectors to a single authentication instance
- How connectors are tried by priority order
- Authenticating different users with different credentials
- Converting result to array format
Use Case: Applications supporting multiple authentication methods or user sources.
03-database-authentication.php
Database Authentication with PDO
Demonstrates:
- Creating a custom connector by extending AbstractConnector
- Using PDO for database queries
- Storing and verifying hashed passwords
- Setting up and testing with SQLite (easily adaptable to MySQL/PostgreSQL)
- Proper SQL prepared statements for security
Use Case: Standard web applications with user accounts stored in a database.
Key Points:
- Uses
password_hash()andpassword_verify()for secure password storage - Demonstrates proper PDO usage with prepared statements
- Shows how to create custom connectors
04-error-handling.php
Error Handling with Exceptions
Demonstrates:
- Using the built-in AuthenticationException
- Proper exception handling patterns
- Silent mode authentication (without exceptions)
- Custom error message mapping
- Using AuthStatusEnum for detailed error information
Use Case: Production applications requiring robust error handling and user-friendly error messages.
Key Points:
- Shows how to extract status codes and messages from exceptions
- Demonstrates both exception and return-value error handling patterns
- Custom error message mapping for better UX
05-advanced-mysql.php
Advanced MySQL Authentication with User Data
Demonstrates:
- Advanced custom connector with additional features
- Loading user profile data during authentication
- Checking user account status (active/inactive)
- Populating AuthenticationResult with user details
- Production-ready MySQL connector implementation
Use Case: Full-featured applications requiring user profile data, account status checks, and multi-language support.
Key Points:
- Shows how to implement custom authenticate() method
- Demonstrates loading additional user data
- Includes account status validation
- Multi-language support example
Database Setup Notes
For examples using databases:
SQLite No setup required - creates in-memory database automatically.
Testing
# Run all tests composer test # Or use PHPUnit directly vendor/bin/phpunit
The test suite includes 59 tests with 143 assertions:
- AuthStatusEnumTest (9 tests): Tests for the authentication status enum values and methods
- AuthenticationResultTest (12 tests): Tests for the authentication result object, factory methods, and toArray() conversion
- AuthenticationTest (13 tests): Tests for the main authentication class including credentials normalization, priority, and connector flow
- BasicConnectorTest (11 tests): Tests for the basic authentication connector including constructor validation and authentication scenarios
- DatabaseConnectorTest (14 tests): Tests for the database authentication connector using mocked DatabaseInterface
Authentication Status
The library uses AuthStatusEnum for type-safe status handling:
| Status | Value | Description |
|---|---|---|
FAILURE |
0 | Authentication failed (generic) |
SUCCESS |
1 | Successful authentication |
EMPTY_IDENTIFIER |
2 | Missing identifier (email/username) in credentials |
EMPTY_PASSWORD |
3 | Missing password in credentials |
USER_NOT_FOUND |
4 | User account not found |
INVALID_PASSWORD |
5 | Invalid password |
USER_BANNED |
6 | Account has been suspended |
USER_NOT_CONFIRMED |
7 | Email not confirmed |
ACCOUNT_LOCKED |
8 | Account locked due to failed attempts |
Each status provides:
value- Returns the integer status codemessage()- Returns the descriptive error messageisSuccess()- Returns true only for SUCCESS statusname- The enum case name (e.g., "SUCCESS", "INVALID_PASSWORD")
Example usage:
$result = $auth->authenticate($credentials); if ($result->isSuccess()) { echo "User ID: " . $result->getUserId(); echo "Email: " . $result->getEmail(); } else { echo "Status Code: " . $result->getStatus()->value; // 5 echo "Message: " . $result->getStatus()->message(); // "Invalid password" echo "Name: " . $result->getStatus()->name; // "INVALID_PASSWORD" }
AuthenticationResult
The AuthenticationResult object provides the following methods:
$result->isSuccess(); // bool - Check if authentication succeeded $result->getStatus(); // AuthStatusEnum - Get the status enum $result->getMessage(); // string - Get error message (or status message) $result->getUserId(); // ?int - Get user ID (if available) $result->getIdentifier(); // string - Get the identifier used $result->getEmail(); // string - Get user email $result->getUsername(); // string - Get username $result->getFirstname(); // string - Get first name $result->getLastname(); // string - Get last name $result->getFullname(); // string - Get full name (or fallback to email/identifier) $result->getType(); // string - Get connector type (e.g., "basic", "database") $result->get($key); // mixed - Get custom data $result->toArray(); // array - Convert to array
License
This project is licensed under the MIT License - see the LICENSE file for details.
jdz/authentication 适用场景与选型建议
jdz/authentication 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Authentication」 「core」 「JDZ」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jdz/authentication 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jdz/authentication 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jdz/authentication 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Contao Open Source CMS
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
DiZed Team Core module for the Magento 2 project
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-15