Download the PHP package ellaisys/aws-cognito without Composer
On this page you can find all versions of the php package ellaisys/aws-cognito. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ellaisys/aws-cognito
More information about ellaisys/aws-cognito
Files in ellaisys/aws-cognito
Package aws-cognito
Short Description AWS Cognito package that allows Auth and other related features using the AWS SDK for PHP
License MIT
Homepage https://ellaisys.github.io/aws-cognito/
Informations about the package aws-cognito
Laravel AWS Cognito Package for Web and API authentication with MFA Feature
AWS Cognito package using the AWS SDK for PHP
This package provides a simple way to use AWS Cognito authentication in Laravel for Web and API Auth Drivers. The idea of this package, and some of the code, is based on the package from Pod-Point which you can find here: Pod-Point/laravel-cognito-auth, black-bits/laravel-cognito-auth and tymondesigns/jwt-auth.
DEMO Application. You can try and register and login. For the first time, it will force the user to change password. The source code of the demo application is also available of the GitHub.
We decided to use it and contribute it to the community as a package, that encourages standarised use and a RAD tool for authentication using AWS Cognito.
Features
- Registration and Confirmation E-Mail (Sign Up)
- Forced password change at first login (configurable)
- Login (Sign In)
- Token Validation for all Session and Token Guard Requests
- Remember Me Cookie
- Single Sign On Updated (Fix: Issue #86)
- Forgot Password (Resend - configurable)
- User Deletion
- Edit User Attributes
- Reset User Password
- Confirm Sign Up
- Easy API Token handling (uses the cache driver)
- DynamoDB support for Web Sessions and API Tokens (useful for server redundency OR multiple containers)
- Easy configuration of Token Expiry (Manage using the cognito console, no code or configurations needed)
- Support for App Client without Secret
- Support for Cognito Groups, including assigning a default group to a new user
- Session (Web) now has AccessToken and RefreshToken as part of the claim object
- Logout (Sign Out) - Remove access tokens from AWS
- Forced Logout (Sign Out) - Revoke the RefreshToken from AWS
- MFA Implementation for Session and Token Guards
- Password validation based on Cognito Configuration
- Mapping Cognito User using Subject UUID NEW
Compatability
PHP Version | Support |
---|---|
7.4 | Yes :heavy_check_mark: |
8.0 | Yes :heavy_check_mark: |
8.1 | Yes :heavy_check_mark: |
8.24 | Yes :heavy_check_mark: |
Laravel Version | Support |
---|---|
7.x | Yes :heavy_check_mark: |
8.x | Yes :heavy_check_mark: |
9.x | Yes :heavy_check_mark: |
10.x | Not tested |
Installation
You can install the package via composer.
Laravel 5.4 and before
Using a version prior to Laravel 5.5 you need to manually register the service provider.
Configuration File: Next you can publish the config.
Last but not least you want to change the auth driver. To do so got to your config\auth.php file and change it to look the following:
[!IMPORTANT] This is a new feature that is released in V1.2.0 and shall work with Laravel 8.37 (with anonymous migration support). For verions below Laravel 8.37, this feature is disabled. You will need to update the users table migration and add the sub column (type:string, nullable:yes, index:yes).
Database Migrations
The AWS Cognito service provider registers its own database migration directory, so remember to migrate your database after installing the package. The AWS Cognito migrations will add a few columns to your users table:
If you need to overwrite the migrations that ship with AWS Cognito, you can publish them using the vendor:publish Artisan command:
If you would like to prevent AWS Cognito's migrations from running entirely, you may use the ignoreMigrations method provided by AWS Cognito. Typically, this method should be called in the register method of your AppServiceProvider:
Cognito User Pool
In order to use AWS Cognito as authentication provider, you require a Cognito User Pool.
If you haven't created one already, go to your Amazon management console and create a new user pool.
Next, generate an App Client. This will give you the App client id and the App client secret
you need for your .env
file.
IMPORTANT: Don't forget to activate the checkbox to Enable sign-in API for server-based Authentication. The Auth Flow is called: ADMIN_USER_PASSWORD_AUTH (formerly ADMIN_NO_SRP_AUTH)
AWS IAM configuration
You also need a new IAM Role with the following Access Rights:
- AmazonCognitoDeveloperAuthenticatedIdentities
- AmazonCognitoPowerUser
- AmazonESCognitoAccess
From this IAM User you must use the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in the laravel environment file.
Cognito configuration
Add the following fields to your .env
file and set the values according to your AWS settings:
[!IMPORTANT] To sync the web session timeout with the cognito access token ttl value, set the SESSION_LIFETIME parameter in the .env file. This value is in minutes with the default value being 120 mins i.e. 2 hours. This will ensure that the laravel session times out at the same time as the access token.
For more details on how to find AWS_COGNITO_CLIENT_ID, AWS_COGNITO_CLIENT_SECRET and AWS_COGNITO_USER_POOL_ID for your application, please refer COGNITOCONFIG File
Importing existing users into the Cognito Pool
If you are already working on an existing project and want to integrate Cognito you have to import a user csv file to your Cognito Pool.
Usage
Our package is providing you 6 traits you can just add to your Auth Controllers to get our package running.
- Ellaisys\Cognito\Auth\AuthenticatesUsers
- Ellaisys\Cognito\Auth\RegistersUsers
- Ellaisys\Cognito\Auth\ResetsPasswords
- Ellaisys\Cognito\Auth\RespondsMFAChallenge
- Ellaisys\Cognito\Auth\SendsPasswordResetEmails
- Ellaisys\Cognito\Auth\VerifiesEmails
In the simplest way you just go through your Auth Controllers and change namespaces from the traits which are currently implemented from Laravel.
You can change structure to suit your needs. Please be aware of the @extend statement in the blade file to fit into your project structure.
At the current state you need to have those 4 form fields defined in here. Those are token
, email
, password
, password_confirmation
.
Single Sign-On
With our package and AWS Cognito we provide you a simple way to use Single Sign-Ons. For configuration options take a look at the config cognito.php.
When you want SSO enabled and a user tries to login into your application, the package checks if the user exists in your AWS Cognito pool. If the user exists, he will be created automatically in your database provided the add_missing_local_user
is to true
, and is logged in simultaneously.
That's what we use the fields sso_user_model
and cognito_user_fields
for. In sso_user_model
you define the class of your user model. In most cases this will simply be App\Models\User.
With cognito_user_fields
you can define the fields which should be stored in Cognito. Put attention here. If you define a field which you do not send with the Register Request this will throw you an InvalidUserFieldException and you won't be able to register.
Now that you have registered your users with their attributes in the AWS Cognito pool and your database and you want to attach a second app which should use the same pool. Well, that's actually pretty easy. You can use the API provisions that allows multiple projects to consume the same AWS Cognito pool.
*IMPORTANT: if your users table has a password field you are not going to need this anymore. What you want to do is set this field to be nullable, so that users can be created without passwords. From now on, Passwords are stored in Cognito.
Any additional registration data you have, for example firstname
, lastname
needs to be added in
cognito.php cognito_user_fields config to be pushed to Cognito. Otherwise they are only stored locally
and are not available if you want to use Single Sign On's.*
Forgot password with resend option
In case the user has not activated the account, AWS Cognito as a default feature does not allow user of use the forgot password feature. We have introduced the AWS documented feature that allows the password to be resent.
We have made this configurable for the developers so that they can use it as per the business requirement. The configuration takes a boolean value. Default is true (allows resend of forgot password)
Middleware configuration for API Routes
In case you are using this library as API driver, you can register the middleware into the kernal.php in the $routeMiddleware
To use the middleware into the Web routes, you can use the std auth middleware as shown below
To use the middleware into the API routes, as shown below
Registering Users
As a default, if you are registering a new user with Cognito, Cognito will send you an email during signUp that includes the username and temporary password for the users to verify themselves.
Using this library in conjunction with AWS Lambda, once can look to customize the email template and content. The email template can be text or html based. The Lambda code for not included in this code repository. You can create your own. Any object (array) that you pass to the registration method is transferred as is to the lambda function, we are not prescriptive about the attribute names.
We have made is very easy for anyone to use the default behaviour.
- You don't need to create an extra field to store the verification token.
- You don't have to bother about the Sessions or API tokens, they are managed for you. The session or token is managed via the standard mechanism of Laravel. You have the liberty to keep it where ever you want, no security loop holes.
- If you use the trait provided by us 'Ellaisys\Cognito\Auth\RegistersUsers', the code will be limited to just a few lines
-
if you are using the Laravel scafolding, then make the password nullable in DB or drop it from schema. Passwords will be only managed by AWS Cognito.
-
You don't need to turn off Cognito to send you emails. We rather propose the use of AWS Cognito or AWS SMS mailers, such that user credentials are always secure.
-
In case you want to suppress the mails to be sent to the new users, you can configure the parameter given below to skip welcome mails to new user registration. Default configuration shall send the welcome email.
-
The configuration given below allows the new user's email address to be auto marked as verified.
-
To assign a default group to a new user when registering set a name of the user group as per the configuration done via AWS Cognito Management Console. The default value is set to null.
- To enable custom password or user defined password, the below configuration if set to true will force the user to set the password during registration, else cognito will generate a random password and send over email and/or SMS based on the configurations.
User Authentication
We have provided you with a useful trait that make the authentication very simple (with Web or API routes). You don't have to worry about any additional code to manage sessions and token (for API).
[!NOTE] The Access Token is now validated with the AWS Cognito certificate. If the certificate is incorrect or expired, it will throw am exception.
The trait takes in some additional parameters, refer below the function signature of the trait. Note that the function takes the object of Illuminate\Support\Collection instead of Illuminate\Http\Request. This will allow you to use this function in any tier of the code.
Also, the 'guard' name reference is passed, so that you can reuse the function for multiple guard drivers in your project. The function has the capability to handle the Session and Token Guards with multiple drivers and providers as defined in /config/auth.php
In case you want to use this trait for Web login, you can write the code as shown below in the AuthController.php
In case you want to use this trait for API based login, you can write the code as shown below in the AuthApiController.php
Signout (Remove Access Token)
The logout methods are now part of the guard implementations, the logout method removes the access-tokens from AWS and also removes from Application Storage managed by this library. Just calling the auth guard logout method will be sufficient. You can implement it into the routes or controller based on your development preference.
The logout method now takes an optional boolean parameter (true) to revoke RefreshToken. The default value is (false) and that will persist the Refresh Token with AWS Cognito.
Refresh Token
You can use this trait for API to generate new token
Delete User
If you want to give your users the ability to delete themselves from your app you can use our deleteUser function from the CognitoClient.
To delete the user you should call deleteUser and pass the email of the user as a parameter to it. After the user has been deleted in your cognito pool, delete your user from your database too.
We have implemented a new config option delete_user
, which you can access through AWS_COGNITO_DELETE_USER
env var.
If you set this config to true, the user is deleted in the Cognito pool. If it is set to false, it will stay registered.
Per default this option is set to false. If you want this behaviour you should set USE_SSO to true to let the user
restore themselves after a successful login.
To access our CognitoClient you can simply pass it as a parameter to your Controller Action where you want to perform the deletion.
Laravel will take care of the dependency injection by itself.
Storing Web Sessions or API Tokens in DynamoDB (Useful for multiserver/container implementation)
If you have a deployment architecture, that involves multiple servers and you want to maintain the web sessions or API tokens across the servers, you can use the AWS DynamoDB. The library is capable of handling the DynamoDB with ease. All that you need to do is create the table in AWS DynamoDB and change a few configurations.
Creating a new table in AWS DynamoDB
- Go to the AWS Console and create a new table.
- Enter the unique table name as per your preferences.
- The primary key (or partition key) should be key of type string
- Use default settings and click the Create button
Update the .env file for Dynamo DB configurations
Add/Edit the following fields to your .env
file and set the values according to your AWS settings:
Refer the AWS DynamoDB Documentation and refer the endpoints provided in Service endpoints section.
Update the DynamoDB table for the TTL columns as expires_at
Automatic User Password update for API usage (for New Cognito Users)
In case of the new cognito users, the AWS SDK will send a session key and the user is expected to change the password, in a forced mode. Make sure you force the users to change the password for the first login by new cognito user.
However, if you have an API based implementation, and want to automatically authenticate the user without forcing the password change, you may do that with below setting fields to your .env
file
Support for App Client without Secret enabled
The library now supports where the AWS configuration of App Client with the Client Secret set to disabled. Use the below configuration into the environment file to enable/disable this. The default is marked as enable (i.e. we expect the App Client Secret to be enabled in AWS Cognito configuration)
Password Validation based of Cognito Configuration
This library fetches the password policy from the cognito pool configurations. The laravel request validations are done based on the regular expression that is created based on this policy. This validations are performed during the Sign Up (Registation), Sign In (Login), Reset and Change password based flows. The validation messages for the password are also dynamic in nature and change based on the configurations.
[!IMPORTANT] In case of special characters, we are supporting all except the pipe character | for now.
Mapping Cognito User using Subject UUID
The library maps the Cognito user subject UUID with the local repository. Everytime a new user is created in cognito, the sub UUID is mapped with the local user table with an user specified column name.
The column in the local BD is identified with the config parameter user_subject_uuid
with the default value set to sub
.
However, to customize the column name in the local DB user table, you may do that with below setting fields to your .env
file
We are working on making sure that pipe character is handled soon.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security
If you discover any security related issues, please email [email protected] and also add it to the issue tracker.
Roadmap
https://github.com/ellaisys/aws-cognito/wiki/RoadMap
How to contribute
- Star this project on GitHub.
- Report bugs or suggest features by creating new issues or adding comments to issues
- Submit pull requests
- Spread the word by blogging about SimplCommerce or sharing it on social networks
- Donate to us
Credits & Contributors
This project exists thanks to all the people who contribute.
Click on these badges to see how you might be able to help:
Support us
EllaiSys was a web and consulting agency specialized in Cloud Computing (AWS and Azure), DevOps, and Product Engneering. We closed our professional services offerings from Oct 2021, however the team continues to support the open source projects as our commitment towards the community. Anyone interested to support the development is welcome.
License
The MIT License (MIT). Please see License File for more information.
Disclaimer
This package is currently in production ready mode with already a few implementations done. We would be happy to hear from you, about the defects or new feature enhancements. However, this being a free support, we would not be able to commit to support SLAs or timelines.
All versions of aws-cognito with dependencies
guzzlehttp/guzzle Version ^6.0|^7.0
firebase/php-jwt Version ^6.0
aws/aws-sdk-php Version >=3.0