Skip to content

Instantly share code, notes, and snippets.

@NirmalAriyathilake
Created October 26, 2018 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NirmalAriyathilake/41bb47682d37cd911389c7ac202a19fe to your computer and use it in GitHub Desktop.
Save NirmalAriyathilake/41bb47682d37cd911389c7ac202a19fe to your computer and use it in GitHub Desktop.
Double Submit Cookies Pattern - server.php
<?php
session_start();
// validate login
if(isset($_POST['submit'])){
ob_end_clean(); // buffer clean
validate($_POST['username'],$_POST['password']);
}
//validate cookies and update status
if(isset($_POST['addstatussubmit'])){
ob_end_clean(); // buffer clean
$cookieNameCsrf = "csrfTokenCookie";
validateStatus($_POST['token_csrf'],$_COOKIE[$cookieNameCsrf]);
}
//generate csrf token
function generateToken($sessionCookie){
if(empty($_SESSION['random_key'])){
$_SESSION['random_key'] = bin2hex(random_bytes(32));
}
$token = hash_hmac('sha256',$sessionCookie,$_SESSION['random_key']);
$sessionID = session_id();
$expireTime = time() + 60*60; // expire time 1 hour
$cookieNameCsrf = "csrfTokenCookie";
setcookie ($cookieNameCsrf, $token, $expireTime, "/","localhost", FALSE, TRUE);
ob_start(); // store in buffer
echo $token;
}
//validate cookie
function validate($username,$password){
/**
* For demo ,
* Username : user
* Password : user
*/
if($username == "user" && $password == "user"){
$cookieName = "sessionCookie";
generateToken($_COOKIE[$cookieName]);
echo "<script> alert('Successfully Logged In') </script>";
echo "<script type=\"text/javascript\"> window.location.href = 'client.php';</script>";
}else{
echo "<script> alert('Login failed! Check username and password again !!!') </script>";
echo "<script type=\"text/javascript\"> window.location.href = 'index.php';</script>";
}
}
//validate status
function validateStatus($token,$csrfCookie){
if($token == $csrfCookie){
echo "<script> alert('Status successfully added') </script>";
echo "<script type=\"text/javascript\"> window.location.href = 'client.php';</script>";
}else{
echo "<script> alert('Status posting failed! CSRF token not matched !!!') </script>";
echo "<script type=\"text/javascript\"> window.location.href = 'client.php';</script>";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment