token-functions.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. if (!isset($GLOBALS['dbLocation']) || !isset($GLOBALS['dbName'])) {
  54. return false;
  55. }
  56. //Quick get user ID
  57. try {
  58. $database = new Dibi\Connection([
  59. 'driver' => 'sqlite3',
  60. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  61. ]);
  62. $result = $database->fetch('SELECT * FROM users WHERE username = ? COLLATE NOCASE OR email = ? COLLATE NOCASE', $username, $email);
  63. // Create JWT
  64. // Set key
  65. // SHA256 Encryption
  66. $signer = new Lcobucci\JWT\Signer\Hmac\Sha256();
  67. // Start Builder
  68. $jwttoken = (new Lcobucci\JWT\Builder())->setIssuer('Organizr')// Configures the issuer (iss claim)
  69. ->setAudience('Organizr')// Configures the audience (aud claim)
  70. ->setId('4f1g23a12aa', true)// Configures the id (jti claim), replicating as a header item
  71. ->setIssuedAt(time())// Configures the time that the token was issue (iat claim)
  72. ->setExpiration(time() + (86400 * $days))// Configures the expiration time of the token (exp claim)
  73. ->set('username', $result['username'])// Configures a new claim, called "username"
  74. ->set('group', $result['group'])// Configures a new claim, called "group"
  75. ->set('groupID', $result['group_id'])// Configures a new claim, called "groupID"
  76. ->set('email', $result['email'])// Configures a new claim, called "email"
  77. ->set('image', $result['image'])// Configures a new claim, called "image"
  78. ->set('userID', $result['id'])// Configures a new claim, called "image"
  79. ->sign($signer, $key)// creates a signature using "testing" as key
  80. ->getToken(); // Retrieves the generated token
  81. $jwttoken->getHeaders(); // Retrieves the token headers
  82. $jwttoken->getClaims(); // Retrieves the token claims
  83. coookie('set', 'organizrToken', $jwttoken, $days);
  84. return $jwttoken;
  85. } catch (Dibi\Exception $e) {
  86. return false;
  87. }
  88. }
  89. function validateToken($token, $global = false)
  90. {
  91. // Validate script
  92. $userInfo = jwtParse($token);
  93. $validated = $userInfo ? true : false;
  94. if ($validated == true) {
  95. if ($global == true) {
  96. try {
  97. $database = new Dibi\Connection([
  98. 'driver' => 'sqlite3',
  99. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  100. ]);
  101. $result = $database->fetch('SELECT * FROM users WHERE id = ?', $userInfo['userID']);
  102. $GLOBALS['organizrUser'] = array(
  103. "token" => $token,
  104. "tokenDate" => $userInfo['tokenDate'],
  105. "tokenExpire" => $userInfo['tokenExpire'],
  106. "username" => $result['username'],
  107. "group" => $result['group'],
  108. "groupID" => $result['group_id'],
  109. "email" => $result['email'],
  110. "image" => $result['image'],
  111. "userID" => $result['id'],
  112. "loggedin" => true,
  113. );
  114. } catch (Dibi\Exception $e) {
  115. $GLOBALS['organizrUser'] = false;
  116. }
  117. }
  118. } else {
  119. // Delete cookie & reload page
  120. coookie('delete', 'organizrToken');
  121. $GLOBALS['organizrUser'] = false;
  122. }
  123. }
  124. function getOrganizrUserToken()
  125. {
  126. if (isset($_COOKIE['organizrToken'])) {
  127. // Get token form cookie and validate
  128. validateToken($_COOKIE['organizrToken'], true);
  129. } else {
  130. $GLOBALS['organizrUser'] = array(
  131. "token" => null,
  132. "tokenDate" => null,
  133. "tokenExpire" => null,
  134. "username" => "Guest",
  135. "group" => getGuest()['group'],
  136. "groupID" => getGuest()['group_id'],
  137. "email" => null,
  138. //"groupImage"=>getGuest()['image'],
  139. "image" => getGuest()['image'],
  140. "userID" => null,
  141. "loggedin" => false
  142. );
  143. }
  144. }