phplaravel-5google-oauth

Laravel google/apiclient Cannot handle token prior to [timestamp]


I'm trying to verify an idToken from google and I keep getting this error. I'm using angular as a font end and laravel as a backend and I just want to verify the idToken before I sign them in.

{message: "Cannot handle token prior to 2018-03-19T22:12:10+0000",…}
exception : "Firebase\JWT\BeforeValidException"
file : "/home/vagrant/web/scratk/office-api/vendor/firebase/php-jwt/src/JWT.php"
line : 124
message : "Cannot handle token prior to 2018-03-19T22:12:10+0000"
trace : [{,…},…]

This is my function within Laravel:

public function socialSignIn(Request $request, Response $response) {
    JWT::$leeway = 5;

    $date = date('Y-m-d h:i:s');

    $provider = $request->input('provider');

    if ($provider == 'google') {

        $id_token = $request->header('Authorization');

        $id_token = str_replace("Bearer ","",$id_token);

        $CLIENT_ID = Config::get('google.client_id');

        $client = new \Google_Client();
        $client->setDeveloperKey($CLIENT_ID);
        $payload = $client->verifyIdToken($id_token);
        if ($payload) {
            // $userid = $payload['sub'];
            return true;
        } else {
            return false;
            // return response()->json([
            //     'failed'
            // ], 300);
        }
    }
}

Solution

  • So after a lot of googling, I found out why I was getting this error and its because the system clock on my VM was behind. Because it was the 17th and the date on the VM was sat 3 march.

    To double check, you can check the date with date

    So the way of getting around this was opening the VM and the ssh and using these commands sudo service ntp stop then sudo ntpd -gq then after this has run through run sudo service ntp start.

    After this, you can run date and it should pass out the actual date.