Giter Site home page Giter Site logo

yiisoft / auth-jwt Goto Github PK

View Code? Open in Web Editor NEW
32.0 19.0 11.0 135 KB

Home Page: https://www.yiiframework.com/

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%
yii3 auth jwt-authentication middleware psr-15 hacktoberfest authentication

auth-jwt's Introduction

Yii Auth JWT


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

The package provides JWT authentication method for Yii Auth.

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/auth-jwt

General usage

Configuring within Yii

  1. Set JWT parameters in your params.php config file:

    'yiisoft/auth-jwt' => [
        'algorithms' => [
            // your signature algorithms
        ],
        'serializers' => [
            // your token serializers
        ],
        'key' => [
            'secret' => 'your-secret',
            'file' => 'your-certificate-file',
        ],
    ],
  2. Setup definitions, required for \Yiisoft\Auth\Middleware\Authentication middleware in a config, for example, in config/web/auth.php:

    /** @var array $params */
    
    use Yiisoft\Auth\Jwt\TokenManagerInterface;
    use Yiisoft\Auth\Jwt\TokenManager;
    use Yiisoft\Auth\AuthenticationMethodInterface;
    use Yiisoft\Auth\Jwt\JwtMethod;
    
    return [
        KeyFactoryInterface::class => [
            'class' => FromSecret::class,
            '__construct()' => [
                $params['yiisoft/auth-jwt']['key']['secret']
            ],
        ],
        
        AuthenticationMethodInterface::class => JwtMethod::class,
    ];

    Note: Don't forget to declare your implementations of \Yiisoft\Auth\IdentityInterface and \Yiisoft\Auth\IdentityRepositoryInterface.

  3. Use Yiisoft\Auth\Middleware\Authentication middleware. Read more about middlewares in the middleware guide.

Configuring independently

You can configure Authentication middleware manually:

/** @var \Yiisoft\Auth\IdentityRepositoryInterface $identityRepository */
$identityRepository = getIdentityRepository();

$tokenRepository = $container->get(\Yiisoft\Auth\Jwt\TokenRepositoryInterface::class);

$authenticationMethod = new \Yiisoft\Auth\Jwt\JwtMethod($identityRepository, $tokenRepository);

$middleware = new \Yiisoft\Auth\Middleware\Authentication(
    $authenticationMethod,
    $responseFactory, // PSR-17 ResponseFactoryInterface.
    $failureHandler // Optional, \Yiisoft\Auth\Handler\AuthenticationFailureHandler by default.
);

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Auth JWT is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

auth-jwt's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar devanych avatar eluhr avatar fantom409 avatar luizcmarin avatar rustamwin avatar samdark avatar sankaest avatar strorch avatar terabytesoftw avatar viktorprogger avatar vjik avatar wiperawa avatar xepozz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

auth-jwt's Issues

JWT encrypted with RS256 instead of HS256

What steps will reproduce the problem?

I am using an RS256 algorithm instead of the (default) HS256 algorithm. RS256 uses a public/private key pair instead of a secret. The TokenManager only implements the possibility to define a secret but not a public or private file path or encryption key.

config/web/auth.php

<?php

declare(strict_types=1);

/** 
 * @var array $params
 */

use Jose\Component\Signature\Algorithm\RS256;
use Yiisoft\Auth\Jwt\TokenManagerInterface;
use Yiisoft\Auth\Jwt\TokenManager;
use Yiisoft\Auth\AuthenticationMethodInterface;
use Yiisoft\Auth\Jwt\JwtMethod;

return [
    TokenManagerInterface::class => [
        '__class' => TokenManager::class,
        '__construct()' => [
             // `secret` not needed for this algorithm, but must be defined for the token manager constructor
            'secret' => $params['yiisoft/auth-jwt']['secret'],
            'algorithms' => [new RS256()]
           // where do i define the public/private key file path or the encryption key?
        ]
    ],
    AuthenticationMethodInterface::class => JwtMethod::class
];

What is the expected result?

A possibility to define a public and private key file path for the token manager and the secret not to be a default

What do you get instead?

No possibility to use a JWT with RS256 algorithm

Additional info

Q A
Version dev-master#cc62114
PHP version 7.4.12
Operating system Docker yiisoftware/yii-php:7.4-apache

Update `web-token/*` library

What steps will reproduce the problem?

composer update

What is the expected result?

Outdated packages are not installed.

What do you get instead?

composer: Package web-token/* is abandoned, you should avoid using it. Use web-token/jwt-library instead.

Additional info

Q A
Version 2.0.1
PHP version 8.3
Operating system macOS

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.