PHPackages                             cakedc/oauth2-cognito - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. cakedc/oauth2-cognito

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

cakedc/oauth2-cognito
=====================

Cognito OAuth 2.0 Client Provider for The PHP League OAuth2-Client

2.1.0(1y ago)18597.7k—6.1%8[2 issues](https://github.com/CakeDC/oauth2-cognito/issues)[2 PRs](https://github.com/CakeDC/oauth2-cognito/pulls)MITPHPPHP &gt;=7.3

Since Feb 13Pushed 1y ago10 watchersCompare

[ Source](https://github.com/CakeDC/oauth2-cognito)[ Packagist](https://packagist.org/packages/cakedc/oauth2-cognito)[ RSS](/packages/cakedc-oauth2-cognito/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (5)Used By (0)

Amazon Cognito Provider for OAuth 2.0 Client
============================================

[](#amazon-cognito-provider-for-oauth-20-client)

[![Latest Version](https://camo.githubusercontent.com/8d3736235ffb28aaae4f4b01ea963b1a087477039232c05baeb215505b832483/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f43616b6544432f6f61757468322d636f676e69746f2e7376673f7374796c653d666c61742d737175617265)](https://github.com/CakeDC/oauth2-cognito/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/9f7e32b5366fc5632a1112f6a8a6ceb0113760f901258a67f1df2006129e03aa/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f43616b6544432f6f61757468322d636f676e69746f2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/CakeDC/oauth2-cognito)[![Coverage Status](https://camo.githubusercontent.com/1ff348a518d5053e41d17b41e3510bc6cfdd367c2f817a8ceda6782aa9f6b4d8/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f43616b6544432f6f61757468322d636f676e69746f2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/CakeDC/oauth2-cognito/code-structure)[![Quality Score](https://camo.githubusercontent.com/19c12f4bc7abdca24a22aa20435fc8f3302f454493284bbe1e95eeef32b532b8/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f43616b6544432f6f61757468322d636f676e69746f2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/CakeDC/oauth2-cognito)[![Total Downloads](https://camo.githubusercontent.com/d719a89796dd57351f358ec45f41c78dfa2f07fee329e21f2eb6bdaa5691ab2a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f43616b6544432f6f61757468322d636f676e69746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/CakeDC/oauth2-cognito)

This package provides Amazon Cognito OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).

Installation
------------

[](#installation)

To install, use composer:

```
composer require cakedc/oauth2-cognito

```

Usage
-----

[](#usage)

Usage is the same as The League's OAuth client, using `\CakeDC\OAuth2\Client\Provider\Cognito` as the provider.

### Authorization Code Flow

[](#authorization-code-flow)

```
$provider = new CakeDC\OAuth2\Client\Provider\Cognito([
    'clientId'          => '{cognito-client-id}',
    'clientSecret'      => '{cognito-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
    'cognitoDomain'     => '{cognito-client-domain}',
    'region'            => '{cognito-region}'
]);

if (!isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the user's details
        $user = $provider->getResourceOwner($token);

        // Use these details to create a new profile
        printf('Hello %s!', $user->getEmail());

    } catch (Exception $e) {

        // Failed to get user details
        exit(':(');
    }
}
```

### Managing Scopes

[](#managing-scopes)

When creating your Amazon Cognito authorization URL, you can specify the state and scopes your application may authorize.

```
$options = [
    'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
    'scope' => ['email','phone','profile']
];

$authorizationUrl = $provider->getAuthorizationUrl($options);
```

If neither are defined, the provider will utilize internal defaults.

At the time of authoring this documentation, the following scopes are available:

- phone
- email
- profile
- openid (required for phone, email or profile)
- aws.cognito.signin.user.admin

Hosted domain
-------------

[](#hosted-domain)

Optionally, if you are using your own domain for you client, you can configure it instead of `cognitoDomain` option when the provider is initialized:

```
$provider = new CakeDC\OAuth2\Client\Provider\Cognito([
    'clientId'          => '{cognito-client-id}',
    'clientSecret'      => '{cognito-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
    'hostedDomain'      => '{cognito-hosted-domain}', //Full domain without trailing slash
]);
```

Testing
-------

[](#testing)

```
$ ./vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/cakedc/oauth2-cognito/blob/master/CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Cake Development Corporation](https://github.com/cakedc)
- [Alejandro Ibarra](https://github.com/ajibarra)
- [All Contributors](https://github.com/cakedc/oauth2-cognito/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/cakedc/oauth2-cognito/blob/master/LICENSE) for more information.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~709 days

Total

4

Last Release

525d ago

Major Versions

1.0.1 → 2.0.02021-04-29

PHP version history (3 changes)1.0.0PHP ^5.6 || ^7.0

1.0.1PHP ^7.0

2.0.0PHP &gt;=7.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/204531?v=4)[Cake Development Corporation](/maintainers/CakeDC)[@CakeDC](https://github.com/CakeDC)

---

Top Contributors

[![ajibarra](https://avatars.githubusercontent.com/u/794722?v=4)](https://github.com/ajibarra "ajibarra (4 commits)")

---

Tags

clientAuthenticationoauthoauth2authorizationCognito

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/cakedc-oauth2-cognito/health.svg)

```
[![Health](https://phpackages.com/badges/cakedc-oauth2-cognito/health.svg)](https://phpackages.com/packages/cakedc-oauth2-cognito)
```

###  Alternatives

[league/oauth2-google

Google OAuth 2.0 Client Provider for The PHP League OAuth2-Client

42121.2M118](/packages/league-oauth2-google)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
