PHPackages                             klsoft/yii2-jwt-auth - 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. klsoft/yii2-jwt-auth

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

klsoft/yii2-jwt-auth
====================

The package provides a Yii 2 authentication method based on a JWT token.

1.0.0(4mo ago)00MITPHPPHP &gt;=7.4

Since Feb 9Pushed 4mo agoCompare

[ Source](https://github.com/klsoft-web/yii2-jwt-auth)[ Packagist](https://packagist.org/packages/klsoft/yii2-jwt-auth)[ Docs](https://github.com/klsoft-web/yii2-jwt-auth)[ RSS](/packages/klsoft-yii2-jwt-auth/feed)WikiDiscussions main Synced today

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

YII2-JWT-AUTH
=============

[](#yii2-jwt-auth)

The package provides a [Yii 2](https://www.yiiframework.com) authentication method based on a JWT token.

See also:

- [YII2-KEYCLOAK-AUTHZ](https://github.com/klsoft-web/yii2-keycloak-authz) - The package provides Keycloak authorization for the web service APIs of [Yii 2](https://www.yiiframework.com)
- [PHP-KEYCLOAK-CLIENT](https://github.com/klsoft-web/php-keycloak-client) - A PHP library that can be used to secure web applications with Keycloak

Requirement
-----------

[](#requirement)

- PHP 7.4 or higher.

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

[](#installation)

```
composer require klsoft/yii2-jwt-auth
```

How to use
----------

[](#how-to-use)

The package requires the implementation of the findIdentityByAccessToken method of Yii\\Web\\IdentityInterface.

### 1. Implement Klsoft\\Yii2JwtAuth\\JwksRepositoryInterface

[](#1-implement-klsoftyii2jwtauthjwksrepositoryinterface)

Example:

```
namespace MyNamespace;

use Yii;
use Klsoft\Yii2JwtAuth\JwksRepositoryInterface;

final class JwksRepository implements JwksRepositoryInterface
{
    private int $jwksCacheDuration;
    private string $jwksUrl;
    private const JWKS = 'jwks';

    public function __construct(
        string $jwksUrl,
        int    $jwksCacheDuration)
    {
        $this->jwksUrl = $jwksUrl;
        $this->jwksCacheDuration = $jwksCacheDuration;
    }

    public function getKeys(): ?array
    {
        $keys = Yii::$app->cache->getOrSet(
            JwksRepository::JWKS,
            function () {
                $options = [
                    'http' => [
                        'method' => 'GET'
                    ],
                ];
                $responseData = file_get_contents($this->jwksUrl, false, stream_context_create($options));
                if (!empty($responseData)) {
                    return json_decode($responseData, true);
                }
                return [];
            },
            $this->jwksCacheDuration);

        if (empty($keys)) {
            Yii::$app->cache->delete(JwksRepository::JWKS);
            return null;
        } else {
            return $keys;
        }
    }
}
```

### 2. Add the JWKS URL to param.php

[](#2-add-the-jwks--url-to-paramphp)

Example:

```
return [
    'jwksUrl' => 'http://localhost:8080/realms/myrealm/protocol/openid-connect/certs',
    'jwksCacheDuration' => 60 * 3
];
```

### 3. Register dependencies

[](#3-register-dependencies)

Example of registering dependencies using the application configuration:

```
use Klsoft\Yii2JwtAuth\JwksRepositoryInterface;
use Klsoft\Yii2JwtAuth\HttpJwtAuth;

'container' => [
        'definitions' => [
            HttpJwtAuth::class => [
                HttpJwtAuth::class,
                [Instance::of(JwksRepositoryInterface::class)]
            ],
        ],
        'singletons' => [
            JwksRepositoryInterface::class => [
                JwksRepository::class,
                [
                    $params['jwksUrl'],
                    $params['jwksCacheDuration']
                ]
            ],
        ]
    ]
```

### 4. Configure the `authentication` behavior

[](#4-configure-the-authentication-behavior)

Example:

```
use yii\rest\Controller;
use Klsoft\Yii2JwtAuth\HttpJwtAuth;

final class MyController extends Controller
{
    private HttpJwtAuth $httpJwtAuth;

    public function __construct(HttpJwtAuth $httpJwtAuth)
    {
        $this->httpJwtAuth = $httpJwtAuth;
    }

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authentication'] = $this->httpJwtAuth;
        return $behaviors;
    }
}
```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance74

Regular maintenance activity

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 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

144d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f4e8ac50e4ad22be84b07f4c06d28cf280d22f689c460cd385c556727e638827?d=identicon)[klsoft-web](/maintainers/klsoft-web)

---

Top Contributors

[![klsoft-web](https://avatars.githubusercontent.com/u/7967163?v=4)](https://github.com/klsoft-web "klsoft-web (1 commits)")

---

Tags

authenticationjwtyii2jwtapiAuthenticationyii2keycloak

### Embed Badge

![Health badge](/badges/klsoft-yii2-jwt-auth/health.svg)

```
[![Health](https://phpackages.com/badges/klsoft-yii2-jwt-auth/health.svg)](https://phpackages.com/packages/klsoft-yii2-jwt-auth)
```

###  Alternatives

[auth0/auth0-php

PHP SDK for Auth0 Authentication and Management APIs.

41021.9M91](/packages/auth0-auth0-php)[ellaisys/aws-cognito

Laravel Authentication using AWS Cognito (Web and API)

123256.9k1](/packages/ellaisys-aws-cognito)[vizir/laravel-keycloak-web-guard

Simple Keycloak Guard to Laravel Web Routes

168617.7k](/packages/vizir-laravel-keycloak-web-guard)[bizley/jwt

JWT integration for Yii 2

69478.8k2](/packages/bizley-jwt)[auth0/symfony

Symfony SDK for Auth0 Authentication and Management APIs.

128814.6k](/packages/auth0-symfony)[kakadu-dev/yii2-jwt-auth

Extension provide JWT auth for Yii2

105.8k](/packages/kakadu-dev-yii2-jwt-auth)

PHPackages © 2026

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