2017-06-06 8 views
0

Ich habe Probleme beim Erstellen einer JWT-Berechtigung für Google Drive. Die Anweisung wird in this link gegeben.Google Drive Auth: Erstellen der JWT-Signatur aus der JSON-Version des privaten Schlüssels

<?php 
//helper function 
function base64url_encode($data) { 
return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); 
} 

//Google's Documentation of Creating a JWT https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests 

$raw_key_data="-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkWhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCLJN7B2USp0bTH\n7aVt1K/pAJa9rgUEkebbRQUPEopManZHgJK8SMzq5nUM3OOVlTdyDyuzxUZW65Y+\nR1xajNHSidE+rJJ3MWwuevXBuUws5oiA+DXJBB6FsTtHDvUPT7sePbJx8Y80S1Jr\nB3apJNdohjSef4UihgjJ93A0PgeSVz7CeUKxrBHdGiK2Ikf7wTCzcRzz/15I6QLu\ncsNbWxo0+koXo9D0+wT3RN+riSyTa2WwYn//B8v6VmDPVrcjxETEXraAIMpqURtK\n4BuK4Qp2pSd1u2zrGc58BVGuT6+nPVpmX6wiZ21sqOulTzgIOXKIuc4zdKvZ+jz/\n1jst3ertAgMBAAECggEADRGn+IYbLGYdeD/Kb2/wG87p2aP8Jas8hzDK4lkH81h2\nho29eoDN+mwt50jh+V08CXMCVE69phFXmb7jHkAmvwMhy6Sy1w4lzpHO/mSUko0O\nmip2BszjvwPgAPMXMlp3RUZfOdOJ80v10Eaxrv5eWxtr2s04aH81WR7sA4Ql+uki\nbTiLbo4odpMVPkoJZATZKQd+L/bBM4a+b3IM87/TE7skMrSgQE6cnjjTI5Uk+WSl\nxjiZvj5XmticW3vPavL/ZXPEZqy5IxvcxdF5rGHCHVu4ah0CmDdc9A3jF0flhewK\nV6mOViqbKGInnMn4kt3l4C3+wF2+dge6t8BQ/TMtwwKBgQDBnpjiC+wRVQy36KCs\ntYdZ60ykuQeKqMyACt6FvC74xy+PjLVntxDaYArva2PVaZSm9B2FgGHPz9AfW80/\n1ZeNHcuwymni8n+4MNpQbJuhuHK8UjSo0rFq6Nrddi9RCjzASSX8s4Dlm01VM65b\nKBnM5p50I9QEY4F4gSZfLLMNowKBgQC3+Tpz23qhkxJPu9cVuvPRFfg1y0NNsp7k\n0qHYMegz1GHoMVs1kg36OtbROqpG2+rIrj0JWTGwLO14fX2TY0jWfao21CGrpkXM\nlY1KSDIMuQv6pd5yh74oqvDDpZwKxxu/nmzcQbd1lN/nFkEW5g0b7e27UoCoovwS\n7qSENbqOLwKBgHYWp8H+aX1stPQZ8p1DngiupTE2FK5yIz/Y4T0JuFBNE+nmdOGL\n2sCFoUXC5sHLwjlNXBAHbCCV66akk/th5yvPR2NNIOWk51bMnOo+Q3GQEJJhRPLO\nhhzhZlN5+IPhzYmtU3jbdjsTzEex3J6GR64b3fqRu4bttZJsmp2jopUnAoGAWW+7\nzt7/+tR4rnJu2Y2NQjQf+mbaTUddb2kWbPe2Hpw9DJgR8zURvngkPor6hIAc33p1\nCbpmwXLV7yFyjthRbJSizwzJYZzvicmaamY2jqDXBf7k6WC8PSD88t/rwAGTp8/o\ntBruiSbawoi7E9q6vL0qOUqeaVzylnGVYQCNtNkCgYBwqL1MNTR8IrXDfZyYdDRP\nWNCRqm7ymuQi7IUKVa+yaBM1ubvEe7EPrlZWlFPDdPmaScx02Qwf++xcGHWzzDX0\nQPmd95OTGafvECXuKVy2NAf2AdCYVruL+17wfPhuz7ANIpgEqsiNAZNe0GtGBjyZ\nVuiSVVML3jW4XUtf63V0/A==\n-----END PRIVATE KEY-----\n"; 

