token-functions.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. function jwtParse($token){
  3. try {
  4. $result = array();
  5. $result['valid'] = false;
  6. // Check Token with JWT
  7. // Set key
  8. if(!isset($GLOBALS['organizrHash'])){
  9. return null;
  10. }
  11. $key = $GLOBALS['organizrHash'];
  12. // SHA256 Encryption
  13. $signer = new Lcobucci\JWT\Signer\Hmac\Sha256();
  14. $jwttoken = (new Lcobucci\JWT\Parser())->parse((string) $token); // Parses from a string
  15. $jwttoken->getHeaders(); // Retrieves the token header
  16. $jwttoken->getClaims(); // Retrieves the token claims
  17. // Start Validation
  18. if($jwttoken->verify($signer, $key)){
  19. $data = new Lcobucci\JWT\ValidationData(); // It will use the current time to validate (iat, nbf and exp)
  20. $data->setIssuer('Organizr');
  21. $data->setAudience('Organizr');
  22. if($jwttoken->validate($data)){
  23. $result['valid'] = true;
  24. $result['username'] = $jwttoken->getClaim('username');
  25. $result['group'] = $jwttoken->getClaim('group');
  26. $result['groupID'] = $jwttoken->getClaim('groupID');
  27. $result['userID'] = $jwttoken->getClaim('userID');
  28. $result['email'] = $jwttoken->getClaim('email');
  29. $result['image'] = $jwttoken->getClaim('image');
  30. $result['tokenExpire'] = $jwttoken->getClaim('exp');
  31. $result['tokenDate'] = $jwttoken->getClaim('iat');
  32. $result['token'] = $jwttoken->getClaim('exp');
  33. }
  34. }
  35. if($result['valid'] == true){ return $result; }else{ return false; }
  36. } catch(\RunException $e) {
  37. return false;
  38. } catch(\OutOfBoundsException $e) {
  39. return false;
  40. } catch(\RunTimeException $e) {
  41. return false;
  42. } catch(\InvalidArgumentException $e) {
  43. return false;
  44. }
  45. }
  46. function createToken($username,$email,$image,$group,$groupID,$key,$days = 1){
  47. //Quick get user ID
  48. try {
  49. $database = new Dibi\Connection([
  50. 'driver' => 'sqlite3',
  51. 'database' => $GLOBALS['dbLocation'].$GLOBALS['dbName'],
  52. ]);
  53. $result = $database->fetch('SELECT * FROM users WHERE username = ? COLLATE NOCASE OR email = ? COLLATE NOCASE',$username,$email);
  54. // Create JWT
  55. // Set key
  56. // SHA256 Encryption
  57. $signer = new Lcobucci\JWT\Signer\Hmac\Sha256();
  58. // Start Builder
  59. $jwttoken = (new Lcobucci\JWT\Builder())->setIssuer('Organizr') // Configures the issuer (iss claim)
  60. ->setAudience('Organizr') // Configures the audience (aud claim)
  61. ->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
  62. ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
  63. ->setExpiration(time() + (86400 * $days)) // Configures the expiration time of the token (exp claim)
  64. ->set('username', $result['username']) // Configures a new claim, called "username"
  65. ->set('group', $result['group']) // Configures a new claim, called "group"
  66. ->set('groupID', $result['group_id']) // Configures a new claim, called "groupID"
  67. ->set('email', $result['email']) // Configures a new claim, called "email"
  68. ->set('image', $result['image']) // Configures a new claim, called "image"
  69. ->set('userID', $result['id']) // Configures a new claim, called "image"
  70. ->sign($signer, $key) // creates a signature using "testing" as key
  71. ->getToken(); // Retrieves the generated token
  72. $jwttoken->getHeaders(); // Retrieves the token headers
  73. $jwttoken->getClaims(); // Retrieves the token claims
  74. coookie('set','organizrToken',$jwttoken,$days);
  75. return $jwttoken;
  76. } catch (Dibi\Exception $e) {
  77. return false;
  78. }
  79. }
  80. function validateToken($token,$global=false){
  81. // Validate script
  82. $userInfo = jwtParse($token);
  83. $validated = $userInfo ? true : false;
  84. if($validated == true){
  85. if($global == true){
  86. try {
  87. $database = new Dibi\Connection([
  88. 'driver' => 'sqlite3',
  89. 'database' => $GLOBALS['dbLocation'].$GLOBALS['dbName'],
  90. ]);
  91. $result = $database->fetch('SELECT * FROM users WHERE id = ?',$userInfo['userID']);
  92. $GLOBALS['organizrUser'] = array(
  93. "token"=>$token,
  94. "tokenDate"=>$userInfo['tokenDate'],
  95. "tokenExpire"=>$userInfo['tokenExpire'],
  96. "username"=>$result['username'],
  97. "group"=>$result['group'],
  98. "groupID"=>$result['group_id'],
  99. "email"=>$result['email'],
  100. "image"=>$result['image'],
  101. "userID"=>$result['id'],
  102. "loggedin"=>true,
  103. );
  104. } catch (Dibi\Exception $e) {
  105. $GLOBALS['organizrUser'] = false;
  106. }
  107. }
  108. }else{
  109. // Delete cookie & reload page
  110. coookie('delete','organizrToken');
  111. $GLOBALS['organizrUser'] = false;
  112. }
  113. }
  114. function getOrganizrUserToken(){
  115. if(isset($_COOKIE['organizrToken'])){
  116. // Get token form cookie and validate
  117. validateToken($_COOKIE['organizrToken'],true);
  118. }else{
  119. $GLOBALS['organizrUser'] = array(
  120. "token"=>null,
  121. "tokenDate"=>null,
  122. "tokenExpire"=>null,
  123. "username"=>"Guest",
  124. "group"=>getGuest()['group'],
  125. "groupID"=>getGuest()['group_id'],
  126. "email"=>null,
  127. "image"=>getGuest()['image'],
  128. "userID"=>null,
  129. "loggedin"=>false
  130. );
  131. }
  132. }