customergauge/cognito

AWS Cognito提供者用于Laravel身份验证

2.0.0 2024-06-12 11:31 UTC

This package is auto-updated.

Last update: 2024-09-12 12:00:31 UTC


README

Build Code Coverage Scrutinizer Code Quality

Laravel Cognito提供者 🔑

此库为Laravel提供CognitoUserProvider。

安装

composer require customergauge/cognito

使用

身份验证配置

auth.php文件中,添加以下设置

默认守卫

    'defaults' => [
        'guard' => 'cognito-token',
        'passwords' => 'users',
    ],

新的守卫配置

    'guards' => [
        'cognito-token' => [
            'driver' => 'token',
            'provider' => 'cognito-provider',
            'storage_key' => 'cognito_token',
            'hash' => false,
        ],
    ],

用户提供者配置

    'providers' => [
        'cognito-provider' => [
            'driver' => \CustomerGauge\Cognito\CognitoUserProvider::class,
        ],
    ],

Cognito环境变量

    /*
    |--------------------------------------------------------------------------
    | Cognito Custom Configuration
    |--------------------------------------------------------------------------
    |
    | The following configuration is not part of standard Laravel application.
    | We use it to configure the CognitoUserProvider process so that we can
    | properly validate the JWT token provided by AWS Cognito.
    |
    */

    'cognito' => [
        'pool' => env('AWS_COGNITO_USER_POOL_ID'),
        'region' => env('AWS_COGNITO_USER_POOL_REGION'),
    ],

身份验证中间件

App\Http\Kernel中配置auth中间件为'auth:cognito-token'

UserFactory

最后,你需要提供一个自己的UserFactory实现并在ServiceProvider中注册它。

final class CognitoUserFactory implements UserFactory
{
    public function make(array $payload): ?Authenticatable
    {
        return new MyUserObject(
            $payload['username'],
            $payload['custom:my_custom_cognito_attribute'],
        );
    }
}

在提供商中

$this->app->bind(CustomerGauge\Cognito\Contracts\UserFactory, App\Auth\CognitoUserFactory::class);