Created
November 8, 2022 04:11
-
-
Save kimboslice99/48bd0a54c3cc67c273da87db4636388c to your computer and use it in GitHub Desktop.
Authenticate OpenVPN against a DB, with Bcrypt hashed passwords
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!isset($argv[1])) | |
{ | |
print 'not ok'; | |
exit(1); | |
} | |
$data = file($argv[1], FILE_IGNORE_NEW_LINES); | |
// Add your [address], [dbuser], [pass], [database] | |
$conn = new mysqli("127.0.0.1", "root", "password", "openvpn"); | |
if ($conn->connect_error) // maybe remove this when you have the script setup | |
{ | |
die("Connection failed: " . $conn->connect_error); | |
} | |
// Create db named openvpn and a table named users | |
// Then add two columns, one named password and another named user | |
// populate them with [username], [bcrypt-hashed-pass] | |
$stmt = $conn->prepare("SELECT password FROM users WHERE user = ?"); | |
$stmt->bind_param("s", $data[0]); | |
$stmt->execute(); | |
$result = $stmt->get_result()->fetch_assoc(); | |
if (!$result) { echo 'not ok'; exit(1); } // User non-existent | |
if (password_verify($data[1], $result['password'])) | |
{ | |
echo 'ok'; | |
exit(0); | |
} | |
else | |
{ | |
echo 'not ok'; | |
exit(1); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment