Skip to content

Commit

Permalink
feat(Jwt): add custom builder callback support
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 committed Jan 10, 2025
1 parent 38718f0 commit 084be48
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions bin/release.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash

set -e
if (( "$#" == 0 ))
then
Expand Down
14 changes: 10 additions & 4 deletions src/Jwt/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 src/Jwt/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 084be48

Please sign in to comment.