Further preparation for 2FA and WebAuthn

This commit is contained in:
Pinga 2023-11-18 12:24:49 +02:00
parent 95e47cd9a6
commit e7ddc2e997
4 changed files with 87 additions and 2 deletions

View file

@ -27,5 +27,38 @@ class ProfileController extends Controller
return view($response,'admin/profile/profile.twig',['email' => $email, 'username' => $username, 'status' => $status, 'role' => $role]);
}
public function getRegistrationChallenge(Request $request, Response $response)
{
$user = $request->getAttribute('user'); // Assuming you have the user info
$username = $user->getUsername(); // Replace with your method to get the username
$userEmail = $user->getEmail(); // Replace with your method to get the user's email
$challenge = $this->webAuthn->prepareChallengeForRegistration($username, $userEmail);
$_SESSION['webauthn_challenge'] = $challenge; // Store the challenge in the session
$response->getBody()->write(json_encode($challenge));
return $response->withHeader('Content-Type', 'application/json');
}
public function verifyRegistration(Request $request, Response $response)
{
$data = json_decode($request->getBody()->getContents(), true);
try {
$credential = $this->webAuthn->processCreate($data, $_SESSION['webauthn_challenge']);
unset($_SESSION['webauthn_challenge']);
// Store the credential data in the database
// $user->addWebAuthnCredential($credential);
$response->getBody()->write(json_encode(['success' => true]));
return $response->withHeader('Content-Type', 'application/json');
} catch (\Exception $e) {
// Handle error, return an appropriate response
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
return $response->withHeader('Content-Type', 'application/json')->withStatus(400);
}
}
}