eftec / authone
最小化认证库
Requires
- php: >=7.2.5
- ext-json: *
- ext-openssl: *
- eftec/cacheone: ^2.12
Requires (Dev)
- eftec/documentstoreone: ^1.23
- eftec/pdoone: ^3.1
- phpunit/phpunit: ^9.5.19
README
这是一个简单的认证系统库,允许不同类型的认证。
支持的功能
认证类型(认证如何工作)
-
PHP会话
-
令牌认证
-
用户名和密码认证
-
类似JWT的令牌载体。
用户存储类型(用户在哪里存储,登录时如何验证)
- PDO(数据库)。此库支持MySQL、SQLServer和Oracle
- 文档。此库允许使用文件系统存储信息,因此不依赖于数据库服务器。
- 缓存。此库允许使用名为CacheOne的库来存储信息。它允许以下类型的持久性
- Redis(Redis可以允许持久化存储)
- Memcached(Memcached没有持久化存储!然而,它仍然是一个替代方案)
- APCU(APCU也没有持久化存储,但仍然允许)
- PDO(数据库,但已由PDO覆盖)
- 文档(文件系统,但已由文档覆盖)
令牌存储类型(临时信息将存储在哪里)。系统还允许使用缓存库作为存储。
- Redis 内存(它还存储在磁盘)缓存服务
- Memcached 仅内存缓存服务
- APCU 内存缓存服务(作为PHP扩展安装)
- PDO 数据库访问。
- 文档 使用文件系统作为缓存服务。它不需要安装任何服务。
认证允许的操作
- createAuth():允许创建新的认证
- validateAuth():允许验证认证。
- renewAuth():允许续订认证,允许延长认证时间。
- invalidateAuth():使认证无效。
- 您可以从GET、POST、HEADER、BODY和COOKIES中获取数据,或自行处理。
注意:然而,并非所有方法都允许这些操作。例如,用户名和密码认证无法使认证无效或续订。
存储允许的操作
-
addUser():允许在存储系统中添加新用户。
-
deleteUser():允许删除用户。
-
disableuser():允许禁用用户。根据配置,您可以允许禁用用户。
-
getUser():获取用户。密码以加密形式获得。
-
updateUser():更新用户。
-
listUsers():允许列出所有用户。并非所有存储系统都允许列出用户。
-
validateUser():通过与用户/密码提供者进行比较来验证用户。
使用令牌和PDO的示例
//Connections (you can check the section types of configuration for more information about $pdoConfig and $tokenConfig) $auth = new AuthOne( 'token', // the type of authentication 'pdo', // the type of store (where the users will be stored), values allowed: 'pdo','document','token' $pdoConfig, // the configuration of the PDO. $tokenConfig // the configuration of token (it is only required for token type of authentication) ); $auth->fieldConfig('mytable', 'myuser', 'mypassword', 'mydisable'); // creating authentication $token = $this->auth->createAuth('admin', 'abc.123', 1); // validating authentication var_dump($this->auth->validate($token));
认证类型
PHP会话
PHP会话取决于PHP会话的配置。默认情况下,PHP在一个文件中创建新会话,并使用会话ID恢复存储在会话中的信息。
虽然PHP会话默认使用cookies,但此库是无状态的,会话ID可以通过URL、HTML标题或cookies发送和接收。
- 优点
- 已集成到PHP中
- 它可以与PHP当前生成的会话共存
- 缺点
- 因为它依赖于PHP会话,所以会话的过期和PHP配置中的行为。
- 它仅适用于单个服务器配置。
启用的功能
- 生存时间(TTL)
- 使无效
令牌认证。
它类似于PHP会话,但不需要依赖于PHP的认证系统。它基于一个64字符的代码,客户端可以使用它进行认证。
此令牌存储在缓存服务器上。
- 优点
- 它比PHP会话更灵活,也更可靠。
- 它可能比PHP会话更快,因为它可以使用内存中的缓存服务器。
- 它比用户名和密码更安全,因为密码只传输一次。
- 缺点
- 它需要设置和配置令牌服务器。
- 它仅适用于单个服务器配置。
启用的功能
- 生存时间(TTL)
- 使无效
用户名和密码
这是最基本的认证类型。客户必须为每次验证发送用户名和密码。如果您使用此验证,则登录和验证方式相同。
-
优点
- 它更容易实现,并且不依赖于或使用缓存来验证信息。它只需要一个存储服务器。
- 它可以在多个服务器安装中工作(如果每个服务器都有相同的用户存储)
-
缺点
- 它不太安全,因为客户每次需要验证时都发送用户名和密码。
- 它也较慢,因为它每次都使用存储服务器验证用户名和密码。
启用的功能
- 生存时间(TTL)
- 使无效
JWTlite令牌承载者
它通过客户和服务器每次相互发送用户内容和一个CRC验证器(作为令牌)来工作。
这种认证方式允许简化依赖于多个服务器的系统中的认证。不需要服务器验证令牌,而是使用自己的令牌作为验证。
它很安全,因为令牌的内容无法被修改或编辑。
此方法与JWT不兼容,因为结构不同
-
此库生成的令牌不包含认证类型或算法。默认情况下,此库使用SHA256,虽然可以更改它,但指定的类型永远不会发送或接收。这有什么意义?
-
使用此库创建的令牌未经序列化,令牌的内容保持不变。
-
令牌可以包含过期时间。内部确定如果过期时间为1000000000,则令牌不会过期。
-
优点
- 验证更快,因为它不需要服务器存储验证令牌。
- 它可以在多个服务器安装中工作。
-
缺点
- 此令牌比其他方法更大。
- 它不允许无效化,因为没有服务器存储要验证或无效化的令牌。
登录返回值的结构
[ 'body'=>the content of the token, example the user, level of the user, full name, email, etc 'token'=the time of expiration + the token that validates that the body has not been changed ]
虽然JWT具有以下结构(序列化为BASE64)
{ "alg": "HS256", "typ": "JWT" } . { content of the token } . verify signature
需要验证的值的结构
$this->validate(the body or content,the token); // if the validation is right, then it returns the body, otherwise it returns false
启用的功能
- 生存时间(TTL)
- 使无效
配置类型
配置PDO存储。
// if you want to store the users and password in the database $pdoConfig = [ 'databaseType' => 'mysql', // the type of database: mysql, sqlsrv or oci (oracle) 'server' => '127.0.0.1', // the server of the database 'user' => 'root', // the user 'pwd' => 'abc.123', // the password 'db' => 'sakila' // the database or schema. In oracle, this value is ignored, and it uses the user. ];
配置文档存储。
// if you want to store the users and passwords in the file-system $docConfig = [ 'database' => __DIR__ . '/base', // the initial folder 'collection' => '', // (optional) the sub-folder 'strategy' => 'folder', // (optional )the lock strategy. // It is used to avoid that two users replace the same file at the same time. 'server' => '', // used by REDIS, example: localhost:6379 'serializeStrategy' => 'json_array' // (optional) the strategy to serialization ];
配置令牌存储
$tokenConfig=[ // it is required if you are using TOKEN. 'type'=>'redis', // it will use redis to store the temporary tokens. // Values allowed: auto (automatic),redis (redis) , // memcache (memcache),apcu (PHP APCU),pdoone (database) and documentone (file system) 'server'=>'127.0.0.1', // the server of REDIS or PDO 'schema'=>'', // (optional), the schema or folder. 'port'=>0, // (optional) the port, used by redis memcache or pdo 'user'=>'', // (optional) the user used by pdo 'password'=>'' // (optional) the password used by pdo ];
变更日志
- 1.4 2022-06-21
- 添加了createAuthObj()方法
- 修复了createAuth方法。
- 修复了validateAuth方法。
- 1.3 2022-06-19
- 添加了failcause字段,可以在其中找到验证错误日志
- 1.2 2022-06-19
- 为createAuth()和validateAuth()添加了令牌承载者。
- 添加了加密参数。
- 用户对象密码已删除(JWTLITE)
- 1.01 2022-06-17修复了尝试注入实例时的错误。
- 1.00 2022-06-12发布版本。
- 0.92 2022-03-22更改了依赖项,现在PdoOne和DocumentStoreOne是可选依赖项。
- 0.85 2022-03-21第一个版本(发布候选版)
- 0.1 2022-03-02第一个版本(alpha版)
许可
版权:Jorge Castro Castillo(2022)双重许可,LGPL和商业许可。