Commit 00d734de authored by Sjoerd Langkemper's avatar Sjoerd Langkemper
Browse files

Simplified Firebase example

parent 2eac094f
No related merge requests found
Showing with 17 additions and 47 deletions
+17 -47
<?php
require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/common.php';
use \Firebase\JWT\JWT;
JWT::$leeway = 10; // $leeway in seconds
$shared_key = "secret";
$private_key = file_get_contents('private.pem');
$public_key = file_get_contents('public.pem');
$auth_header = $_SERVER['HTTP_AUTHORIZATION'];
if (stripos($auth_header, 'bearer ') == 0) {
$auth_header = substr($auth_header, 7);
}
JWT::$supported_algs['none'] = ['hash_hmac', 'none'];
$algorithms = [
'none' => 'none',
'HS256' => $shared_key,
'RS256' => $public_key,
];
if ($auth_header) {
$header = json_decode(base64_decode(substr($auth_header, 0, strpos($auth_header, '.'))));
$key = $algorithms[$header->alg];
try {
$decoded = JWT::decode($auth_header, $key, array_keys($algorithms));
echo "Valid $algorithm JWT: ";
print_r($decoded);
} catch (Exception $e) {
echo "Invalid JWT: $e\n";
function encodeJwt($tokenObj, $algorithmName, $keys) {
if ($algorithmName == 'none') {
return "not supported";
}
} else {
$token = array(
# Issuer
"iss" => "http://demo.sjoerdlangkemper.nl/",
# Issued at
"iat" => time(),
# Expire
"exp" => time() + 120,
"data" => [
"hello" => "world"
]
);
echo '<xmp>';
$jwt = JWT::encode($token, $shared_key, 'HS256');
echo "HS256: $jwt\n";
$key = $keys;
if (is_array($keys)) {
$key = $keys[0];
}
return JWT::encode($tokenObj, $key, $algorithmName);
}
$jwt = JWT::encode($token, $private_key, 'RS256');
echo "RS256: $jwt\n";
function decodeJwt($token) {
$keys = getAlgorithmKeys();
$key = $keys['RS256'][1];
echo "Public key: \n$public_key\n";
return JWT::decode($token, $key, array_keys($keys));
}
include('base.php');
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment