ilzrv/laravel-steam-auth

为 Laravel 实现Steam认证

v3.1.0 2024-03-13 08:04 UTC

README

Latest Stable Version Total Downloads GitHub Workflow Status Codecov License

该包允许您在Laravel项目中实现Steam认证。

要求

  • Laravel 9+
  • PHP 8.1+

安装

安装包

composer require ilzrv/laravel-steam-auth

发布配置文件

php artisan vendor:publish --provider="Ilzrv\LaravelSteamAuth\ServiceProvider"

设置Steam API密钥

将您的Steam API密钥添加到.env文件中。您可以在此处找到它。

如果您想使用多个API密钥,请用逗号分隔它们

STEAM_AUTH_API_KEYS=YourSteamApiKey1,YourSteamApiKey2

提示

客户端设置

您可以使用客户端支持的所有设置,例如,一个Guzzle代理

<?php

declare(strict_types=1);

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Illuminate\Http\Request;
use Ilzrv\LaravelSteamAuth\SteamAuthenticator;

public function __invoke(
    Request $request,
    HttpFactory $httpFactory,
): RedirectResponse {
    $client = new Client([
        'proxy' => 'socks5://user:password@192.168.1.1:1080',
    ]);

    $steamAuthenticator = new SteamAuthenticator(
        new Uri($request->getUri()),
        $client,
        $httpFactory,
    );
    
    // Continuation of your code...
}

代理域名

如果您想创建一个代理域名,请更新steam-auth.php中的redirect_url为您的绝对地址,例如https://auth.test/login。您可以为本地环境和生产环境使用不同的域名,如下所示

<?php

declare(strict_types=1);

return [
    'redirect_url' => env('APP_ENV', 'production') == 'production'
        ? 'https://auth.test/login'
        : null,
];

在NGINX的代理域名设置中,您可以指定以下内容

server {
    listen 443 ssl http2;
    server_name auth.test;
    return 301 https://general.test$uri$is_args$args;
}

基本示例

routes/web.php

Route::get('login', \App\Http\Controllers\Auth\SteamAuthController::class);

创建一个控制器SteamAuthController.php

<?php

declare(strict_types=1);

namespace App\Http\Controllers\Auth;

use App\Models\User;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\Psr7\Uri;
use Illuminate\Auth\AuthManager;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Ilzrv\LaravelSteamAuth\Exceptions\Authentication\SteamResponseNotValidAuthenticationException;
use Ilzrv\LaravelSteamAuth\Exceptions\Validation\ValidationException;
use Ilzrv\LaravelSteamAuth\SteamAuthenticator;
use Ilzrv\LaravelSteamAuth\SteamUserDto;

final class SteamAuthController
{
    public function __invoke(
        Request $request,
        Redirector $redirector,
        Client $client,
        HttpFactory $httpFactory,
        AuthManager $authManager,
    ): RedirectResponse {
        $steamAuthenticator = new SteamAuthenticator(
            new Uri($request->getUri()),
            $client,
            $httpFactory,
        );

        try {
            $steamAuthenticator->auth();
        } catch (ValidationException|SteamResponseNotValidAuthenticationException) {
            return $redirector->to(
                $steamAuthenticator->buildAuthUrl()
            );
        }

        $steamUser = $steamAuthenticator->getSteamUser();

        $authManager->login(
            $this->firstOrCreate($steamUser),
            true
        );

        return $redirector->to('/');
    }

    private function firstOrCreate(SteamUserDto $steamUser): User
    {
        return User::firstOrCreate([
            'steam_id' => $steamUser->getSteamId(),
        ], [
            'name' => $steamUser->getPersonaName(),
            'avatar' => $steamUser->getAvatarFull(),
            'player_level' => $steamUser->getPlayerLevel(),
            // ...and other what you need
        ]);
    }
}