duocircle/remoteauth-laravel-sdk

RemoteAuth 的 Laravel SDK。

8.0.2 2019-06-01 23:12 UTC

This package is auto-updated.

Last update: 2024-10-01 00:06:46 UTC


README

您可以使用此包快速开始使用 RemoteAuth

RemoteAuth Laravel SDK 在幕后使用 RemoteAuth PHP SDK。

概述

此包为您设置以下内容

  • 通过 Socialite 实现OAuth认证流程
  • 配置 RemoteAuth PHP SDK
  • 使用 Laravel 缓存进行请求结果缓存
  • 处理访问令牌过期时的刷新

设置

配置

您需要将以下配置值添加到 config/services.php

    'remoteauth' => [
        'client_id' => env('REMOTEAUTH_CLIENT_ID'),
        'client_secret' => env('REMOTEAUTH_CLIENT_SECRET'),
        'url' => env('REMOTEAUTH_URL'),
        'redirect' => config('app.url') . '/login/remoteauth/callback',
        'scopes' => ''
    ],
  • client_id - 从 RemoteAuth OAuth 客户端 UI 获取。
  • client_secret - 从 RemoteAuth OAuth 客户端 UI 获取。
  • url - 这是您使用的 RemoteAuth 服务器的 URL。
  • redirect - 这是 RemoteAuth 在 OAuth 工作流期间响应的您的应用程序的端点。
  • scopes - 授予访问令牌时应请求的权限范围。

用户

有一个配置变量可用于声明您的哪个模型代表 RemoteAuth 用户。

RemoteAuth::setUserModel(\App\User::class);

您的 User 模型必须实现 remoteauth-php-sdk 提供的 RemoteAuthUser 接口。

以下是一个标准方法重写的示例

class User extends Authenticatable implements RemoteAuthUser
{

    // ... standard user model stuff here
    
    /** @Override */
    public function remoteAuthUserId(): string
    {
        return $this->remoteauth_user_id;
    }
    
    /** @Override */
    public function accessToken(): string
    {
        return $this->access_token;
    }

    /** @Override */
    public function refreshToken(): string
    {
        return $this->refresh_token;
    }

    /** @Override */
    public function accessTokenExpiration(): \DateTime
    {
        return $this->expires_at;
    }

    public function handleTokenRefresh(string $userId, string $accessToken, string $refreshToken, int $expiresIn): void
    {
        $this->remoteauth_user_id = $userId;
        $this->access_token = $accessToken;
        $this->refresh_token = $refreshToken;
        $this->expires_at = Carbon::now()->addSeconds($expiresIn);
        $this->save();
    }
}

服务提供者注册

RemoteAuth Laravel SDK 提供了标准设置,让您快速开始。它将为您处理整个 OAuth 工作流,前提您有一个标准的工作流程。

AppServiceProvider.php 中,注册您的 User 模型并注册用于 OAuth 流的路线

// AppServiceProvider.php

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    RemoteAuth::setUserModel(\App\User::class);
    RemoteAuth::registerRoutes();
}

RemoteAuth::registerRoutes() 方法可以接受一个闭包参数。如果传递,则在 OAuth 工作流成功时将调用此闭包。

闭包接受一个参数,$userDetails。此参数是从 Socialite 返回的用户对象。

RemoteAuth::registerRoutes(function(User $userDetails) {
    $user = \App\User::firstOrNew([
        'email' => $userDetails->email,
    ], [
        'name' => $userDetails->name,
    ]);

    $user->handleTokenRefresh($userDetails->id, $userDetails->token, $userDetails->refreshToken, $userDetails->expiresIn);

    Auth::login($user);

    return redirect('/');
});

如果您没有传递闭包,则默认闭包将更新(或创建新用户)通过电子邮件地址识别的用户。然后,它将在您的 User 类上调用 handleTokenRefresh() 方法,然后登录用户。

传递给闭包的 $userDetails 包含以下属性

  • id
  • name
  • email
  • token
  • refreshToken
  • expiresIn

RemoteAuth::registerRoutes() 为您注册两个路由

  • /login/remoteauth - 当开始登录流程时,需要将用户重定向到此端点。
  • /login/remoteauth/callback - 当用户通过 RemoteAuth 进行认证时,RemoteAuth 将调用此端点。