oelmenshawy/laravel-passport-user-id-grant

Laravel Passport 的用户 ID 授权

dev-master 2020-06-06 16:17 UTC

This package is auto-updated.

Last update: 2024-09-07 02:22:23 UTC


README

此包为您的 OAuth2 服务器添加用户 ID 授权。如果您有一个 API 并希望允许用户通过用户 ID 返回 access_tokenrefresh_token,则这可能很有用。

结果,您将能够将用户 ID 与您自己的 OAuth2 服务器签发的 access_tokenrefresh_token 进行交换。您将收到这个用户 ID,并在自己处返回对应的用户实例。

安装

您可以通过以下命令使用 composer 安装此包:

composer require oelmenshawy/laravel-passport-user-id-grant

包将自动注册自己。

使用方法

使用 axios 的示例

axios.post('/oauth/token', {
    grant_type: 'user_id', // static 'user_id' value
    client_id: clientId, // client id
    client_secret: clientSecret, // client secret
    user_id: userId, // user id
  })
  .then((response) => {
    const {
      access_token: accessToken,
      expires_in: expiresIn,
      refresh_token: refreshToken,
    } = response.data;

    // success logic
  })
  .catch((error) => {
    const {
      message,
      hint,
    } = error.response.data;

    // error logic
  });

使用 guzzle 的示例

<?php

use GuzzleHttp\Client;
use Illuminate\Support\Arr;

$http = new Client;

$response = $http->post($domain . '/oauth/token', [
    RequestOptions::FORM_PARAMS => [
        'grant_type' => 'user_id', // static 'user_id' value
        'client_id' => $clientId, // client id
        'client_secret' => $clientSecret, // client secret
        'user_id' => $userId, // user id
    ],
    RequestOptions::HTTP_ERRORS => false,
]);
$data = json_decode($response->getBody()->getContents(), true);

if ($response->getStatusCode() === Response::HTTP_OK) {
    $accessToken = Arr::get($data, 'access_token');
    $expiresIn = Arr::get($data, 'expires_in');
    $refreshToken = Arr::get($data, 'refresh_token');

    // success logic
} else {
    $message = Arr::get($data, 'message');
    $hint = Arr::get($data, 'hint');

    // error logic
}

测试

您可以使用以下命令运行测试:

composer test

更新日志

有关最近更改的更多信息,请参阅 更新日志

贡献

有关详细信息,请参阅 贡献指南

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件