Skip to content

Commit

Permalink
Adding notes
Browse files Browse the repository at this point in the history
  • Loading branch information
steevenz committed Jan 11, 2019
1 parent 1965af4 commit 02a6abc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
7 changes: 6 additions & 1 deletion app/Http/Middleware/JsonWebToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ class JsonWebToken implements RequestHandlerInterface
public function handle(ServerRequestInterface $request)
{
if (services()->has('jsonWebTokenAuthentication')) {
/**
* This is an example to implement HTTP Authentication with Json Web Token (JWT).
* Try put this code into your request and see the result.
*
* eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjcsInVzZXJuYW1lIjoic3RlZXZlbnoifQ.D29MZcJa2svH5kNt4bFcUtIXvJ4ohYJ-0vNxsgMWAvc
*/
if(false !== ($token = input()->bearerToken())) {
print_out($token);
$payload = services('jsonWebTokenAuthentication')->decode($token);
globals()->store('payload', $payload);
} else {
Expand Down
45 changes: 43 additions & 2 deletions app/Http/Middleware/WebToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,50 @@ public function handle(ServerRequestInterface $request)
{
if (services()->has('webTokenAuthentication')) {

services('webTokenAuthentication')->setToken('WEBTOKEN-TESTING');
/**
* $token
*
* This is an example to implement HTTP X-WEB-TOKEN authentication.
* The web token can be generated freely according to your own token generator concept.
*
* @example
* This token is generated from simple generator concept.
* $token = md5(json_encode(['uid' => 7, 'username' => 'steevenz'] ));
*
* // result: ed3d68c4d51f52734e5bb6add37147d2
*
* @var string
*/

if ( ! services('webTokenAuthentication')->verify()) {
/**
* $users
*
* This is a users database thats hold users accounts.
*
* @var array
*/
$users = [
'ed3d68c4d51f52734e5bb6add37147d2' => [
'uid' => 7,
'username' => 'steevenz',
]
];

if($token = input()->webToken()) {
/**
* Let's verify it with Web Token Authentication service callback.
*/
$validate = services('webTokenAuthentication')->verify($token, function($token) use ($users) {
return array_key_exists($token, $users);
});

if($validate) {
$payload = $users[ $token ]; // this is an example payload
globals()->store('payload', $payload);
}
}

if(empty($payload)) {
output()->sendError(403, [
'message' => language()->getLine('403_INVALID_WEBTOKEN')
]);
Expand Down

0 comments on commit 02a6abc

Please sign in to comment.