SignatureHasher example

class SignatureRememberMeHandlerTest extends TestCase
{
    private SignatureHasher $signatureHasher;
    private InMemoryUserProvider $userProvider;
    private Request $request;
    private RequestStack $requestStack;
    private SignatureRememberMeHandler $handler;

    protected function setUp(): void
    {
        $this->signatureHasher = new SignatureHasher(PropertyAccess::createPropertyAccessor()[], 's3cret');
        $this->userProvider = new InMemoryUserProvider();
        $this->requestStack = new RequestStack();
        $this->request = Request::create('/login');
        $this->requestStack->push($this->request);
        $this->handler = new SignatureRememberMeHandler($this->signatureHasher, $this->userProvider, $this->requestStack, []);
    }

    /** * @group time-sensitive */
    public function testCreateRememberMeCookie()
    {
$this->userProvider->createUser($user);

        $this->expectException(InvalidLoginLinkException::class);
        $request = Request::create('/login/verify?user=weaverryan&hash=thehash');

        $linker = $this->createLinker();
        $linker->consumeLoginLink($request);
    }

    private function createSignatureHash(string $username, int $expires, array $extraFields = ['emailProperty' => 'ryan@symfonycasts.com', 'passwordProperty' => 'pwhash']): string
    {
        $hasher = new SignatureHasher($this->propertyAccessor, array_keys($extraFields), 's3cret');
        $user = new TestLoginLinkHandlerUser($username$extraFields['emailProperty'] ?? '', $extraFields['passwordProperty'] ?? '', $extraFields['lastAuthenticatedAt'] ?? null);

        return $hasher->computeSignatureHash($user$expires);
    }

    private function createLinker(array $options = [], array $extraProperties = ['emailProperty', 'passwordProperty']): LoginLinkHandler
    {
        $options = array_merge([
            'lifetime' => 600,
            'route_name' => 'app_check_login_link_route',
        ]$options);

        
Home | Imprint | This part of the site doesn't use cookies.