From 9e38fd27ab86ea9a45f9c65b0d75b14fb705fe44 Mon Sep 17 00:00:00 2001 From: jakani24 Date: Fri, 26 Apr 2024 14:36:34 +0200 Subject: [PATCH] Update login.php --- src/server/cyberhex-code/test/login.php | 140 +++++++++++++++++++----- 1 file changed, 113 insertions(+), 27 deletions(-) diff --git a/src/server/cyberhex-code/test/login.php b/src/server/cyberhex-code/test/login.php index bf6294b..9aaf524 100644 --- a/src/server/cyberhex-code/test/login.php +++ b/src/server/cyberhex-code/test/login.php @@ -1,5 +1,5 @@ - - - - - - - - - addRootCertificates('rootCertificates/solo.pem'); + //} + //if (filter_input(INPUT_GET, 'apple')) { + $WebAuthn->addRootCertificates('rootCertificates/apple.pem'); + //} + //if (filter_input(INPUT_GET, 'yubico')) { + $WebAuthn->addRootCertificates('rootCertificates/yubico.pem'); + //} + //if (filter_input(INPUT_GET, 'hypersecu')) { + $WebAuthn->addRootCertificates('rootCertificates/hypersecu.pem'); + //} + //if (filter_input(INPUT_GET, 'google')) { + $WebAuthn->addRootCertificates('rootCertificates/globalSign.pem'); + $WebAuthn->addRootCertificates('rootCertificates/googleHardware.pem'); + //} + //if (filter_input(INPUT_GET, 'microsoft')) { + $WebAuthn->addRootCertificates('rootCertificates/microsoftTpmCollection.pem'); + //} + //if (filter_input(INPUT_GET, 'mds')) { + $WebAuthn->addRootCertificates('rootCertificates/mds'); + //} + + } // Handle different functions if ($fn === 'getCreateArgs') { @@ -253,19 +315,43 @@ try { // Save challenge to session or somewhere else if needed } else if ($fn === 'getGetArgs') { - // Get get arguments - // Retrieve credential IDs from the database based on $userId - $ids = []; // Fetch credential IDs from the database - $getArgs = $WebAuthn->getGetArgs($ids, 60*4); + $ids = []; + + if ($requireResidentKey) { + if (!isset($_SESSION['registrations']) || !is_array($_SESSION['registrations']) || count($_SESSION['registrations']) === 0) { + throw new Exception('we do not have any registrations in session to check the registration'); + } + + } else { + // load registrations from session stored there by processCreate. + // normaly you have to load the credential Id's for a username + // from the database. + if (isset($_SESSION['registrations']) && is_array($_SESSION['registrations'])) { + foreach ($_SESSION['registrations'] as $reg) { + if ($reg->userId === $userId) { + $ids[] = $reg->credentialId; + } + } + } + + if (count($ids) === 0) { + throw new Exception('no registrations in session for userId ' . $userId); + } + } + + $getArgs = $WebAuthn->getGetArgs($ids, 60*4, $typeUsb, $typeNfc, $typeBle, $typeHyb, $typeInt, $userVerification); + header('Content-Type: application/json'); print(json_encode($getArgs)); - // Save challenge to session or somewhere else if needed - } else if ($fn === 'processGet') { + // save challange to session. you have to deliver it to processGet later. + $_SESSION['challenge'] = $WebAuthn->getChallenge(); + + }else if ($fn === 'processGet') { // Process get // Retrieve registration data from the database based on credential ID $id = base64_decode($post->id); - $stmt = $conn->prepare("SELECT * FROM registrations WHERE credentialId = ?"); + $stmt = $conn->prepare("SELECT * FROM user WHERE credential_id = ?"); $stmt->execute([$id]); $registration = $stmt->fetch(PDO::FETCH_ASSOC); @@ -278,7 +364,7 @@ try { $signature = base64_decode($post->signature); $userHandle = base64_decode($post->userHandle); $challenge = $_SESSION['challenge'] ?? ''; - $credentialPublicKey = $registration['publicKey']; + $credentialPublicKey = $registration['public_key']; // Process the get request $WebAuthn->processGet($clientDataJSON, $authenticatorData, $signature, $credentialPublicKey, $challenge, null, $userVerification === 'required'); @@ -298,5 +384,5 @@ try { header('Content-Type: application/json'); print(json_encode($return)); } -*/ + ?>