I am using Firebase Token Generator to generate secure tokens in PHP:
const DEFAULT_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
date_default_timezone_set('Europe/London');
try {
$generator = new TokenGenerator(DEFAULT_SECRET);
$token = $generator
->setOptions(array(
'expires' => strtotime('now + 1 minute'),
'debug' => true
))
->setData(array('uid' => 'exampleID'))
->create();
} catch (TokenException $e) {
echo "Error: ".$e->getMessage();
}
$response = array(
'token' => $token
);
echo json_encode($response);
On the client side I am using a JSON request to retrieve the token as a JSON object:
$.getJSON('http://localhost/firebase/index.php', function(json) {
var jwtToken = json.token;
launchFirebase(jwtToken);
});
function launchFirebase(token) {
var fb = new Firebase(FirebaseURL);
fb.authWithCustomToken(token, function(error, authData) {
if (error) {
alert("There was an error posting your vote. Please try again later.");
} else {
checkEmail();
}
});
function checkEmail(){
new Firebase(FirebaseURL)
.orderByChild('email')
.startAt(vote.email)
.endAt(vote.email)
.once('value', function(snap) {
var result = snap.val();
callback(result);
});
}
}
I can now read data from Firebase so this works fine. My security concern is that anyone can view the source code and go directly to the PHP script (http://localhost/firebase/index.php) to retrieve the token.
They could then view all the data via an API call like this: https://examplesite.firebaseio.com/.json?print=pretty&auth=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
What's the best practice to keep the PHP script secure?