$private_key = openssl_get_privatekey($raw_key_data); 
if ($private_key === FALSE) 
{ 
    error_log ("Unable to extract private key from raw key data: " . openssl_error_string()); 
}; 

print("$private_key\n"); 

//{Base64url encoded JSON header} 

$jwtHeader = base64url_encode(json_encode(array(
"alg" => "RS256", 
"typ" => "JWT" 
))); 

//{Base64url encoded JSON claim set} 
$now = time(); 
$jwtClaim = base64url_encode(json_encode(array(
"iss" => "[email protected]", 
"scope" => "https://www.googleapis.com/auth/drive.file", 
"aud" => "https://www.googleapis.com/oauth2/v4/token", 
"exp" => $now + 3600, 
"iat" => $now 
))); 

//The base string for the signature: {Base64url encoded JSON header}. {Base64url encoded JSON claim set} 

openssl_sign(
$jwtHeader.".".$jwtClaim, 
$jwtSig, 
$private_key, 
"SHA256" 
); 

$jwtSign = base64url_encode($jwtSig); 

//{Base64url encoded JSON header}.{Base64url encoded JSON claim set}. {Base64url encoded signature} 
$jwtAssertion = $jwtHeader.".".$jwtClaim.".".$jwtSig; 
print("$jwtAssertion\n"); 

?> 

Wenn ich diesen Code ausführen, bekomme ich durch eine Nonsense-Signatur gefolgt. Ich habe sogar versucht, den privaten Schlüssel in eine Datei zu setzen und aus der Datei zu lesen, in der es mehrere Zeilen gibt (privater Schlüssel getrennt bei \n), aber keinen Erfolg. Ich kann keine aussagekräftige JWT-Signatur aus dem privaten Schlüssel generieren. Kann mir jemand bei diesem Problem helfen?

Danke,

Antwort

1

ich es geschafft haben, dieses Problem zu lösen. Unten ist der aktualisierte Code, der die JSON-Datei von Google heruntergeladen liest und erstellt eine assertion, mit dem man dann Zugriffstoken von Google bekommen kann:

<?php 
//helper function 
function base64url_encode($data) { 
return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); 
} 

// Read the JSON credential file my-private-key.json download from Google 
$private_key_file="my-private-key.json"; 
$json_file = file_get_contents($private_key_file); 

$info = json_decode($json_file); 
$private_key = $info->{'private_key'}; 

//{Base64url encoded JSON header} 
$jwtHeader = base64url_encode(json_encode(array(
"alg" => "RS256", 
"typ" => "JWT" 
))); 

//{Base64url encoded JSON claim set} 
$now = time(); 
$jwtClaim = base64url_encode(json_encode(array(
"iss" => $info->{'client_email'}, 
"scope" => "https://www.googleapis.com/auth/drive.file", 
"aud" => "https://www.googleapis.com/oauth2/v4/token", 
"exp" => $now + 3600, 
"iat" => $now 
))); 

$data = $jwtHeader.".".$jwtClaim; 

// Signature 
$Sig = ''; 
openssl_sign($data,$Sig,$private_key,'SHA256'); 
$jwtSign = base64url_encode($Sig ); 


//{Base64url encoded JSON header}.{Base64url encoded JSON claim set}.{Base64url encoded signature} 

$jwtAssertion = $data.".".$jwtSign; 
echo "$jwtAssertion\n"; 

Sie dann Ihren Code zum Beispiel durch die Kommunikation mit Google Drive testen können:

curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=YOUR-JWT-ASSERTION-BUILD-ABOVE' https://www.googleapis.com/oauth2/v4/token 
+0

Es funktionierte für mich. Vielen Dank. –

Verwandte Themen