Forked from jonsa/app\Http\Controllers\AuthenticationController.php
Created
March 10, 2016 04:25
-
-
Save abcsun/fb0e60a82e3db7e0a340 to your computer and use it in GitHub Desktop.
Minimal Lumen framework configuration with Dingo and JWT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Tymon\JWTAuth\JWTAuth; | |
use Illuminate\Http\Request; | |
use Tymon\JWTAuth\Exceptions\JWTException; | |
/** | |
* Class AuthenticationController | |
* @package App\Http\Controllers | |
*/ | |
class AuthenticationController extends Controller | |
{ | |
/** | |
* @var JWTAuth | |
*/ | |
private $auth; | |
/** | |
* @param JWTAuth $auth | |
*/ | |
public function __construct(JWTAuth $auth) | |
{ | |
$this->auth = $auth; | |
} | |
/** | |
* @param Request $request | |
* @return \Symfony\Component\HttpFoundation\Response | |
*/ | |
public function authenticate(Request $request) | |
{ | |
// grab credentials from the request | |
$credentials = $request->only('email', 'password'); | |
try { | |
// attempt to verify the credentials and create a token for the user | |
$token = $this->auth->attempt($credentials); | |
if (!$token) { | |
return response()->json(['error' => 'invalid_credentials'], 401); | |
} | |
} catch (JWTException $e) { | |
// something went wrong whilst attempting to encode the token | |
return response()->json(['error' => 'could_not_create_token'], 500); | |
} | |
// all good so return the token | |
return response()->json(compact('token')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| Application Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register all of the routes for an application. | |
| It is a breeze. Simply tell Lumen the URIs it should respond to | |
| and give it the Closure to call when that URI is requested. | |
| | |
*/ | |
use App\Http\Controllers\AuthenticationController; | |
$app->post('authenticate', [ | |
'uses' => AuthenticationController::class . '@authenticate', | |
'as' => 'sign_in' | |
]); | |
$api->group(['middleware' => 'api.auth'], function () use ($app, $api) { | |
$api->get('/todo', function () use ($app, $api) { | |
$user = $app['tymon.jwt.auth']->toUser(); | |
return ['todos' => [ | |
'items' => ['Code awesome stuff', 'Feed the cat'], | |
'owner' => $user->id, | |
'name' => $user->name, | |
]]; | |
}); | |
}); | |
$app->get('/', function () { | |
$url = route('sign_in'); | |
return <<<HTML | |
<form method="post" action="$url"> | |
<input type="email" name="email"> | |
<input type="text" name="password"> | |
<input type="submit" value="Submit"> | |
</form> | |
HTML; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Tymon\JWTAuth\JWTAuth; | |
use Dingo\Api\Auth\Auth as DingoAuth; | |
use Illuminate\Support\ServiceProvider; | |
use Dingo\Api\Auth\Provider\JWT as JWTProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->extend('api.auth', function (DingoAuth $auth) { | |
$auth->extend('jwt', function ($app) { | |
return new JWTProvider($app[JWTAuth::class]); | |
}); | |
return $auth; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Auth\Authenticatable; | |
use Laravel\Lumen\Auth\Authorizable; | |
use Illuminate\Database\Eloquent\Model; | |
use Tymon\JWTAuth\Contracts\JWTSubject; | |
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; | |
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; | |
/** | |
* Class User | |
* @package App | |
*/ | |
class User extends Model implements | |
AuthenticatableContract, | |
AuthorizableContract, | |
JWTSubject | |
{ | |
use Authenticatable, Authorizable; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'name', 'email', | |
]; | |
/** | |
* The attributes excluded from the model's JSON form. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', | |
]; | |
/** | |
* Get the identifier that will be stored in the subject claim of the JWT. | |
* | |
* @return mixed | |
*/ | |
public function getJWTIdentifier() | |
{ | |
return $this->getKey(); | |
} | |
/** | |
* Return a key value array, containing any custom claims to be added to the JWT. | |
* | |
* @return array | |
*/ | |
public function getJWTCustomClaims() | |
{ | |
return []; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__.'/../vendor/autoload.php'; | |
try { | |
(new Dotenv\Dotenv(__DIR__.'/../'))->load(); | |
} catch (Dotenv\Exception\InvalidPathException $e) { | |
// | |
} | |
/* | |
|-------------------------------------------------------------------------- | |
| Create The Application | |
|-------------------------------------------------------------------------- | |
| | |
| Here we will load the environment and create the application instance | |
| that serves as the central piece of this framework. We'll use this | |
| application as an "IoC" container and router for this framework. | |
| | |
*/ | |
$app = new Laravel\Lumen\Application( | |
realpath(__DIR__.'/../') | |
); | |
// $app->withFacades(); | |
$app->withEloquent(); | |
/* | |
|-------------------------------------------------------------------------- | |
| Register Container Bindings | |
|-------------------------------------------------------------------------- | |
| | |
| Now we will register a few bindings in the service container. We will | |
| register the exception handler and the console kernel. You may add | |
| your own bindings here if you like or you can make another file. | |
| | |
*/ | |
$app->singleton( | |
Illuminate\Contracts\Debug\ExceptionHandler::class, | |
App\Exceptions\Handler::class | |
); | |
$app->singleton( | |
Illuminate\Contracts\Console\Kernel::class, | |
App\Console\Kernel::class | |
); | |
/* | |
|-------------------------------------------------------------------------- | |
| Register Middleware | |
|-------------------------------------------------------------------------- | |
| | |
| Next, we will register the middleware with the application. These can | |
| be global middleware that run before and after each request into a | |
| route or middleware that'll be assigned to some specific routes. | |
| | |
*/ | |
// $app->middleware([ | |
// App\Http\Middleware\ExampleMiddleware::class | |
// ]); | |
// $app->routeMiddleware([ | |
// 'auth' => App\Http\Middleware\Authenticate::class, | |
// ]); | |
/* | |
|-------------------------------------------------------------------------- | |
| Register Service Providers | |
|-------------------------------------------------------------------------- | |
| | |
| Here we will register all of the application's service providers which | |
| are used to bind services into the container. Service providers are | |
| totally optional, so you are not required to uncomment this line. | |
| | |
*/ | |
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class); | |
$app->register(Dingo\Api\Provider\LumenServiceProvider::class); | |
$app->register(App\Providers\AppServiceProvider::class); | |
// $app->register(App\Providers\AuthServiceProvider::class); | |
// $app->register(App\Providers\EventServiceProvider::class); | |
/* | |
|-------------------------------------------------------------------------- | |
| Load The Application Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Next we will include the routes file so that they can all be added to | |
| the application. This will provide all of the URLs the application | |
| can respond to, as well as the controllers that may handle them. | |
| | |
*/ | |
$app['api.router']->version('v1', function ($api) use ($app) { | |
require __DIR__ . '/../app/Http/routes.php'; | |
}); | |
return $app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "laravel/lumen", | |
"description": "The Laravel Lumen Framework.", | |
"keywords": ["framework", "laravel", "lumen"], | |
"license": "MIT", | |
"type": "project", | |
"require": { | |
"php": ">=5.5.9", | |
"laravel/lumen-framework": "5.2.*", | |
"vlucas/phpdotenv": "~2.2", | |
"dingo/api": "dev-master#595436348703f03917cad512ee5bb67fcb9004fc", | |
"tymon/jwt-auth": "0.6.*@dev" | |
}, | |
"require-dev": { | |
"fzaninotto/faker": "~1.4", | |
"phpunit/phpunit": "~4.0" | |
}, | |
"autoload": { | |
"psr-4": { | |
"App\\": "app/" | |
} | |
}, | |
"autoload-dev": { | |
"classmap": [ | |
"tests/", | |
"database/" | |
] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
/* | |
|-------------------------------------------------------------------------- | |
| Authentication Defaults | |
|-------------------------------------------------------------------------- | |
| | |
| This option controls the default authentication "guard" and password | |
| reset options for your application. You may change these defaults | |
| as required, but they're a perfect start for most applications. | |
| | |
*/ | |
'defaults' => [ | |
'guard' => env('AUTH_GUARD', 'api'), | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| Authentication Guards | |
|-------------------------------------------------------------------------- | |
| | |
| Next, you may define every authentication guard for your application. | |
| Of course, a great default configuration has been defined for you | |
| here which uses session storage and the Eloquent user provider. | |
| | |
| All authentication drivers have a user provider. This defines how the | |
| users are actually retrieved out of your database or other storage | |
| mechanisms used by this application to persist your user's data. | |
| | |
| Supported: "session", "token" | |
| | |
*/ | |
'guards' => [ | |
'api' => ['provider' => 'jwt', 'driver' => 'jwt'], | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| User Providers | |
|-------------------------------------------------------------------------- | |
| | |
| All authentication drivers have a user provider. This defines how the | |
| users are actually retrieved out of your database or other storage | |
| mechanisms used by this application to persist your user's data. | |
| | |
| If you have multiple user tables or models you may configure multiple | |
| sources which represent each model / table. These sources may then | |
| be assigned to any extra authentication guards you have defined. | |
| | |
| Supported: "database", "eloquent" | |
| | |
*/ | |
'providers' => [ | |
'jwt' => ['driver' => 'eloquent', 'model' => App\User::class] | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| Resetting Passwords | |
|-------------------------------------------------------------------------- | |
| | |
| Here you may set the options for resetting passwords including the view | |
| that is your password reset e-mail. You may also set the name of the | |
| table that maintains all of the reset tokens for your application. | |
| | |
| You may specify multiple password reset configurations if you have more | |
| than one user table or model in the application and you want to have | |
| separate password reset settings based on the specific user types. | |
| | |
| The expire time is the number of minutes that the reset token should be | |
| considered valid. This security feature keeps tokens short-lived so | |
| they have less time to be guessed. You may change this as needed. | |
| | |
*/ | |
'passwords' => [ | |
// | |
], | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of jwt-auth. | |
* | |
* (c) Sean Tymon <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
return [ | |
/* | |
|-------------------------------------------------------------------------- | |
| JWT Authentication Secret | |
|-------------------------------------------------------------------------- | |
| | |
| Don't forget to set this in your .env file, as it will be used to sign | |
| your tokens. A helper command is provided for this: | |
| `php artisan jwt:secret` | |
| | |
| Note: This will be used for Symmetric algorithms only (HMAC), | |
| since RSA and ECDSA use a private/public key combo (See below). | |
| | |
*/ | |
'secret' => env('JWT_SECRET'), | |
/* | |
|-------------------------------------------------------------------------- | |
| JWT Authentication Keys | |
|-------------------------------------------------------------------------- | |
| | |
| What algorithm you are using, will determine whether your tokens are | |
| signed with a random string (defined in `JWT_SECRET`) or using the | |
| following public & private keys. | |
| | |
| Symmetric Algorithms: | |
| HS256, HS384 & HS512 will use `JWT_SECRET`. | |
| | |
| Asymmetric Algorithms: | |
| RS256, RS384 & RS512 / ES256, ES384 & ES512 will use the keys below. | |
| | |
*/ | |
'keys' => [ | |
/* | |
|-------------------------------------------------------------------------- | |
| Public Key | |
|-------------------------------------------------------------------------- | |
| | |
| A path or resource to your public key. | |
| | |
| E.g. 'file://path/to/public/key' | |
| | |
*/ | |
'public' => env('JWT_PUBLIC_KEY'), | |
/* | |
|-------------------------------------------------------------------------- | |
| Private Key | |
|-------------------------------------------------------------------------- | |
| | |
| A path or resource to your private key. | |
| | |
| E.g. 'file://path/to/private/key' | |
| | |
*/ | |
'private' => env('JWT_PRIVATE_KEY'), | |
/* | |
|-------------------------------------------------------------------------- | |
| Passphrase | |
|-------------------------------------------------------------------------- | |
| | |
| The passphrase for your private key. Can be null if none set. | |
| | |
*/ | |
'passphrase' => env('JWT_PASSPHRASE'), | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| JWT time to live | |
|-------------------------------------------------------------------------- | |
| | |
| Specify the length of time (in minutes) that the token will be valid for. | |
| Defaults to 1 hour. | |
| | |
| You can also set this to null, to yield a never expiring token. | |
| Some people may want this behaviour for e.g. a mobile app. | |
| This is not particularly recommended, so make sure you have appropriate | |
| systems in place to revoke the token if necessary. | |
| | |
*/ | |
'ttl' => env('JWT_TTL', 60), | |
/* | |
|-------------------------------------------------------------------------- | |
| Refresh time to live | |
|-------------------------------------------------------------------------- | |
| | |
| Specify the length of time (in minutes) that the token can be refreshed | |
| within. I.E. The user can refresh their token within a 2 week window of | |
| the original token being created until they must re-authenticate. | |
| Defaults to 2 weeks. | |
| | |
*/ | |
'refresh_ttl' => env('JWT_REFRESH_TTL', 20160), | |
/* | |
|-------------------------------------------------------------------------- | |
| JWT hashing algorithm | |
|-------------------------------------------------------------------------- | |
| | |
| Specify the hashing algorithm that will be used to sign the token. | |
| | |
| See here: https://github.com/namshi/jose/tree/master/src/Namshi/JOSE/Signer/OpenSSL | |
| for possible values. | |
| | |
*/ | |
'algo' => env('JWT_ALGO', 'HS256'), | |
/* | |
|-------------------------------------------------------------------------- | |
| Required Claims | |
|-------------------------------------------------------------------------- | |
| | |
| Specify the required claims that must exist in any token. | |
| A TokenInvalidException will be thrown if any of these claims are not | |
| present in the payload. | |
| | |
*/ | |
'required_claims' => ['iss', 'iat', 'exp', 'nbf', 'sub', 'jti'], | |
/* | |
|-------------------------------------------------------------------------- | |
| Blacklist Enabled | |
|-------------------------------------------------------------------------- | |
| | |
| In order to invalidate tokens, you must have the the blacklist enabled. | |
| If you do not want or need this functionality, then set this to false. | |
| | |
*/ | |
'blacklist_enabled' => env('JWT_BLACKLIST_ENABLED', true), | |
/* | |
| ------------------------------------------------------------------------- | |
| Blacklist Grace Period | |
| ------------------------------------------------------------------------- | |
| | |
| When multiple concurrent requests are made with the same JWT, | |
| it is possible that some of them fail, due to token regeneration | |
| on every request. | |
| | |
| Set grace period in seconds to prevent parallel request failure. | |
| | |
*/ | |
'blacklist_grace_period' => env('JWT_BLACKLIST_GRACE_PERIOD', 0), | |
/* | |
|-------------------------------------------------------------------------- | |
| Providers | |
|-------------------------------------------------------------------------- | |
| | |
| Specify the various providers used throughout the package. | |
| | |
*/ | |
'providers' => [ | |
/* | |
|-------------------------------------------------------------------------- | |
| JWT Provider | |
|-------------------------------------------------------------------------- | |
| | |
| Specify the provider that is used to create and decode the tokens. | |
| | |
*/ | |
'jwt' => Tymon\JWTAuth\Providers\JWT\Namshi::class, | |
/* | |
|-------------------------------------------------------------------------- | |
| Authentication Provider | |
|-------------------------------------------------------------------------- | |
| | |
| Specify the provider that is used to authenticate users. | |
| | |
*/ | |
'auth' => Tymon\JWTAuth\Providers\Auth\Illuminate::class, | |
/* | |
|-------------------------------------------------------------------------- | |
| Storage Provider | |
|-------------------------------------------------------------------------- | |
| | |
| Specify the provider that is used to store tokens in the blacklist. | |
| | |
*/ | |
'storage' => Tymon\JWTAuth\Providers\Storage\Illuminate::class, | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment