canis/lumen-jwt-auth

This package is abandoned and no longer maintained. No replacement package was suggested.

Light wrapper around lcobucci/jwt for Lumen

1.0.5 2016-02-23 22:23 UTC

This package is auto-updated.

Last update: 2020-01-24 17:02:00 UTC


README

This is a Guard driver for Lumen that adds JWT support using the Laravel Auth class. All the heavy lifting is from lcobucci/jwt.

Latest Stable Version Scrutinizer Code Quality Code Coverage Build Status License

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist canis/lumen-jwt-auth

or add

"canis/lumen-jwt-auth": "^1.0"

to the require section of your composer.json.

Copy config/jwt.php to your Lumen application's config directory. In your local .env file, set values for JWT_ISSUER and JWT_SECRET.

Documentation

In your bootstrap/app.php file, add:

$app->register(\Canis\Lumen\Jwt\ServiceProvider::class);

Sample config/auth.php file (with multiple providers):

<?php
return [
    'defaults' => [
        'guard' => env('AUTH_GUARD', 'user'),
    ],
    'guards' => [
        'user' => [
            'driver' => 'jwt',
            'provider' => 'user',
        ],
        'client' => [
            'driver' => 'jwt',
            'provider' => 'client',
        ],
    ],
    'providers' => [
        'user' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class
        ],
        'client' => [
            'driver' => 'eloquent',
            'model' => App\Models\Client::class
        ]
    ]
];

You can use the Authenticate middleware found in Lumen's skeleton (included below). Once loaded as auth in the routeMiddleware, you can add auth to the routes you'd like to use. If you want to specify the guard (see the auth.php file above), append it like so: auth:user or auth:client.

<?php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;

class Authenticate
{
    /**
     * The authentication guard factory instance.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
    protected $auth;

    /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Auth\Factory  $auth
     * @return void
     */
    public function __construct(Auth $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if ($this->auth->guard($guard)->guest()) {
            return response('Unauthorized.', 401);
        }
        return $next($request);
    }
}

Security Vulnerabilities

If you discover a security vulnerability within this library, please send an e-mail to security@canis.io. All security vulnerabilities will be promptly addressed.

License

This library is open-sourced software licensed under the MIT license.