PHPackages                             braincube-io/oauth2-php - 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. braincube-io/oauth2-php

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

braincube-io/oauth2-php
=======================

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

1.0.0(8y ago)126MITPHPPHP ^7.0

Since Oct 9Pushed 8y ago1 watchersCompare

[ Source](https://github.com/braincube-io/oauth2-php)[ Packagist](https://packagist.org/packages/braincube-io/oauth2-php)[ RSS](/packages/braincube-io-oauth2-php/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Braincube Provider for OAuth 2.0 Client
=======================================

[](#braincube-provider-for-oauth-20-client)

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

This package is compliant with [PSR-1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md), [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md), [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md), and [PSR-7](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md). If you notice compliance oversights, please send a patch via pull request.

Requirements
------------

[](#requirements)

- [Composer](https://getcomposer.org/) 1.4.1
- PHP 7.0

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

[](#installation)

Add the following to your `composer.json` file.

```
{
    "require": {
        "braincube-io/oauth2-php": "1.0.0"
    }
}
```

Usage
-----

[](#usage)

### Authorization Code Flow

[](#authorization-code-flow)

```
session_start();

require __DIR__ . '/vendor/autoload.php';

$provider = new \League\OAuth2\Client\Provider\Braincube([
    'clientId'          => '{braincube-app-id}', // The client ID assigned to you by the provider
    'clientSecret'      => '{braincube-app-secret}', // The client password assigned to you by the provider
    'redirectUri'       => 'https://example.com/callback-url'
]);

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    if (isset($_SESSION['token'])) {
        unset($_SESSION['token']);
    }

    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $provider->getAuthorizationUrl();

    // Get the state generated for you and store it to the session.
    $_SESSION['oauth2state'] = $provider->getState();

    echo 'Log in with Braincube!';
    exit;

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

    if (isset($_SESSION['oauth2state'])) {
            unset($_SESSION['oauth2state']);
    }

    exit('Invalid state');

} else {

    try {

        if (isset($_SESSION['token'])) {
            echo 'Access Token: ' . $_SESSION['token'] . "";
            echo 'Logout!';
        } else {
            // Try to get an access token using the authorization code grant.
            $accessToken = $provider->getAccessToken('authorization_code', [
                'code' => $_GET['code']
            ]);

            // We have an access token, which we may use in authenticated
            // requests against the service provider's API.
            echo 'Access Token: ' . $accessToken->getToken() . "";
            echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "";
            echo 'Expired in: ' . $accessToken->getExpires() . "";
            echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "";

            // Using the access token, we may look up details about the
            // resource owner.
            $resourceOwner = $provider->getResourceOwner($accessToken);

            prettify($resourceOwner->toArray());

            // The provider provides a way to get an authenticated API request for
            // the service, using the access token; it returns an object conforming
            // to Psr\Http\Message\RequestInterface.
            $request = $provider->getAuthenticatedRequest(
                'GET',
                'https://yourapi',
                $accessToken
            );

            $response = $provider->getParsedResponse($request);
            prettify($response);

            $_SESSION['token'] = $accessToken->getToken();
        }

    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

        // Failed to get the access token or user details.
        exit($e->getMessage());

    }

}

function prettify($code) {
    echo "";
    var_export($code);
    echo "";
}
```

### The BraincubeUser Entity

[](#the-braincubeuser-entity)

When using the `getResourceOwner()` method to obtain the user node, it will be returned as a `BraincubeUser` entity.

```
$user = $provider->getResourceOwner($token);

$email = $user->getEmail();
var_dump($email);
# string(15) "thezuck@foo.com"

$fullName = $user->getFullName();
var_dump($fullName);
# string(8) "The Zuck"

$allowedProducts = $user->getAllowedProducts();
var_dump($allowedProducts);
# array
```

You can also get all the data from the User node as a plain-old PHP array with `toArray()`.

```
$userData = $user->toArray();
```

Testing
-------

[](#testing)

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

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

[](#contributing)

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

License
-------

[](#license)

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

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

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

Unknown

Total

1

Last Release

3141d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a46b767dd6fcba71c8a94306dcfd563fa672cecb7da870d06e799a81da6dcb6c?d=identicon)[virgil.calabrese](/maintainers/virgil.calabrese)

---

Top Contributors

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

---

Tags

clientAuthenticationoauthoauth2authorizationbraincube

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/braincube-io-oauth2-php/health.svg)

```
[![Health](https://phpackages.com/badges/braincube-io-oauth2-php/health.svg)](https://phpackages.com/packages/braincube-io-oauth2-php)
```

###  Alternatives

[league/oauth2-google

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

41721.2M118](/packages/league-oauth2-google)[cakedc/oauth2-cognito

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

18597.7k](/packages/cakedc-oauth2-cognito)

PHPackages © 2026

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