-
-
Save fhferreira/f3e31966b2c0aaf70ed1 to your computer and use it in GitHub Desktop.
Simple JWT-PHP Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Services\Jwt\Exceptions; | |
class BeforeValidException extends \UnexpectedValueException { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Services\Jwt\Exceptions; | |
class ExpiredException extends \UnexpectedValueException { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Services\Jwt\Exceptions; | |
class InvalidKeyException extends \UnexpectedValueException { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\Jwt; | |
use JWT; | |
use App\Services\Jwt\Exceptions\BeforeValidException; | |
use App\Services\Jwt\Exceptions\ExpiredException; | |
use App\Services\Jwt\Exceptions\SignatureInvalidException; | |
use App\Services\Jwt\Exceptions\InvalidKeyException; | |
class Manager | |
{ | |
/** | |
* The secret. | |
* | |
* @var string | |
*/ | |
protected $secret; | |
/** | |
* Constructs the JWT Manager with the configured secret. | |
* | |
* @param string|null $secret | |
*/ | |
public function __construct($secret = null) | |
{ | |
$this->secret = conf('services.jwt.secret', $secret); | |
} | |
/** | |
* Encodes a payload. | |
* | |
* @param array $payload | |
* @param array|null $scopes | |
* | |
* @return string | |
*/ | |
public function encode($payload = [], $scopes = null) | |
{ | |
$payload = array_merge($payload, [ | |
'scopes' => is_array($scopes) ? $scopes : config('services.jwt.default_scopes', '*') | |
]); | |
return JWT::encode($payload, $this->secret); | |
} | |
/** | |
* Decodes a payload. | |
* | |
* @param string $payload | |
* | |
* @return array | |
*/ | |
public function decode($payload) | |
{ | |
try { | |
$payload = JWT::decode($payload, $this->secret); | |
} | |
catch (\BeforeValidException $e) | |
{ | |
throw new BeforeValidException; | |
} | |
catch (\ExpiredException $e) | |
{ | |
throw new ExpiredException; | |
} | |
catch (\SignatureInvalidException $e) | |
{ | |
throw new SignatureInvalidException; | |
} | |
catch (\Exception $e) | |
{ | |
throw new InvalidKeyException; | |
} | |
return (array) $payload; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Services\Jwt\Exceptions; | |
class SignatureInvalidException extends \UnexpectedValueException { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment