diff --git a/app/Http/Middleware/JsonWebToken.php b/app/Http/Middleware/JsonWebToken.php index a152553..ef5f498 100644 --- a/app/Http/Middleware/JsonWebToken.php +++ b/app/Http/Middleware/JsonWebToken.php @@ -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 { diff --git a/app/Http/Middleware/WebToken.php b/app/Http/Middleware/WebToken.php index 7744ec2..09585d9 100644 --- a/app/Http/Middleware/WebToken.php +++ b/app/Http/Middleware/WebToken.php @@ -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') ]);