Skip to content

Commit

Permalink
Issue calcinai#6 additional properties for resource owner
Browse files Browse the repository at this point in the history
Some additional useful properties and also the ability to access them as camelCase (without changing the underlying property names to avoid a BC break)
  • Loading branch information
judgej committed Sep 6, 2020
1 parent c25ddc6 commit 4a4ed25
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/XeroResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,39 @@ class XeroResourceOwner
*/
public $family_name;

/**
* @var string the unique user ID, different to Xero user ID
*/
public $sub;

/**
* @var int details expirey time
*/
public $exp;

/**
* @var int the time the authorisation was first granted for this token
*/
public $auth_time;

/**
* @var int token issued at time; last authentication flow time
*/
public $iat;

/**
* @var string
*/
public $aud;

/**
* @param $token
* @return static
*/
public static function fromJWT($token)
{
list($header, $body, $crypto) = explode('.', $token);

//This needs to be done manually as we don't get a signed JWT
$decoded = JWT::jsonDecode(JWT::urlsafeB64Decode($body));

Expand All @@ -54,7 +80,27 @@ public static function fromJWT($token)
$self->email = $decoded->email;
$self->given_name = $decoded->given_name;
$self->family_name = $decoded->family_name;
$self->sub = $decoded->sub;
$self->exp = $decoded->exp;
$self->auth_time = $decoded->auth_time;
$self->iat = $decoded->iat;
$self->aud = $decoded->aud;

return $self;
}

/**
* Provide camelCase access to the underlying properties.
*
* @param $name
* @return mixed
*/
public function __get($name)
{
$snakeCase = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name));

if (property_exists($this, $snakeCase)) {
return $this->$snakeCase;
}
}
}

0 comments on commit 4a4ed25

Please sign in to comment.