Skip to content

Commit

Permalink
Implementing JWT example
Browse files Browse the repository at this point in the history
  • Loading branch information
steevenz committed Jan 11, 2019
1 parent 2a6a7e4 commit 1965af4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
*/.DS_Store
cache
cache
vendor
3 changes: 2 additions & 1 deletion app/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
*/

$services = [
'webTokenAuthentication' => '\O2System\Security\Authentication\WebToken'
'webTokenAuthentication' => '\O2System\Security\Authentication\WebToken',
//'jsonWebTokenAuthentication' => '\O2System\Security\Authentication\Jwt\Token'
];
10 changes: 6 additions & 4 deletions app/Controllers/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ public function index()
$this->sendPayload(
[
'request' => [
'method' => $_SERVER[ 'REQUEST_METHOD' ],
'time' => $_SERVER[ 'REQUEST_TIME' ],
'uri' => $_SERVER[ 'REQUEST_URI' ],
'agent' => $_SERVER[ 'HTTP_USER_AGENT' ],
'method' => $this->input->server('REQUEST_METHOD'),
'time' => $this->input->server('REQUEST_TIME'),
'uri' => $this->input->server('REQUEST_URI'),
'agent' => $this->input->server('HTTP_USER_AGENT'),
'authorization' => $this->input->server('HTTP_AUTHORIZATION')
],
'headers' => $headers,
'get' => $_GET,
'post' => $_POST,
'payload' => $this->globals->payload
]
);
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public function __construct()
parent::__construct();

$this->register( new Middleware\WebToken(), 'webToken' );
$this->register( new Middleware\JsonWebToken(), 'jsonWebToken' );
}
}
49 changes: 49 additions & 0 deletions app/Http/Middleware/JsonWebToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* This file is part of the O2System PHP Framework package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Steeve Andrian Salim
* @copyright Copyright (c) Steeve Andrian Salim
*/

// ------------------------------------------------------------------------

namespace App\Http\Middleware;

// ------------------------------------------------------------------------

use O2System\Psr\Http\Message\ServerRequestInterface;
use O2System\Psr\Http\Server\RequestHandlerInterface;

/**
* Class JsonWebToken
*
* @package O2System\Framework\Http\Middleware
*/
class JsonWebToken implements RequestHandlerInterface
{
/**
* JsonWebToken::handle
*
* Handles a request and produces a response
*
* May call other collaborating code to generate the response.
*/
public function handle(ServerRequestInterface $request)
{
if (services()->has('jsonWebTokenAuthentication')) {
if(false !== ($token = input()->bearerToken())) {
print_out($token);
$payload = services('jsonWebTokenAuthentication')->decode($token);
globals()->store('payload', $payload);
} else {
output()->sendError(403, [
'message' => language()->getLine('403_INVALID_JSONWEBTOKEN')
]);
}
}
}
}

0 comments on commit 1965af4

Please sign in to comment.