eftec/authone

最小化认证库

1.4 2022-06-21 17:49 UTC

This package is auto-updated.

Last update: 2024-09-21 22:45:49 UTC


README

这个最小化库是一个简单的认证系统,支持不同类型的认证。

Packagist Total Downloads Maintenance composer php php CocoaPods

支持的特性

认证类型(认证如何工作)

  • PHP Session

  • Token认证

  • 用户名和密码认证

  • 类似JWT的token载体。

用户存储类型(用户将存储在哪里,登录时如何验证)

  • PDO(数据库)。此库支持MySQL、SQLServer和Oracle
  • Document。此库允许使用文件系统存储信息,因此不依赖于数据库服务器。
  • Cache。此库允许使用名为CacheOne的库来存储信息,它允许以下类型的持久性
    • Redis(Redis可以允许持久存储)
    • Memcached(Memcached没有持久存储!然而,它仍然是一个替代方案)
    • APCU(APCU也没有持久存储,但仍然允许)
    • PDO(数据库,但已由PDO覆盖)
    • Document(文件系统,但已由Document覆盖)

Token存储类型(临时信息将存储在哪里)。系统还允许使用Cache库作为存储。

  • Redis内存缓存服务(它也会存储在磁盘上)
  • Memcached仅内存缓存服务
  • APCU内存缓存服务(作为PHP扩展安装)
  • PDO数据库访问。
  • Document使用文件系统作为缓存服务。它不需要安装任何服务。

允许的认证操作

  • createAuth():允许创建新的认证
  • validateAuth():允许验证认证。
  • renewAuth():允许更新认证,允许扩展认证时间。
  • invalidateAuth():使认证无效。
  • 您可以从GET、POST、HEADER、BODY和COOKIES中获取数据,或自行处理。

注意:然而,并非所有方法都允许所有这些操作。例如,用户名和密码认证不能使认证无效或更新。

允许的存储操作

  • addUser():允许在存储系统中添加新用户。

  • deleteUser():允许删除用户。

  • disableuser():允许禁用用户。根据配置,您可以选择允许禁用用户。

  • getUser():获取用户。密码以加密形式获得。

  • updateUser():更新用户。

  • listUsers():允许列出所有用户。并非所有存储系统都允许列出用户。

  • validateUser():将用户与用户/密码提供者进行比较以验证用户。

使用Token和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 Session

phpsession

PHP Session依赖于PHP会话的配置。默认情况下,PHP在文件中创建新的会话,并使用Session ID来恢复存储在会话中的信息。

虽然PHP会话默认使用cookies,但此库是中立的,会话ID可以通过URL、HTML头或cookies发送和接收。

  • 优点
    • 已集成到PHP中
    • 可以与PHP当前生成的会话共存
  • 缺点
    • 由于它依赖于PHP会话,因此PHP配置中会话的过期和行为。
    • 它只适用于单个服务器配置。

已启用的功能

  • 生存时间(TTL)
  • 无效化

令牌认证。

token

它与PHP会话类似,但它不依赖于PHP认证系统。它基于一个64字符代码的令牌,客户端可以使用该令牌进行认证。

此令牌存储在缓存服务器中。

  • 优点
    • 它比PHP会话更灵活,更可靠。
    • 它可能比PHP会话更快,因为它可以使用内存中的缓存服务器。
    • 它比用户名和密码更安全,因为密码只传输一次。
  • 缺点
    • 它需要设置和配置令牌服务器。
    • 它只适用于单个服务器配置。

已启用的功能

  • 生存时间(TTL)
  • 无效化

用户名和密码

up

这是最基本的认证类型。客户必须每次验证时发送用户名和密码。如果您使用此验证,则登录和验证工作方式相同。

  • 优点

    • 它更容易实现,并且不依赖于或使用缓存来验证信息。它只需要一个存储服务器。
    • 它可以在多个服务器安装中工作(如果每个服务器具有相同的用户存储)
  • 缺点

    • 它不太安全,因为客户每次需要验证时都会发送用户名和密码。
    • 它也较慢,因为它必须每次使用存储服务器验证用户名和密码。

已启用的功能

  • 生存时间(TTL)
  • 无效化

JWTlite令牌承载

jwt

它通过客户机和服务器之间发送用户内容和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和商业。