diff --git a/samples/website_integration/bcrypt/form.php b/samples/website_integration/bcrypt/form.php new file mode 100644 index 00000000..7801be4d --- /dev/null +++ b/samples/website_integration/bcrypt/form.php @@ -0,0 +1,86 @@ + + + + + AuthMe Integration Sample + + + +Login sample +This is a demo form for AuthMe website integration. Enter your AuthMe login details +into the following form to test it. +
+ + + + + + + +
Name
Pass
+
'; +} + +function get_from_post_or_empty($index_name) { + return trim( + filter_input(INPUT_POST, $index_name, FILTER_UNSAFE_RAW, FILTER_REQUIRE_SCALAR | FILTER_FLAG_STRIP_LOW) + ?: ''); +} + + +// Login logic +function process_login($user, $pass) { + if (authme_check_password($user, $pass)) { + printf('

Hello, %s!

', htmlspecialchars($user)); + echo 'Successful login. Nice to have you back!' + . '
Back to form'; + return true; + } else { + echo '

Error

Invalid username or password.'; + } + return false; +} + +// Register logic +function process_register($user, $pass) { + if (authme_has_user($user)) { + echo '

Error

This user already exists.'; + } else { + // Note that we don't validate the password or username at all in this demo... + $register_success = authme_register($user, $pass); + if ($register_success) { + printf('

Welcome, %s!

Thanks for registering', htmlspecialchars($user)); + echo '
Back to form'; + return true; + } else { + echo '

Error

Unfortunately, there was an error during the registration.'; + } + } + return false; +} + +?> + + + diff --git a/samples/website_integration/bcrypt/integration.php b/samples/website_integration/bcrypt/integration.php new file mode 100644 index 00000000..75911838 --- /dev/null +++ b/samples/website_integration/bcrypt/integration.php @@ -0,0 +1,107 @@ +prepare('SELECT password FROM ' . AUTHME_TABLE . ' WHERE username = ?'); + $stmt->bind_param('s', $username); + $stmt->execute(); + $stmt->bind_result($password); + if ($stmt->fetch()) { + return $password; + } + } + return null; +} + +/** + * Returns whether the user exists in the database or not. + * + * @param string $username the username to check + * @return bool true if the user exists; false otherwise + */ +function authme_has_user($username) { + $mysqli = authme_get_mysqli(); + if ($mysqli !== null) { + $stmt = $mysqli->prepare('SELECT 1 FROM ' . AUTHME_TABLE . ' WHERE username = ?'); + $stmt->bind_param('s', $username); + $stmt->execute(); + return $stmt->fetch(); + } + + // Defensive default to true; we actually don't know + return true; +} + +/** + * Registers a player with the given username. + * + * @param string $username the username to register + * @param string $password the password to associate to the user + * @return bool whether or not the registration was successful + */ +function authme_register($username, $password) { + $mysqli = authme_get_mysqli(); + if ($mysqli !== null) { + $hash = password_hash($password, PASSWORD_BCRYPT); + $stmt = $mysqli->prepare('INSERT INTO ' . AUTHME_TABLE . ' (username, realname, password, ip) ' + . 'VALUES (?, ?, ?, ?)'); + $username_low = strtolower($username); + $stmt->bind_param('ssss', $username, $username_low, $hash, $_SERVER['REMOTE_ADDR']); + return $stmt->execute(); + } + return false; +} + diff --git a/samples/website_integration/form.php b/samples/website_integration/sha256/form.php similarity index 91% rename from samples/website_integration/form.php rename to samples/website_integration/sha256/form.php index 5f2985cb..5ffecf34 100644 --- a/samples/website_integration/form.php +++ b/samples/website_integration/sha256/form.php @@ -1,5 +1,5 @@ @@ -36,7 +36,7 @@ into the following form to test it. -
Name
Pass
+
'; } diff --git a/samples/website_integration/integration.php b/samples/website_integration/sha256/integration.php similarity index 97% rename from samples/website_integration/integration.php rename to samples/website_integration/sha256/integration.php index 3008fb98..e0de0bb1 100644 --- a/samples/website_integration/integration.php +++ b/samples/website_integration/sha256/integration.php @@ -1,6 +1,6 @@