Commit 56e5681a authored by Dzulfqar Ridha's avatar Dzulfqar Ridha
parents fa9f8b14 2fa46d73
...@@ -3,7 +3,8 @@ APP_ENV=local ...@@ -3,7 +3,8 @@ APP_ENV=local
APP_KEY= APP_KEY=
APP_DEBUG=true APP_DEBUG=true
APP_URL=http://localhost APP_URL=http://localhost
APP_TIMEZONE=UTC APP_TIMEZONE=Asia/Jakarta
DB_TIMEZONE=+07:00
LOG_CHANNEL=stack LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL= LOG_SLACK_WEBHOOK_URL=
...@@ -17,3 +18,6 @@ DB_PASSWORD=secret ...@@ -17,3 +18,6 @@ DB_PASSWORD=secret
CACHE_DRIVER=file CACHE_DRIVER=file
QUEUE_CONNECTION=sync QUEUE_CONNECTION=sync
JWT_SECRET=JhbGciOiJIUzI1N0eXAiOiJKV1QiLC
JWT_EXP_HOUR_TIME=3600
...@@ -27,7 +27,7 @@ class BaseController extends Controller ...@@ -27,7 +27,7 @@ class BaseController extends Controller
$client = new Client([ $client = new Client([
'base_uri' => 'https://api.mashery.com/', 'base_uri' => 'https://api.mashery.com/',
'headers' => [ 'headers' => [
'Authorization' => 'Bearer ' . $this->request->auth['token'], 'Authorization' => $this->request->auth->masheryToken->token_type . ' ' . $this->request->auth->masheryToken->access_token,
'Content-Type' => 'application/json' 'Content-Type' => 'application/json'
], ],
'http_errors' => false 'http_errors' => false
......
...@@ -10,7 +10,9 @@ namespace App\Http\Controllers; ...@@ -10,7 +10,9 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Classes\MResponse; use App\Classes\MResponse;
use Firebase\JWT\JWT;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Carbon\Carbon;
class MasheryController extends Controller class MasheryController extends Controller
{ {
...@@ -47,7 +49,18 @@ class MasheryController extends Controller ...@@ -47,7 +49,18 @@ class MasheryController extends Controller
} else { } else {
$mResponse->success = true; $mResponse->success = true;
$mResponse->message = 'success'; $mResponse->message = 'success';
$mResponse->data = $resBody;
$payload = [
'iss' => 'jwt', // Issuer of the token
'sub' => (string)$resBody['access_token'], // Subject of the token
'iat' => time(), // Time when JWT was issued.
'exp' => time() + env('JWT_EXP_HOUR_TIME'), // Expiration time
'masheryToken' => $resBody
];
$mResponse->data = [
'token' => JWT::encode($payload, env('JWT_SECRET'))
];
} }
} catch (\Exception $e) { } catch (\Exception $e) {
......
...@@ -10,6 +10,8 @@ namespace App\Http\Middleware; ...@@ -10,6 +10,8 @@ namespace App\Http\Middleware;
use App\Classes\MResponse; use App\Classes\MResponse;
use Closure; use Closure;
use Firebase\JWT\JWT;
use Firebase\JWT\ExpiredException;
class ClientMiddleware class ClientMiddleware
{ {
...@@ -23,12 +25,17 @@ class ClientMiddleware ...@@ -23,12 +25,17 @@ class ClientMiddleware
$response->message = 'Token not provided'; $response->message = 'Token not provided';
return response()->json($response, 401); return response()->json($response, 401);
} }
try {
$credentials = JWT::decode($token, env('JWT_SECRET'), ['HS256']);
} catch (ExpiredException $e) {
$response->message = 'Provided token is expired';
return response()->json($response, 400);
} catch (Exception $e) {
$response->message = 'An error while decoding token';
return response()->json($response, 400);
}
$authUser = [ $request->auth = $credentials;
'token' => $token
];
$request->auth = $authUser;
return $next($request); return $next($request);
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
"type": "project", "type": "project",
"require": { "require": {
"php": ">=7.1.3", "php": ">=7.1.3",
"firebase/php-jwt": "^5.0",
"guzzlehttp/guzzle": "~6.0", "guzzlehttp/guzzle": "~6.0",
"laravel/lumen-framework": "5.8.*", "laravel/lumen-framework": "5.8.*",
"pearl/lumen-request-validate": "^1.2", "pearl/lumen-request-validate": "^1.2",
......
...@@ -4,7 +4,7 @@ ...@@ -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": "21afb945c55a0717258763da87c1b757", "content-hash": "61e11dfb76ed859fac129c5d5f227abc",
"packages": [ "packages": [
{ {
"name": "doctrine/inflector", "name": "doctrine/inflector",
...@@ -238,6 +238,52 @@ ...@@ -238,6 +238,52 @@
], ],
"time": "2018-12-04T22:38:24+00:00" "time": "2018-12-04T22:38:24+00:00"
}, },
{
"name": "firebase/php-jwt",
"version": "v5.0.0",
"source": {
"type": "git",
"url": "https://github.com/firebase/php-jwt.git",
"reference": "9984a4d3a32ae7673d6971ea00bae9d0a1abba0e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/firebase/php-jwt/zipball/9984a4d3a32ae7673d6971ea00bae9d0a1abba0e",
"reference": "9984a4d3a32ae7673d6971ea00bae9d0a1abba0e",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": " 4.8.35"
},
"type": "library",
"autoload": {
"psr-4": {
"Firebase\\JWT\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Neuman Vong",
"email": "neuman+pear@twilio.com",
"role": "Developer"
},
{
"name": "Anant Narayanan",
"email": "anant@php.net",
"role": "Developer"
}
],
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.",
"homepage": "https://github.com/firebase/php-jwt",
"time": "2017-06-27T22:17:23+00:00"
},
{ {
"name": "guzzlehttp/guzzle", "name": "guzzlehttp/guzzle",
"version": "6.3.3", "version": "6.3.3",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment