PHPackages                             longthanhtran/yii2-oauth2-server - 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. longthanhtran/yii2-oauth2-server

ActiveYii2-extension[Authentication &amp; Authorization](/categories/authentication)

longthanhtran/yii2-oauth2-server
================================

Yii2 OAuth2 authorization server

1.2.0(4y ago)06MITPHPPHP &gt;=7.4 || ^8.0

Since Sep 10Pushed 4y ago1 watchersCompare

[ Source](https://github.com/longthanhtran/yii2-oauth-server)[ Packagist](https://packagist.org/packages/longthanhtran/yii2-oauth2-server)[ RSS](/packages/longthanhtran-yii2-oauth2-server/feed)WikiDiscussions main Synced today

READMEChangelog (7)Dependencies (7)Versions (10)Used By (0)

Yii2 base OAuth2 authorization server
=====================================

[](#yii2-base-oauth2-authorization-server)

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

[](#installation)

```
composer require longthanhtran/yii2-oauth2-server
```

Notes
-----

[](#notes)

- The package serves as Yii2 module to perform 2 main functions of OAuth2 Authorization server. This bases on league/oauth2-server and can run on PHP 7.4 or 8.0.
- Sample module config Review and create a file name modules.php inside `config` folder with following content. Then append `'modules' => $modules,` right inside @app/config/web.php (behind $params key)

```
use longthanhtran\oauth2\Module;
use League\OAuth2\Server\Grant\AuthCodeGrant;
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
use League\OAuth2\Server\Grant\PasswordGrant;
use League\OAuth2\Server\Grant\RefreshTokenGrant;

return [
    'oauth2' => [
        'class' => 'longthanhtran\oauth2\Module',
        'privateKey' => __DIR__ . '/../keys/private.key',
        'publicKey' => __DIR__ . '/../keys/public.key',
        'encryptionKey' => "you-need-to-prepare-this-encryption-key",
        'enableGrantTypes' => function(Module $module) {
            $server = $module->authorizationServer;

            // Client Credentials Grant
            $server->enableGrantType(
                new ClientCredentialsGrant(),
                new DateInterval('PT1H') // expires after 1 hour
            );

            // Authorization Code Grant
            $authCodeGrant = new AuthCodeGrant(
                $module->authCodeRepository,
                $module->refreshTokenRepository,
                new DateInterval('PT10M') // expires after 10 minutes
            );
            $authCodeGrant->setRefreshTokenTTL(
                new DateInterval('P1M') // expires after 1 month
            );
            $server->enableGrantType(
                $authCodeGrant,
                new DateInterval('PT1H') // expires after 1 hour
            );

            // Refresh Token Grant
            $refreshTokenGrant = new RefreshTokenGrant(
                $module->refreshTokenRepository
            );
            $refreshTokenGrant->setRefreshTokenTTL(
                new DateInterval('P1M') // expires after 1 month
            );
            $server->enableGrantType(
                $refreshTokenGrant,
                new DateInterval('PT1H') // expires after 1 hour
            );
            // Password Grant - legacy grant
            $passwordGrant = new PasswordGrant(
                $module->userRepository,
                $module->refreshTokenRepository
            );
            $passwordGrant->setRefreshTokenTTL(new DateInterval('P1M'));
            $server->enableGrantType(
                $passwordGrant,
                new DateInterval('PT1H') // expires after 1 hour
            );
        }
    ]
];
```

Be sure to prepare the `privateKey`, `publicKey` (in @app/keys folder) and encryption Key. With `encryptionKey`, you can refer to [Cryptography](https://www.yiiframework.com/doc/guide/2.0/en/security-cryptography) guideline on Yii2's guide.

- To prepare the schema, run migration with

```
yii migrate --migrationPath=@vendor/longthanhtran/yii2-oauth2-server/oauth2/migrations
```

- To validate user's credential, you can implement UserEntityInterface for your User class, sample provide below. Be sure to `use UserQueryTrait` in `User`

```
namespace app\models;

use League\OAuth2\Server\Entities\ClientEntityInterface;

trait UserQueryTrait {

    public function getUserEntityByUserCredentials($username,
                                                   $password,
                                                   $grantType,
                                                   ClientEntityInterface $clientEntity)
    {
        $user = User::findOne(['username' => $username]);
        if ($user && $user->validatePassword($password)) {
            return $user;
        }
        return null;
    }
}
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

 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 ~1 days

Total

9

Last Release

1750d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/429373?v=4)[longtran](/maintainers/longtran)[@longtran](https://github.com/longtran)

---

Top Contributors

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

---

Tags

oauth2extensionmoduleyii

### Embed Badge

![Health badge](/badges/longthanhtran-yii2-oauth2-server/health.svg)

```
[![Health](https://phpackages.com/badges/longthanhtran-yii2-oauth2-server/health.svg)](https://phpackages.com/packages/longthanhtran-yii2-oauth2-server)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.6M574](/packages/shopware-core)[simplesamlphp/simplesamlphp-module-oidc

A SimpleSAMLphp module adding support for the OpenID Connect protocol

5018.2k1](/packages/simplesamlphp-simplesamlphp-module-oidc)[concrete5/core

Concrete core subtree split

20166.1k52](/packages/concrete5-core)[filsh/yii2-oauth2-server

OAuth2 Server for PHP

337549.0k12](/packages/filsh-yii2-oauth2-server)[jeremy379/laravel-openid-connect

OpenID Connect support to the PHP League's OAuth2 Server. Compatible with Laravel Passport.

59437.0k9](/packages/jeremy379-laravel-openid-connect)

PHPackages © 2026

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