token-functions.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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['email'] = $jwttoken->getClaim('email');
  28. $result['image'] = $jwttoken->getClaim('image');
  29. $result['tokenExpire'] = $jwttoken->getClaim('exp');
  30. $result['tokenDate'] = $jwttoken->getClaim('iat');
  31. $result['token'] = $jwttoken->getClaim('exp');
  32. }
  33. }
  34. if($result['valid'] == true){ return $result; }else{ return false; }
  35. } catch(\RunException $e) {
  36. return false;
  37. } catch(\OutOfBoundsException $e) {
  38. return false;
  39. } catch(\RunTimeException $e) {
  40. return false;
  41. } catch(\InvalidArgumentException $e) {
  42. return false;
  43. }
  44. }
  45. function createToken($username,$email,$image,$group,$groupID,$key,$days = 1){
  46. // Create JWT
  47. // Set key
  48. // SHA256 Encryption
  49. $signer = new Lcobucci\JWT\Signer\Hmac\Sha256();
  50. // Start Builder
  51. $jwttoken = (new Lcobucci\JWT\Builder())->setIssuer('Organizr') // Configures the issuer (iss claim)
  52. ->setAudience('Organizr') // Configures the audience (aud claim)
  53. ->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
  54. ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
  55. ->setExpiration(time() + (86400 * $days)) // Configures the expiration time of the token (exp claim)
  56. ->set('username', $username) // Configures a new claim, called "username"
  57. ->set('group', $group) // Configures a new claim, called "group"
  58. ->set('groupID', $groupID) // Configures a new claim, called "groupID"
  59. ->set('email', $email) // Configures a new claim, called "email"
  60. ->set('image', $image) // Configures a new claim, called "image"
  61. ->sign($signer, $key) // creates a signature using "testing" as key
  62. ->getToken(); // Retrieves the generated token
  63. $jwttoken->getHeaders(); // Retrieves the token headers
  64. $jwttoken->getClaims(); // Retrieves the token claims
  65. coookie('set','organizrToken',$jwttoken,$days);
  66. return $jwttoken;
  67. }
  68. function validateToken($token,$global=false){
  69. // Validate script
  70. $userInfo = jwtParse($token);
  71. $validated = $userInfo ? true : false;
  72. if($validated == true){
  73. if($global == true){
  74. $GLOBALS['organizrUser'] = array(
  75. "token"=>$token,
  76. "tokenDate"=>$userInfo['tokenDate'],
  77. "tokenExpire"=>$userInfo['tokenExpire'],
  78. "username"=>$userInfo['username'],
  79. "group"=>$userInfo['group'],
  80. "groupID"=>$userInfo['groupID'],
  81. "email"=>$userInfo['email'],
  82. "image"=>$userInfo['image'],
  83. "loggedin"=>true,
  84. );
  85. }
  86. }else{
  87. // Delete cookie & reload page
  88. coookie('delete','organizrToken');
  89. $GLOBALS['organizrUser'] = false;
  90. }
  91. }
  92. function getOrganizrUserToken(){
  93. if(isset($_COOKIE['organizrToken'])){
  94. // Get token form cookie and validate
  95. validateToken($_COOKIE['organizrToken'],true);
  96. }else{
  97. $GLOBALS['organizrUser'] = array(
  98. "token"=>null,
  99. "tokenDate"=>null,
  100. "tokenExpire"=>null,
  101. "username"=>"Guest",
  102. "group"=>getGuest()['group'],
  103. "groupID"=>getGuest()['group_id'],
  104. "email"=>null,
  105. "image"=>getGuest()['image'],
  106. "loggedin"=>false
  107. );
  108. }
  109. }