aplr/laravel-passport-grant-anonymous

Laravel Passport 的匿名授权。

v0.1.4 2018-09-13 10:47 UTC

This package is auto-updated.

Last update: 2024-09-20 10:33:52 UTC


README

Travis Packagist license

简介

laravel-passport-grant-anonymous 包允许你在 Laravel Passport 中使用匿名授权,除了默认的密码授权。

这允许没有密码的“匿名”用户使用唯一的标识符(例如,一个仅对用户设备唯一的字符串)获取 OAuth 令牌。

安装

在您的 composer.json 中要求安装 aplr/laravel-passport-grant-anonymous 包,并更新您的依赖项。

$ composer require aplr/laravel-passport-grant-anonymous

Laravel 5.5 使用包自动发现,因此不需要您手动添加 ServiceProvider。

如果您不使用自动发现,请将 ServiceProvider 添加到 config/app.php 中的 providers 数组。

Aplr\LaravelPassportAnonymous\ServiceProvider::class,

用法

您需要在用户表中提供一个 auth_id 字段,以便授权后可以检索关联的用户模型。

准备您的模型

为此,您要么提供上述字段,或者实现方法 public findByAuthId(string $authId),该方法通过 authId 返回用户模型。

使用授权

准备模型后,您可以使用授权,就像通常使用 密码授权 一样,但将 grant_type 设置为 anonymous,并通过 auth_id 字段传递唯一标识符。使用匿名授权请求访问令牌如下所示

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'anonymous',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'auth_id' => 'some-unique-identifier',
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);

许可证

laravel-passport-grant-anonymous 是开源软件,使用 MIT 许可证授权。