2016-11-03 1 views
1

안녕하세요 문제가 구글/apiclient에게catch되지 않은 예외 메시지 '중포 기지 JWT BeforeValidException은'토큰 이전 2016-11-03T21을 처리 할 수 ​​없습니다 : 37 : + 0100 13 '

Fatal error: Uncaught exception 'Firebase\JWT\BeforeValidException' with message 'Cannot handle token prior to 2016-11-03T21:37:13+0100' in C:\xampp\htdocs\Google\vendor\firebase\php-jwt\src\JWT.php:124 Stack trace: #0 C:\xampp\htdocs\Google\vendor\google\apiclient\src\Google\AccessToken\Verify.php(100): Firebase\JWT\JWT::decode('eyJhbGciOiJSUzI...', '-----BEGIN PUBL...', Array) #1 C:\xampp\htdocs\Google\vendor\google\apiclient\src\Google\Client.php(705): Google_AccessToken_Verify->verifyIdToken('eyJhbGciOiJSUzI...', '474251646530-0t...') #2 C:\xampp\htdocs\Google\app\class\google_auth.php(51): Google_Client->verifyIdToken() #3 C:\xampp\htdocs\Google\app\class\google_auth.php(35): GoogleAuth->getPayLoad() #4 C:\xampp\htdocs\Google\index.php(10): GoogleAuth->checkRedirectCode() #5 {main} thrown in C:\xampp\htdocs\Google\vendor\firebase\php-jwt\src\JWT.php on line 124

를 사용하려고해야 내 인덱스 :

<?php 
    require_once('app/ini.php'); 
    require_once('vendor/autoload.php'); 
    require_once('app/class/google_auth.php'); 


    $googleClient = new Google_Client(); 
    $auth = new GoogleAuth($googleClient); 

    if ($auth->checkRedirectCode()) { 
     header("Location: index.php"); 
    } 

?> 

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 

    <?php if (!$auth->isLoggedIn()): //Verificar Inicio de Sesion ?> 
     <a href="<?php echo $auth->getAuthUrl(); ?>">Inicie Sesion con Google</a> 
    <?php else: //Si no ha iniciado Sesion ?> 
     Bienvenido.. <a href="logout.php">Cerrar Sesion</a> 
    <?php endif; ?> 

</body> 
</html> 

클래스 GoogleAuth :

<?php 

    class GoogleAuth{//Clase para la autenticacion del usuario google 

     protected $client;//Variable de cliente 

     public function __construct(Google_Client $googleClient = null){ 
      $this->client = $googleClient; 

      if ($this->client) { 

       $this->client->setClientId('474251646530-0tiho0cbf4dusercontent.com');//Usuario Auth Google 
       $this->client->setClientSecret('bMuLusxvnvPg2zRz');//Clave Auth Google 
       $this->client->setRedirectUri('http://localhost/Google/index.php'); 
       $this->client->setScopes('email'); 

      } 
     } 

     public function isLoggedIn(){//Metodo que devuelve el estatus de la Sesion con Google (true o false) 
      return isset($_SESSION['access_token']); 

     } 

     public function getAuthUrl(){//Funcion que devuelve el enlace requerido para iniciar sesion 
      return $this->client->createAuthUrl(); 

     } 

     public function checkRedirectCode(){ 
      if (isset($_GET['code'])) { 
       $this->client->authenticate($_GET['code']); 
       $this->setToken($this->client->getAccessToken()); 

       $payload=$this->getPayLoad(); 
       echo "<pre>", print_r($payload) ,"<pre>"; 
       return true; 
      } 
      return false; 
     } 

     public function setToken($token){ 
      $_SESSION['access_token']=$token; 
      $this->client->setAccessToken($token); 
     } 

     public function logout(){ 
      unset($_SESSION['access_token']); 
     } 

     public function getPayLoad(){ 
      $payload=$this->client->verifyIdToken()->getAttributes(); 
      return $payload; 
     } 

    } 

?> 

은 저에게

를 도와주세요 이 함수 내부 691,363,210
+0

지금은이 오류를 치명적인 오류가 : C에서 부울에 멤버 함수 getAttributes()로 전화 : \ XAMPP \ htdocs를 \ 구글 \ 동일한 코드 –

답변

0

:

public function getPayLoad(){ 
    $payload=$this->client->verifyIdToken()->getAttributes(); 
    return $payload; 
} 

getAttributes()를 제거하고 다음과 같이두고 :

public function getPayLoad(){ 
    $payload=$this->client->verifyIdToken(); 
    //print_r($payload); //This gives you the information you need 
    return $payload; 
} 
1

당신은 서버 시간 문제가 있습니다. JWT 라이브러리와 서버 사이의 시간은 다릅니다. 해당 라이브러리는 'UTC'시간대를 사용합니다. 귀하의 유스 케이스에 따라, 같은 존에 있도록 시간대를 맞추거나, 필요하지 않으면이 라인을 주석 처리 할 수 ​​있습니다.

// Configures the time that the token can be used (nbf claim) 
// ->setNotBefore(time() + 60) 
+0

라인 (52)에 대한 응용 프로그램 \ 클래스 \의 google_auth.php OP 코드에는 그 라인이 존재하지 않습니다. 나는 그 질문이 편집되었다고 생각하니? 그렇지 않다면, 그 라인은 어디에있을 예정입니까? – Taurus

+0

@ 황소 자리 그 줄은 당신이 발행자, setAudience 등을 설정하는 설정 객체에있다. – phil

+0

나는 토큰과 구글과 관련된 3 줄의 코드 만 사용한다 :'$ CLIENT_ID = "xxxxxxx-xxxxxxxxxx.apps.googleusercontent.com"; $ id_token = $ _POST [ "id"]; $ client = new Google_Client ([ 'client_id'=> $ CLIENT_ID]); $ payload = $ client-> verifyIdToken ($ id_token);'. 나는 서버 측에 정말 좋지 않다. 그러나 나는이 것들을 (issuer, setAudience, etc ...) 설정해야 할까? – Taurus

1

JWT 라이브러리는 서명 및 검증 서버간에 클럭 스큐 시간이있는 경우를 고려하여 (초)를 여유를 사용한다. 라이브러리와 서버 사이의 시간 차이를 해결하기 위해 여유

보다 더 중요한 경우

이 오류가 발생,

\vendor\google\apiclient\src\Google\AccessToken\Verify.php

로 이동하여 getJwtService 기능에 여유를 향상시킬 수 있습니다.

private function getJwtService() 
    { 
    $jwtClass = 'JWT'; 
    if (class_exists('\Firebase\JWT\JWT')) { 
     $jwtClass = 'Firebase\JWT\JWT'; 
    } 

    if (property_exists($jwtClass, 'leeway')) { 
     // adds 1 second to JWT leeway 
     // @see https://github.com/google/google-api-php-client/issues/827 
     $jwtClass::$leeway += 10; 
    } 

    return new $jwtClass; 
    }