webthink/guzzle-jwt

此包已废弃,不再维护。未建议替代包。

此包包含用于请求JWT令牌的有用类和接口

v1.1.0 2020-01-08 08:40 UTC

README

codecov Build Status SensioLabsInsight MIT licensed

此包的目标是提供一组有用的类,以便使用Guzzle进行JWT认证。

安装

您可以使用composer安装此包

composer require webthink/guzzle-jwt

用法

基本用法

首先,您需要实现自己的认证器。

class MyAuthenticator implements AuthenticatorInterface
{
    public function authenticate($username, $password)
    {
    // code.. and return a TokenInterface.
    }
}

然后,您需要将包提供的中间件添加到您的处理器堆栈中。

// Your handler stack
$stack = HandlerStack::create();

// Add middleware
$stack->append(new Middleware($myAuthenticator));

$httpClient = new Client([
    'handler' => $stack,
    'jwt' => [
        'username' => 'username',
        'password' => 'password',
    ],
]);

$response = $httClient->get('/my_api_that_requires_jwt_token');

注意

我不会继续创建实现AuthenticatorInterface的类。AuthenticatorInterface可以由使用此包的开发者实现。

存储

存储是缓存JWT以供多个请求使用的一种方式。根据存储方式,令牌可以用于多个HTTP请求。

为了使用存储,您需要实现自己的认证器,该认证器将使用存储,或者使用StoreAuthenticator

$myAuthenticator = new MyAuthenticator();
$storeAuthenticator = new StoreAuthenticator($myAuthenticator, new MemoryStorage());

然后,您可以将存储认证器传递到中间件,并按照上述步骤进行。

贡献

请随意评论任何您认为可能走错方向的内容,或者更好地通过PR贡献。

待办事项

  • 增加单元测试。
  • 研究包含一些认证器的可能性。

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。