token-functions.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. trait TokenFunctions
  3. {
  4. public function jwtParse($token)
  5. {
  6. try {
  7. $result = array();
  8. $result['valid'] = false;
  9. // Check Token with JWT
  10. // Set key
  11. if (!isset($this->config['organizrHash'])) {
  12. return null;
  13. }
  14. $key = $this->config['organizrHash'];
  15. // SHA256 Encryption
  16. $signer = new Lcobucci\JWT\Signer\Hmac\Sha256();
  17. $jwttoken = (new Lcobucci\JWT\Parser())->parse((string)$token); // Parses from a string
  18. $jwttoken->getHeaders(); // Retrieves the token header
  19. $jwttoken->getClaims(); // Retrieves the token claims
  20. // Start Validation
  21. if ($jwttoken->verify($signer, $key)) {
  22. $data = new Lcobucci\JWT\ValidationData(); // It will use the current time to validate (iat, nbf and exp)
  23. $data->setIssuer('Organizr');
  24. $data->setAudience('Organizr');
  25. if ($jwttoken->validate($data)) {
  26. $result['valid'] = true;
  27. //$result['username'] = $jwttoken->getClaim('username');
  28. //$result['group'] = $jwttoken->getClaim('group');
  29. //$result['groupID'] = $jwttoken->getClaim('groupID');
  30. $result['userID'] = $jwttoken->getClaim('userID');
  31. //$result['email'] = $jwttoken->getClaim('email');
  32. //$result['image'] = $jwttoken->getClaim('image');
  33. $result['tokenExpire'] = $jwttoken->getClaim('exp');
  34. $result['tokenDate'] = $jwttoken->getClaim('iat');
  35. //$result['token'] = $jwttoken->getClaim('exp');
  36. }
  37. }
  38. if ($result['valid'] == true) {
  39. return $result;
  40. } else {
  41. return false;
  42. }
  43. } catch (\RunException $e) {
  44. return false;
  45. } catch (\OutOfBoundsException $e) {
  46. return false;
  47. } catch (\RunTimeException $e) {
  48. return false;
  49. } catch (\InvalidArgumentException $e) {
  50. return false;
  51. }
  52. }
  53. }