Skip to content

Commit

Permalink
some cleanup, tighten up a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
jszobody committed Feb 13, 2023
1 parent b2a8475 commit 39730f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 72 deletions.
100 changes: 29 additions & 71 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\ForwardsCalls;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
Expand All @@ -19,39 +21,18 @@
*/
class Client
{
use ForwardsCalls, Conditionable;

protected Builder $builder;
protected string $signingKey;
protected bool $isSigned = false;
protected array $configures = [];

public function __construct(
protected string $defaultSigningKey,
protected string $signingKey,
protected int|CarbonImmutable $lifetime,
protected string $issuer,
protected string $audience)
{
$this->reset();
}

public function reset(): self
{
$this->builder = new Builder(new JoseEncoder(), ChainedFormatter::default());
$this->lifetime($this->lifetime);
$this->configures = [];
$this->signingKey = $this->defaultSigningKey;
$this->isSigned = false;

return $this;
}

public function defaultAudience(): string
{
return $this->audience;
}

public function defaultIssuer(): string
{
return $this->issuer;
}

public function signWith(string $signingKey): self
Expand All @@ -66,44 +47,46 @@ public function signingKey(): string
return $this->signingKey;
}

public function getToken(): Plain
public function audience(): string
{
// Ensure we have an audience set
if (!in_array('permittedFor', $this->configures)) {
$this->builder->permittedFor($this->audience);
}

// Ensure we have an issuer set
if (!in_array('issuedBy', $this->configures)) {
$this->builder->issuedBy($this->issuer);
}
return $this->audience;
}

$token = $this->builder->getToken(new Sha256(), InMemory::plainText($this->signingKey()));
public function issuer(): string
{
return $this->issuer;
}

$this->reset();
public function getToken(): Plain
{
// Set our own default audience, issuer, and expiration if none has been set so far
in_array('permittedFor', $this->configures) || $this->permittedFor($this->audience());
in_array('issuedBy', $this->configures) || $this->issuedBy($this->issuer());
in_array('expiresAt', $this->configures) || $this->lifetime($this->lifetime);

return $token;
return $this->builder->getToken(new Sha256(), InMemory::plainText($this->signingKey()));
}

public function __toString(): string
{
return (string) $this->getToken();
return $this->getToken()->toString();
}

public function expiresAt(DateTime|DateTimeImmutable $expiration): self
public function expiresAt(DateTimeInterface $expiration): self
{
if($expiration instanceof DateTime) {
$expiration = DateTimeImmutable::createFromMutable($expiration);
}

$this->builder->expiresAt($expiration);
$this->configures[] = "expiresAt";

return $this;
}

public function lifetime(int $lifetime): self
public function lifetime(int $seconds): self
{
$this->builder->expiresAt(CarbonImmutable::now()->addSeconds($lifetime));
$this->expiresAt(CarbonImmutable::now()->addSeconds($seconds));

return $this;
}
Expand All @@ -117,48 +100,23 @@ public function withClaims(array $claims = []): self
return $this;
}

public function get(string$id, array $claims = [], int|DateTimeInterface $lifetime = null, string $signingKey = null): string
public function get(string $id, array $claims = [], int|DateTimeInterface $lifetime = null, string $signingKey = null): string
{
if ($signingKey !== null) {
$this->signWith($signingKey);
}

if(is_int($lifetime)) {
$this->lifetime($lifetime);
}

if($lifetime instanceof DateTimeInterface) {
$this->expiresAt($lifetime);
}

return $this
->when($signingKey !== null, fn() => $this->signWith($signingKey))
->when(is_int($lifetime), fn() => $this->lifetime($lifetime))
->when($lifetime instanceof DateTimeInterface, fn() => $this->expiresAt($lifetime))
->withClaims($claims)
->identifiedBy($id)
->getToken()
->toString();
}

public function setAudience(string $audience): self
{
$this->builder->permittedFor($audience);
$this->claims[] = "aud";

return $this;
}

public function setIssuer(string $issuer)
{
$this->builder->issuedBy($issuer);
$this->claims[] = "iss";

return $this;
}

public function __call(string $method, array $parameters): mixed
{
$this->configures[] = $method;

$result = call_user_func_array([$this->builder, $method], $parameters);
$result = $this->forwardCallTo($this->builder, $method, $parameters);

return $result instanceof Builder
? $this
Expand Down
2 changes: 1 addition & 1 deletion src/ParsedToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function validationRules(string $id, $signingKey): array
new LooseValidAt(SystemClock::fromUTC()),

// Optionally check that the token was intended for us
config('jwt.validate.audience') ? new PermittedFor(JWT::defaultAudience()) : null,
config('jwt.validate.audience') ? new PermittedFor(JWT::audience()) : null,

// And finally that it has the correct ID
new IdentifiedBy($id)
Expand Down

0 comments on commit 39730f8

Please sign in to comment.