Example #1
0
            if ($user->getSecret()) {
                $_SESSION['user_in_progress'] = $user;
                $app->redirect('/auth2fa');
            }
            $_SESSION['user'] = $user;
            $app->redirect('/setup2fa');
        }
    }
    $app->flash('error', 'Failed to log in');
    $app->redirect('/login');
});
$app->get('/setup2fa', function () use($app) {
    $user = $_SESSION['user'];
    $g = new \Google\Authenticator\GoogleAuthenticator();
    // invent a secret for this user
    $secret = $g->generateSecret();
    $app->flash('secret', $secret);
    // Create a QR code via Google charts. The data to encode (chl) is:
    //      otpauth://totp/{label}?secret={secret}
    // where:
    //      label = {hostname}:{username}
    //
    // (see https://code.google.com/p/google-authenticator/wiki/KeyUriFormat)
    $data = sprintf("otpauth://totp/%s%%3A%s%%3Fsecret%%3D%s", $_SERVER['HTTP_HOST'], $user->getUsername(), $secret);
    $qrCodeUrl = "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=" . $data;
    $app->render('setup2fa.twig', ['user' => $_SESSION['user'], 'secret' => $secret, 'qrCodeUrl' => $qrCodeUrl]);
});
$app->post('/setup2fa', function () use($app) {
    $secret = $app->environment['slim.flash']['secret'];
    $code = $app->request->post('code');
    $g = new \Google\Authenticator\GoogleAuthenticator();