Skip to content

Commit

Permalink
feat(Jwt): add custom builder callback support (#143)
Browse files Browse the repository at this point in the history
- Update builderAccessToken and builderRefreshToken methods to accept an optional Closure
- The Closure allows custom modifications to the token builder
- This enhancement provides more flexibility for token creation
  • Loading branch information
zds-s authored Jan 10, 2025
1 parent 1954c86 commit fbb19b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions AbstractJwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,32 @@ public function __construct(
private readonly RefreshTokenConstraint $refreshTokenConstraint
) {}

public function builderRefreshToken(string $sub): UnencryptedToken
public function builderRefreshToken(string $sub, ?\Closure $callable = null): UnencryptedToken
{
return $this->getJwtFacade()->issue(
$this->getSigner(),
$this->getSigningKey(),
function (Builder $builder, \DateTimeImmutable $immutable) use ($sub) {
function (Builder $builder, \DateTimeImmutable $immutable) use ($sub, $callable) {
$builder = $builder->identifiedBy($sub);
$builder = $builder->expiresAt($this->getRefreshExpireAt($immutable));
if ($callable !== null) {
$builder = $callable($builder);
}
return $builder->relatedTo('refresh');
}
);
}

public function builderAccessToken(string $sub): UnencryptedToken
public function builderAccessToken(string $sub, ?\Closure $callable = null): UnencryptedToken
{
return $this->getJwtFacade()->issue(
$this->getSigner(),
$this->getSigningKey(),
function (Builder $builder, \DateTimeImmutable $immutable) use ($sub) {
function (Builder $builder, \DateTimeImmutable $immutable) use ($sub, $callable) {
$builder = $builder->identifiedBy($sub);
if ($callable !== null) {
$builder = $callable($builder);
}
return $builder->expiresAt($this->getExpireAt($immutable));
}
);
Expand Down
4 changes: 2 additions & 2 deletions JwtInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

interface JwtInterface
{
public function builderAccessToken(string $sub): UnencryptedToken;
public function builderAccessToken(string $sub, ?\Closure $callable = null): UnencryptedToken;

public function builderRefreshToken(string $sub): UnencryptedToken;
public function builderRefreshToken(string $sub, ?\Closure $callable = null): UnencryptedToken;

public function parserAccessToken(string $accessToken): UnencryptedToken;

Expand Down

0 comments on commit fbb19b1

Please sign in to comment.