public function it_creates_new_tokens_from_a_jwt_payload_interface_object(JwtPayloadInterface $payload, JwtDriverInterface $jwt) { $jwt->createToken(['foo' => 'bar'], 'secret_123', 'HS256')->willReturn('newtoken_123'); $payload->getPayload()->willReturn(['foo' => 'bar']); $result = $this->createToken($payload)->shouldHaveType(JwtToken::class); if ($result->token() !== 'newtoken_123') { throw new \Exception('New token was not set correctly.'); } }
/** * Create a new token with the provided payload * * The default algorithm used is HS256. To set a custom one, set * the env variable JWT_ALGO. * * @todo Support for enforcing required claims in payload as well as defaults * * @param JwtPayloadInterface|array $payload * @param string|null $secret * @param string|null $algo * * @return JwtToken */ public function createToken($payload, $secret = null, $algo = null) { $algo = $algo ?: $this->algorithm(); $secret = $secret ?: $this->secret(); if ($payload instanceof JwtPayloadInterface) { $payload = $payload->getPayload(); } $newToken = $this->jwt->createToken($payload, $secret, $algo); $token = clone $this; $token->setToken($newToken); return $token; }