Created
November 12, 2023 15:07
-
-
Save phacman/20089a73684c324a1194d631458a25bc to your computer and use it in GitHub Desktop.
Extract data from standard keycloak payload via "__call" magic method.
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 PhacMan\MultiTool\Keycloak; | |
use PhacMan\MultiTool\Convert\CompoundWord; | |
/** | |
* Extract data from standard keycloak payload. | |
* | |
* @method int|null getExp() 1673171296 | |
* @method int|null getIat() 1673170996 | |
* @method string|null getJti() d58f5dc8-fd8f-462b-8a19-716f8f83985b | |
* @method string|null getIss() http://localhost:6080/auth/realms/something | |
* @method string|null getAud() account | |
* @method string|null getSub() 36fa0f91-f94d-4a0c-afed-6b7d952e47da | |
* @method string|null getTyp() Bearer | |
* @method string|null getAzp() something | |
* @method string|null getSessionState() 2a88c258-f73a-4103-b862-e1fbd8f4e04e | |
* @method string|null getAcr() 1 | |
* @method array|null getAllowedOrigins() [] | |
* @method array|null getRealmAccess() [] | |
* @method array|null getResourceAccess() [] | |
* @method string|null getScope() email profile | |
* @method string|null getSid() 2a88c258-f73a-4103-b862-e1fbd8f4e04e | |
* @method bool|null getEmailVerified() true | |
* @method string|null getName() Something Name | |
* @method string|null getPreferredUsername() username | |
* @method string|null getGivenName() Given Name | |
* @method string|null getFamilyName() Family Name | |
* @method string|null getEmail() some@thing.com | |
*/ | |
class PayloadDto | |
{ | |
private CompoundWord $convert; | |
/** | |
* @var array<string, array<int, string>|int|string> | |
*/ | |
private array $data; | |
public function __construct( | |
string $payload | |
) { | |
$json = json_decode($payload, true); | |
$this->data = !\is_array($json) ? [] : $json; | |
$this->convert = new CompoundWord(); | |
} | |
/** | |
* @param string $name | |
* @param array<int, string> $arguments | |
* @return mixed | |
*/ | |
public function __call(string $name, array $arguments = []): mixed | |
{ | |
$key = $this->convert->camelToSnake($name); | |
$key = substr($key, 4); | |
if ('allowed_origins' === $key) { | |
$key = 'allowed-origins'; | |
} | |
return $this->data[$key] ?? null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment