mirror of
https://github.com/kevin-DL/exercise_tracker_api.git
synced 2026-01-11 11:04:32 +00:00
exercise_tracker_api-2 JWT
This commit is contained in:
@@ -5,14 +5,17 @@ namespace App\Http\Controllers;
|
|||||||
use App\User;
|
use App\User;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Response;
|
|
||||||
use Illuminate\Support\Facades\Auth;
|
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||||
|
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
|
||||||
|
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
|
||||||
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||||
|
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
public function register(Request $request): JsonResponse {
|
public function register(Request $request): JsonResponse
|
||||||
|
{
|
||||||
$validator = Validator::make($request->all(), [
|
$validator = Validator::make($request->all(), [
|
||||||
'username' => 'required|string|min:3|max:255|unique:users',
|
'username' => 'required|string|min:3|max:255|unique:users',
|
||||||
'email' => 'required|string|email|max:255|unique:users',
|
'email' => 'required|string|email|max:255|unique:users',
|
||||||
@@ -29,16 +32,48 @@ class UserController extends Controller
|
|||||||
'password' => Hash::make($request->get('password')),
|
'password' => Hash::make($request->get('password')),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return response()->json($user,201);
|
$token = JWTAuth::fromUser($user);
|
||||||
|
|
||||||
|
return response()->json(compact('user', 'token'), 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function login(Request $request): JsonResponse {
|
public function login(Request $request): JsonResponse
|
||||||
|
{
|
||||||
$credentials = $request->only('email', 'password');
|
$credentials = $request->only('email', 'password');
|
||||||
|
|
||||||
if (! Auth::attempt($credentials)) {
|
try {
|
||||||
return response()->json(['error' => 'invalid_credentials'], 400);
|
if (!$token = JWTAuth::attempt($credentials)) {
|
||||||
|
return response()->json(['error' => 'invalid_credentials'], 400);
|
||||||
|
}
|
||||||
|
} catch (JWTException $e) {
|
||||||
|
return response()->json(['error' => 'could_not_create_token'], 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json('yes');
|
return response()->json(compact('token'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAuthenticatedUser()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
|
||||||
|
if (!$user = JWTAuth::parseToken()->authenticate()) {
|
||||||
|
return response()->json(['user_not_found'], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (TokenExpiredException $e) {
|
||||||
|
|
||||||
|
return response()->json(['token_expired'], $e->getStatusCode());
|
||||||
|
|
||||||
|
} catch (TokenInvalidException $e) {
|
||||||
|
|
||||||
|
return response()->json(['token_invalid'], $e->getStatusCode());
|
||||||
|
|
||||||
|
} catch (JWTException $e) {
|
||||||
|
|
||||||
|
return response()->json(['token_absent'], $e->getStatusCode());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json(compact('user'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,5 +62,6 @@ class Kernel extends HttpKernel
|
|||||||
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
|
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
|
||||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||||
|
'jwt.verify' => \App\Http\Middleware\JwtMiddleware::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
38
app/Http/Middleware/JwtMiddleware.php
Normal file
38
app/Http/Middleware/JwtMiddleware.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Exception;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||||
|
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
|
||||||
|
|
||||||
|
|
||||||
|
class JwtMiddleware extends BaseMiddleware
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle an incoming request.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @param Closure $next
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle($request, Closure $next)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$user = JWTAuth::parseToken()->authenticate();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException){
|
||||||
|
return response()->json(['status' => 'Token is Invalid']);
|
||||||
|
}else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException){
|
||||||
|
return response()->json(['status' => 'Token is Expired']);
|
||||||
|
}else{
|
||||||
|
return response()->json(['status' => 'Authorization Token not found']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
13
app/User.php
13
app/User.php
@@ -5,8 +5,9 @@ namespace App;
|
|||||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
|
use Tymon\JWTAuth\Contracts\JWTSubject;
|
||||||
|
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable implements JWTSubject
|
||||||
{
|
{
|
||||||
use Notifiable;
|
use Notifiable;
|
||||||
|
|
||||||
@@ -36,4 +37,14 @@ class User extends Authenticatable
|
|||||||
protected $casts = [
|
protected $casts = [
|
||||||
'email_verified_at' => 'datetime',
|
'email_verified_at' => 'datetime',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function getJWTIdentifier()
|
||||||
|
{
|
||||||
|
return $this->getKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getJWTCustomClaims()
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,8 @@
|
|||||||
"fruitcake/laravel-cors": "^1.0",
|
"fruitcake/laravel-cors": "^1.0",
|
||||||
"guzzlehttp/guzzle": "^6.3",
|
"guzzlehttp/guzzle": "^6.3",
|
||||||
"laravel/framework": "^7.0",
|
"laravel/framework": "^7.0",
|
||||||
"laravel/tinker": "^2.0"
|
"laravel/tinker": "^2.0",
|
||||||
|
"tymon/jwt-auth": "^1.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"facade/ignition": "^2.0",
|
"facade/ignition": "^2.0",
|
||||||
|
|||||||
336
composer.lock
generated
336
composer.lock
generated
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "6558f74828bca9ebecac73d90cea4b1a",
|
"content-hash": "b14e843dc2793940e08e52b3b0f85b97",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "asm89/stack-cors",
|
"name": "asm89/stack-cors",
|
||||||
@@ -917,6 +917,61 @@
|
|||||||
],
|
],
|
||||||
"time": "2020-04-07T15:01:31+00:00"
|
"time": "2020-04-07T15:01:31+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "lcobucci/jwt",
|
||||||
|
"version": "3.3.1",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/lcobucci/jwt.git",
|
||||||
|
"reference": "a11ec5f4b4d75d1fcd04e133dede4c317aac9e18"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/lcobucci/jwt/zipball/a11ec5f4b4d75d1fcd04e133dede4c317aac9e18",
|
||||||
|
"reference": "a11ec5f4b4d75d1fcd04e133dede4c317aac9e18",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"ext-mbstring": "*",
|
||||||
|
"ext-openssl": "*",
|
||||||
|
"php": "^5.6 || ^7.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"mikey179/vfsstream": "~1.5",
|
||||||
|
"phpmd/phpmd": "~2.2",
|
||||||
|
"phpunit/php-invoker": "~1.1",
|
||||||
|
"phpunit/phpunit": "^5.7 || ^7.3",
|
||||||
|
"squizlabs/php_codesniffer": "~2.3"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "3.1-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Lcobucci\\JWT\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"BSD-3-Clause"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Luís Otávio Cobucci Oblonczyk",
|
||||||
|
"email": "lcobucci@gmail.com",
|
||||||
|
"role": "Developer"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "A simple library to work with JSON Web Token and JSON Web Signature",
|
||||||
|
"keywords": [
|
||||||
|
"JWS",
|
||||||
|
"jwt"
|
||||||
|
],
|
||||||
|
"time": "2019-05-24T18:30:49+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "league/commonmark",
|
"name": "league/commonmark",
|
||||||
"version": "1.4.2",
|
"version": "1.4.2",
|
||||||
@@ -1188,6 +1243,69 @@
|
|||||||
],
|
],
|
||||||
"time": "2019-12-20T14:22:59+00:00"
|
"time": "2019-12-20T14:22:59+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "namshi/jose",
|
||||||
|
"version": "7.2.3",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/namshi/jose.git",
|
||||||
|
"reference": "89a24d7eb3040e285dd5925fcad992378b82bcff"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/namshi/jose/zipball/89a24d7eb3040e285dd5925fcad992378b82bcff",
|
||||||
|
"reference": "89a24d7eb3040e285dd5925fcad992378b82bcff",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"ext-date": "*",
|
||||||
|
"ext-hash": "*",
|
||||||
|
"ext-json": "*",
|
||||||
|
"ext-pcre": "*",
|
||||||
|
"ext-spl": "*",
|
||||||
|
"php": ">=5.5",
|
||||||
|
"symfony/polyfill-php56": "^1.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpseclib/phpseclib": "^2.0",
|
||||||
|
"phpunit/phpunit": "^4.5|^5.0",
|
||||||
|
"satooshi/php-coveralls": "^1.0"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"ext-openssl": "Allows to use OpenSSL as crypto engine.",
|
||||||
|
"phpseclib/phpseclib": "Allows to use Phpseclib as crypto engine, use version ^2.0."
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Namshi\\JOSE\\": "src/Namshi/JOSE/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Alessandro Nadalin",
|
||||||
|
"email": "alessandro.nadalin@gmail.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Alessandro Cinelli (cirpo)",
|
||||||
|
"email": "alessandro.cinelli@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "JSON Object Signing and Encryption library for PHP.",
|
||||||
|
"keywords": [
|
||||||
|
"JSON Web Signature",
|
||||||
|
"JSON Web Token",
|
||||||
|
"JWS",
|
||||||
|
"json",
|
||||||
|
"jwt",
|
||||||
|
"token"
|
||||||
|
],
|
||||||
|
"time": "2016-12-05T07:27:31+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "nesbot/carbon",
|
"name": "nesbot/carbon",
|
||||||
"version": "2.32.2",
|
"version": "2.32.2",
|
||||||
@@ -2979,6 +3097,76 @@
|
|||||||
],
|
],
|
||||||
"time": "2020-03-09T19:04:49+00:00"
|
"time": "2020-03-09T19:04:49+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "symfony/polyfill-php56",
|
||||||
|
"version": "v1.15.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/symfony/polyfill-php56.git",
|
||||||
|
"reference": "d51ec491c8ddceae7dca8dd6c7e30428f543f37d"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/symfony/polyfill-php56/zipball/d51ec491c8ddceae7dca8dd6c7e30428f543f37d",
|
||||||
|
"reference": "d51ec491c8ddceae7dca8dd6c7e30428f543f37d",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.3.3",
|
||||||
|
"symfony/polyfill-util": "~1.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "1.15-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Symfony\\Polyfill\\Php56\\": ""
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"bootstrap.php"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Nicolas Grekas",
|
||||||
|
"email": "p@tchwork.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions",
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"keywords": [
|
||||||
|
"compatibility",
|
||||||
|
"polyfill",
|
||||||
|
"portable",
|
||||||
|
"shim"
|
||||||
|
],
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"url": "https://symfony.com/sponsor",
|
||||||
|
"type": "custom"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://github.com/fabpot",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||||
|
"type": "tidelift"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"time": "2020-03-09T19:04:49+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "symfony/polyfill-php72",
|
"name": "symfony/polyfill-php72",
|
||||||
"version": "v1.15.0",
|
"version": "v1.15.0",
|
||||||
@@ -3120,6 +3308,72 @@
|
|||||||
],
|
],
|
||||||
"time": "2020-02-27T09:26:54+00:00"
|
"time": "2020-02-27T09:26:54+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "symfony/polyfill-util",
|
||||||
|
"version": "v1.15.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/symfony/polyfill-util.git",
|
||||||
|
"reference": "d8e76c104127675d0ea3df3be0f2ae24a8619027"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/symfony/polyfill-util/zipball/d8e76c104127675d0ea3df3be0f2ae24a8619027",
|
||||||
|
"reference": "d8e76c104127675d0ea3df3be0f2ae24a8619027",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.3.3"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "1.15-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Symfony\\Polyfill\\Util\\": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Nicolas Grekas",
|
||||||
|
"email": "p@tchwork.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Symfony utilities for portability of PHP codes",
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"keywords": [
|
||||||
|
"compat",
|
||||||
|
"compatibility",
|
||||||
|
"polyfill",
|
||||||
|
"shim"
|
||||||
|
],
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"url": "https://symfony.com/sponsor",
|
||||||
|
"type": "custom"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://github.com/fabpot",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||||
|
"type": "tidelift"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"time": "2020-03-02T11:55:35+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "symfony/process",
|
"name": "symfony/process",
|
||||||
"version": "v5.0.7",
|
"version": "v5.0.7",
|
||||||
@@ -3617,6 +3871,86 @@
|
|||||||
"homepage": "https://github.com/tijsverkoyen/CssToInlineStyles",
|
"homepage": "https://github.com/tijsverkoyen/CssToInlineStyles",
|
||||||
"time": "2019-10-24T08:53:34+00:00"
|
"time": "2019-10-24T08:53:34+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "tymon/jwt-auth",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/tymondesigns/jwt-auth.git",
|
||||||
|
"reference": "d4cf9fd2b98790712d3e6cd1094e5ff018431f19"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/tymondesigns/jwt-auth/zipball/d4cf9fd2b98790712d3e6cd1094e5ff018431f19",
|
||||||
|
"reference": "d4cf9fd2b98790712d3e6cd1094e5ff018431f19",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"illuminate/auth": "^5.2|^6|^7",
|
||||||
|
"illuminate/contracts": "^5.2|^6|^7",
|
||||||
|
"illuminate/http": "^5.2|^6|^7",
|
||||||
|
"illuminate/support": "^5.2|^6|^7",
|
||||||
|
"lcobucci/jwt": "^3.2",
|
||||||
|
"namshi/jose": "^7.0",
|
||||||
|
"nesbot/carbon": "^1.0|^2.0",
|
||||||
|
"php": "^5.5.9|^7.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"illuminate/console": "^5.2|^6|^7",
|
||||||
|
"illuminate/database": "^5.2|^6|^7",
|
||||||
|
"illuminate/routing": "^5.2|^6|^7",
|
||||||
|
"mockery/mockery": ">=0.9.9",
|
||||||
|
"phpunit/phpunit": "~4.8|~6.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-develop": "1.0-dev"
|
||||||
|
},
|
||||||
|
"laravel": {
|
||||||
|
"aliases": {
|
||||||
|
"JWTAuth": "Tymon\\JWTAuth\\Facades\\JWTAuth",
|
||||||
|
"JWTFactory": "Tymon\\JWTAuth\\Facades\\JWTFactory"
|
||||||
|
},
|
||||||
|
"providers": [
|
||||||
|
"Tymon\\JWTAuth\\Providers\\LaravelServiceProvider"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Tymon\\JWTAuth\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Sean Tymon",
|
||||||
|
"email": "tymon148@gmail.com",
|
||||||
|
"homepage": "https://tymon.xyz",
|
||||||
|
"role": "Developer"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "JSON Web Token Authentication for Laravel and Lumen",
|
||||||
|
"homepage": "https://github.com/tymondesigns/jwt-auth",
|
||||||
|
"keywords": [
|
||||||
|
"Authentication",
|
||||||
|
"JSON Web Token",
|
||||||
|
"auth",
|
||||||
|
"jwt",
|
||||||
|
"laravel"
|
||||||
|
],
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"url": "https://www.patreon.com/seantymon",
|
||||||
|
"type": "patreon"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"time": "2020-03-04T11:21:28+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "vlucas/phpdotenv",
|
"name": "vlucas/phpdotenv",
|
||||||
"version": "v4.1.4",
|
"version": "v4.1.4",
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ return [
|
|||||||
/*
|
/*
|
||||||
* Package Service Providers...
|
* Package Service Providers...
|
||||||
*/
|
*/
|
||||||
|
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
|
||||||
/*
|
/*
|
||||||
* Application Service Providers...
|
* Application Service Providers...
|
||||||
*/
|
*/
|
||||||
@@ -226,7 +226,8 @@ return [
|
|||||||
'URL' => Illuminate\Support\Facades\URL::class,
|
'URL' => Illuminate\Support\Facades\URL::class,
|
||||||
'Validator' => Illuminate\Support\Facades\Validator::class,
|
'Validator' => Illuminate\Support\Facades\Validator::class,
|
||||||
'View' => Illuminate\Support\Facades\View::class,
|
'View' => Illuminate\Support\Facades\View::class,
|
||||||
|
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
|
||||||
|
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
|
||||||
],
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|||||||
304
config/jwt.php
Normal file
304
config/jwt.php
Normal file
@@ -0,0 +1,304 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of jwt-auth.
|
||||||
|
*
|
||||||
|
* (c) Sean Tymon <tymon148@gmail.com>
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The 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.
|
||||||
|
| Notice: If you set this to null you should remove 'exp' element from 'required_claims' list.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'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.
|
||||||
|
|
|
||||||
|
| You can also set this to null, to yield an infinite refresh time.
|
||||||
|
| Some may want this instead of never expiring tokens 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.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'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',
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Persistent Claims
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Specify the claim keys to be persisted when refreshing a token.
|
||||||
|
| `sub` and `iat` will automatically be persisted, in
|
||||||
|
| addition to the these claims.
|
||||||
|
|
|
||||||
|
| Note: If a claim does not exist then it will be ignored.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'persistent_claims' => [
|
||||||
|
// 'foo',
|
||||||
|
// 'bar',
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Lock Subject
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This will determine whether a `prv` claim is automatically added to
|
||||||
|
| the token. The purpose of this is to ensure that if you have multiple
|
||||||
|
| authentication models e.g. `App\User` & `App\OtherPerson`, then we
|
||||||
|
| should prevent one authentication request from impersonating another,
|
||||||
|
| if 2 tokens happen to have the same id across the 2 different models.
|
||||||
|
|
|
||||||
|
| Under specific circumstances, you may want to disable this behaviour
|
||||||
|
| e.g. if you only have one authentication model, then you would save
|
||||||
|
| a little on token size.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'lock_subject' => true,
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Leeway
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This property gives the jwt timestamp claims some "leeway".
|
||||||
|
| Meaning that if you have any unavoidable slight clock skew on
|
||||||
|
| any of your servers then this will afford you some level of cushioning.
|
||||||
|
|
|
||||||
|
| This applies to the claims `iat`, `nbf` and `exp`.
|
||||||
|
|
|
||||||
|
| Specify in seconds - only if you know you need it.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'leeway' => env('JWT_LEEWAY', 0),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Blacklist Enabled
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| In order to invalidate tokens, you must have 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),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Cookies encryption
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| By default Laravel encrypt cookies for security reason.
|
||||||
|
| If you decide to not decrypt cookies, you will have to configure Laravel
|
||||||
|
| to not encrypt your cookie token by adding its name into the $except
|
||||||
|
| array available in the middleware "EncryptCookies" provided by Laravel.
|
||||||
|
| see https://laravel.com/docs/master/responses#cookies-and-encryption
|
||||||
|
| for details.
|
||||||
|
|
|
||||||
|
| Set it to true if you want to decrypt cookies.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'decrypt_cookies' => false,
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| 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\Lcobucci::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,
|
||||||
|
|
||||||
|
],
|
||||||
|
|
||||||
|
];
|
||||||
@@ -14,10 +14,6 @@ use Illuminate\Support\Facades\Route;
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::middleware('auth:api')->get('/user', function (Request $request) {
|
|
||||||
return $request->user();
|
|
||||||
});
|
|
||||||
|
|
||||||
Route::prefix('exercises')->group(function () {
|
Route::prefix('exercises')->group(function () {
|
||||||
Route::get('', 'ExerciseController@index');
|
Route::get('', 'ExerciseController@index');
|
||||||
});
|
});
|
||||||
@@ -26,3 +22,7 @@ Route::prefix('auth')->group(function() {
|
|||||||
Route::post('register', 'UserController@register');
|
Route::post('register', 'UserController@register');
|
||||||
Route::post('login', 'UserController@login');
|
Route::post('login', 'UserController@login');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::group(['middleware' => ['jwt.verify']], function() {
|
||||||
|
Route::get('profile', 'UserController@getAuthenticatedUser');
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user