Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow data to be appended to jwt #497

Closed

Conversation

frederikbosch
Copy link
Contributor

At the moment the content of the JWT is defined from inside the library. It is not possible to append data. But an access token entity already has an interface. So it was not hard to implement the feature.

There must be a location that creates the access token, which can be implemented by the developer. So I added another method to the AccessTokenRepositoryInterface called createNewToken.

public function createNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
{
    return new MyAccessTokenEntity();
}

Now the developer can create his own his own implementation of AccessTokenEntityInterface which in turn implements for convertToJWT.

Secondly, after the access token is validated it must be possible to read contents from the jwt. I created an additional event for this BearerWasValidated. From the outside, this might look like this.

$validator = new BearerTokenValidator();
$validator->addListener('validated', function (BearerWasValidated $event) { $jwt = $event->getToken(); });
$server = new Server(.. $validator);

When the access token is validated, the content from the jwt can be read.

@frederikbosch
Copy link
Contributor Author

After thinking things through, it would be best to move convertToJWT to a separate class. Then we can skip createNewToken too. I would be happy to implement either method.

use Lcobucci\JWT\Builder;

class AccessTokenToJwtConverter implements AccessTokenToJwtConverterInterface {
  private $builder;

  public function __construct(Builder $builder) {
    $this->builder = $builder;
  }

  public function convert (AccessTokenEntity $accessToken) {
    // conversion
   return $jwt;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant