-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
480 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Rougin\Authsum\Source; | ||
|
||
/** | ||
* NOTE: This class only parses the JSON web token without validating it. | ||
* Kindly use third-party JSON web token parsed instead (e.g., lcobucci/jwt). | ||
* | ||
* @link https://stackoverflow.com/q/38552003 | ||
* | ||
* @package Authsum | ||
* | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
class JwtParser implements JwtParserInterface | ||
{ | ||
/** | ||
* @link https://www.converticacommerce.com/support-maintenance/security/php-one-liner-decode-jwt-json-web-tokens | ||
* | ||
* Parses the token string. | ||
* | ||
* @param string $token | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function parse($token) | ||
{ | ||
$items = explode('.', $token); | ||
|
||
$parsed = ''; | ||
|
||
if (isset($items[1])) | ||
{ | ||
$parsed = str_replace('-', '+', $items[1]); | ||
} | ||
|
||
$parsed = str_replace('_', '/', $parsed); | ||
|
||
$decoded = base64_decode($parsed); | ||
|
||
/** @var array<string, mixed>|null */ | ||
$result = json_decode($decoded, true); | ||
|
||
if ($result) | ||
{ | ||
return $result; | ||
} | ||
|
||
$text = 'Unable to parse an invalid token'; | ||
|
||
throw new \UnexpectedValueException($text); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Rougin\Authsum\Source; | ||
|
||
/** | ||
* @package Authsum | ||
* | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
interface JwtParserInterface | ||
{ | ||
/** | ||
* Parses the token string. | ||
* | ||
* @param string $token | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function parse($token); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
namespace Rougin\Authsum\Source; | ||
|
||
use Rougin\Authsum\Source; | ||
|
||
/** | ||
* @package Authsum | ||
* | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
class JwtSource extends Source implements WithUsername, WithPayload | ||
{ | ||
/** | ||
* @var \Rougin\Authsum\Source\JwtParserInterface | ||
*/ | ||
protected $parser; | ||
|
||
/** | ||
* @var array<string, string> | ||
*/ | ||
protected $payload = array(); | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $usernameField; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $usernameValue; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $token = 'token'; | ||
|
||
/** | ||
* @param \Rougin\Authsum\Source\JwtParserInterface $parser | ||
*/ | ||
public function __construct(JwtParserInterface $parser) | ||
{ | ||
$this->parser = $parser; | ||
} | ||
|
||
/** | ||
* Checks if it exists from the source. | ||
* | ||
* @return boolean | ||
*/ | ||
public function isValid() | ||
{ | ||
if (! array_key_exists($this->token, $this->payload)) | ||
{ | ||
return $this->setNotFound($this->token); | ||
} | ||
|
||
$token = $this->payload[$this->token]; | ||
|
||
try | ||
{ | ||
$parsed = $this->parser->parse($token); | ||
|
||
if (! array_key_exists($this->usernameField, $parsed)) | ||
{ | ||
return $this->setNotFound($this->usernameField); | ||
} | ||
} | ||
catch (\Exception $e) | ||
{ | ||
return $this->setError($e->getMessage()); | ||
} | ||
|
||
$same = $parsed[$this->usernameField] === $this->usernameValue; | ||
|
||
return $same ? $this->setResult() : $this->setError(); | ||
} | ||
|
||
/** | ||
* Sets the prepared payload. | ||
* | ||
* @param array<string, string> $payload | ||
* | ||
* @return self | ||
*/ | ||
public function setPayload($payload) | ||
{ | ||
$this->payload = $payload; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the username field. | ||
* | ||
* @param string $username | ||
* | ||
* @return self | ||
*/ | ||
public function setUsernameField($username) | ||
{ | ||
$this->usernameField = $username; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the username value. | ||
* | ||
* @param string $username | ||
* | ||
* @return self | ||
*/ | ||
public function setUsernameValue($username) | ||
{ | ||
$this->usernameValue = $username; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $token | ||
* | ||
* @return self | ||
*/ | ||
public function setTokenField($token) | ||
{ | ||
$this->token = $token; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return boolean | ||
*/ | ||
protected function setNotFound($name) | ||
{ | ||
return $this->setError('Field "' . $name . '" not found from 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Rougin\Authsum\Source; | ||
|
||
/** | ||
* @package Authsum | ||
* | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
interface WithPayload | ||
{ | ||
/** | ||
* Sets the prepared payload. | ||
* | ||
* @param array<string, string> $payload | ||
* | ||
* @return self | ||
*/ | ||
public function setPayload($payload); | ||
} |
Oops, something went wrong.