2016-12-01 1 views
1

保護された機能を搭載したPHPファイルに到達するFirebaseを利用するJavaScript Webアプリケーションを含むプロジェクトで作業しています。JavaScript認証機関から送信されたPHPのFirebaseトークンの妥当性確認

私は呼び出すことによって(JWT)トークンを取得し、これを行うためには:

firebase.auth().currentUser.getToken(true) 

フル機能ビーイング:PHP側では

firebase.auth().currentUser.getToken(true).then(function(idToken) { 

    var uid = firebase.auth().currentUser.uid; 

    var http = new XMLHttpRequest(); 
    var url = "http://localhost/jwt.php"; 
    var params = "token=" + idToken + "&uid=" + uid; 
    http.open("POST", url, true); 

    //Send the proper header information along with the request 
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

    http.onreadystatechange = function() {//Call a function when the state changes. 
     if(http.readyState == 4 && http.status == 200) { 
      alert(http.responseText); 
     } 
    } 
    http.send(params);  

    console.log("TOKEN: " + idToken); 
}).catch(function(error) { 
    // Handle error 
}); 

を私は使用してトークンを検証していますlcobucci/jwtライブラリー。

use Lcobucci\JWT\Parser; 
use Lcobucci\JWT\ValidationData; 
use Lcobucci\JWT\Signer\Keychain; 
use Lcobucci\JWT\Signer\Rsa\Sha256; 

$data = new ValidationData(); 
$data->setIssuer('https://securetoken.google.com/<Project ID>'); 

$signer = new Sha256(); 
$keychain = new Keychain(); 

if($_POST["token"]) { 
    $token = (new Parser())->parse((string) $_POST["token"]); 
    $token->getHeaders(); // Retrieves the token header 
    $token->getClaims(); // Retrieves the token claims 

    $kid = $token->getHeader('kid'); 
    $iat = $token->getClaim('iat'); 

    //Grab Google keys 
    $json_url = file_get_contents('https://www.googleapis.com/robot/v1/metadata/x509/[email protected]'); 
    $json = json_decode($json_url, true); 

    $public_key = $json[$kid]; // Matches kid from header to private key provided by Google 


    try { 
     $isTokenValid = $token->verify($signer, $public_key); // Verify token 
    } catch (Exception $e) { 
     $isTokenValid = false; 
    } 

    if($isTokenValid) { 

     echo "Valid"; // Add protected functionality here 

    } else { 
     echo "Invalid"; 
    } 
} 

私の質問は:これは安全ですか?

答えて

0

はい、このようなトークンシグネチャは安全です。 これは、トークンの内容が変更されておらず、Googleからの鍵で署名されていることを証明します。

あなたはここにJWTについて詳しく知ることができます。

https://jwt.io/introduction/はまた、あなたがトークン

$token->validate($data); 

を検証することができます。これは、発行者(ISS請求)とトークンの有効期限(EXPを検証します請求項) https://github.com/lcobucci/jwt/blob/3.2/README.md

関連する問題