/** * @test */ public function generateToken_useSecureRandom_validToken() { $generator = new TrustedTokenGenerator(); //Use SecureRandom $token = $generator->generateToken(20); $this->assertEquals(20, strlen($token)); }
/** * Create a cookie for trusted computer * * @param \Scheb\TwoFactorBundle\Model\TrustedComputerInterface $user * @param \Symfony\Component\HttpFoundation\Request $request */ public function createTrustedCookie(Request $request, TrustedComputerInterface $user) { $tokenList = $request->cookies->get($this->cookieName, null); // Generate new token $token = $this->tokenGenerator->generateToken(32); $tokenList .= ($tokenList !== null ? ";" : "") . $token; $validUntil = $this->getDateTimeNow()->add(new \DateInterval("PT" . $this->cookieLifetime . "S")); // Add token to user entity $user->addTrustedComputer($token, $validUntil); $this->persister->persist($user); // Create cookie return new Cookie($this->cookieName, $tokenList, $validUntil, "/"); }
/** * Create a cookie for trusted computer. * * @param Request $request * @param mixed $user * * * @return Cookie */ public function createTrustedCookie(Request $request, $user) { $tokenList = $request->cookies->get($this->cookieName, null); // Generate new token $token = $this->tokenGenerator->generateToken(32); $tokenList .= ($tokenList !== null ? ';' : '') . $token; $validUntil = $this->getDateTimeNow()->add(new \DateInterval('PT' . $this->cookieLifetime . 'S')); // Add token to user entity $this->trustedComputerManager->addTrustedComputer($user, $token, $validUntil); $domain = null; $requestHost = $request->getHost(); if ($requestHost !== 'localhost') { $domain = '.' . $requestHost; } // Create cookie return new Cookie($this->cookieName, $tokenList, $validUntil, '/', $domain, $this->cookieSecure); }