customergauge/cognito

AWS Cognito provider for Laravel Authentication
52,861 22
Install
composer require customergauge/cognito
Latest Version:2.0.0
PHP:>=8.1
Laravel:7 – 11
License:MIT
Last Updated:Jun 12, 2024
Links: GitHub  ·  Packagist
Maintainer: Deleu

Build Code Coverage Scrutinizer Code Quality

Laravel Cognito Provider 🔑

This library provides a CognitoUserProvider for Laravel.

Installation

composer require customergauge/cognito

Usage

Auth configuration

In the auth.php file, add the following settings:

Default Guard

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

The new Guard configuration

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

The User Provider configuration


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

Cognito Environment Variables

    /*
    |--------------------------------------------------------------------------
    | 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'),
    ],

Auth Middleware

Configure the auth middleware at App\Http\Kernel with 'auth:cognito-token'

UserFactory

The last thing you'll need is to provide your own implementation of UserFactory and register it in a ServiceProvider.

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

In the provider:

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

Related Packages

ellaisys/aws-cognito

Laravel authentication with AWS Cognito, supporting web, API, SSO, MFA, WebAuthn...

308,909 122
hackthebox/laravel-iam-auth

AWS IAM authentication for Laravel: RDS database connections and SDK credential...

9,359 5
pallant/laravel-aws-cognito-auth

An authentication driver for Laravel for authenticating users in AWS Cognito Use...

7,673 77
benbjurstrom/cognito-jwt-guard

A laravel auth guard for JSON Web Tokens issued by Amazon AWS Cognito

13,065 11
zarate-systems/laravel-cognito-auth

An authentication driver for Laravel for authenticating users in AWS Cognito Use...

1,169 2

Version History

Version Released PHP Laravel License
2.0.0 >=8.1 >=7.1 MIT
1.4.0 >=7.3 >=7.1 MIT
1.3.0 >=7.3 ^7.1 || ^8.0 || ^9.0 MIT
1.2.0 >=7.3 ^7.1 || ^8.0 || ^9.0 MIT
1.1.0 >=7.3 ^7.1 || ^8.0 MIT