ValidateTokenController.class.php 1.22 KB
Newer Older
Zohten's avatar
Zohten committed
1 2 3 4 5 6 7 8
<?php

include_once __ROOT_DIR . '/libs/php-jwt/src/BeforeValidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/ExpiredException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/SignatureInvalidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;

Zohten's avatar
Zohten committed
9 10 11 12 13 14
class ValidateTokenController extends Controller
{
    public function __construct($name, $request)
    {
        parent::__construct($name, $request);
    }
Zohten's avatar
Zohten committed
15

Zohten's avatar
Zohten committed
16 17 18 19 20 21 22
    public function processRequest()
    {
        try {
            $jwt_token = $this->request->getJwtToken();
            // echo "jwt = $jwt_token";
            $decodedJWT = JWT::decode($jwt_token, JWT_BACKEND_KEY, array('HS256'));
            $jsonResult = json_encode(array(
Zohten's avatar
Zohten committed
23 24 25
             "message" => "Access granted.",
             "data" => $decodedJWT
         ));
Zohten's avatar
Zohten committed
26 27
        } catch (Exception $e) {
            header('WWW-Authenticate: Bearer realm="'.JWT_ISSUER.'"');
Zohten's avatar
Zohten committed
28

Zohten's avatar
Zohten committed
29
            $jsonResult =  json_encode(array(
Zohten's avatar
Zohten committed
30 31 32
             "message" => "Access denied.",
             "error" => $e->getMessage()
         ));
Zohten's avatar
Zohten committed
33 34 35 36
            return Response::unauthorizedResponse($jsonResult);
        }
        $response = Response::okResponse($jsonResult);
        return $response;
Zohten's avatar
Zohten committed
37
    }
Zohten's avatar
Zohten committed
38
}