token-functions.php 6.1 KB

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