laizerox/php-wowemu-auth

Wowemu 兼容的 SRP6 认证实现

v1.0.0 2019-07-26 21:52 UTC

This package is auto-updated.

Last update: 2024-09-13 23:58:33 UTC


README

需求

  • PHP 7.1+
  • Web 服务器(例如 Apache 或 Nginx)
  • CMaNGOS 实例

安装

您可以通过 Composer 安装此库

composer require laizerox/php-wowemu-auth

使用方法

注册

首先,您需要使用 Composer 的 autoloader。将其放在脚本顶部。

require_once __DIR__ . '/vendor/autoload.php';
use Laizerox\Wowemu\SRP\UserClient;

接下来,您需要使用用户在注册表单上提交的用户名和密码来创建验证器和盐值。

$client = new UserClient($username);
$salt = $client->generateSalt();
$verifier = $client->generateVerifier($password);

一旦生成了这些值,只需将它们插入到数据库中的 vs 字段即可。

登录

首先,您需要使用 Composer 的 autoloader。将其放在脚本顶部。

require_once __DIR__ . '/vendor/autoload.php';
use Laizerox\Wowemu\SRP\UserClient;

接下来,您需要生成您的 "验证器"。将其视为用户在登录表单的密码字段中输入的密码的哈希版本。

$client = new UserClient($username, $saltFromDatabase);
$verifier = strtoupper($client->generateVerifier($password));

接下来,您需要将此值与存储在您的 CMaNGOS realmd.account 表中的值进行比较。以下是一个示例。

示例

注册

此示例介绍用户如何通过网页表单进行注册。

<?php

/* register.php */

require_once __DIR__ . '/vendor/autoload.php';
use Laizerox\Wowemu\SRP\UserClient;

/* Connect to your CMaNGOS database. */
$db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);

/* If the form has been submitted. */
if (isset($_POST['register'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    /* Grab the users IP address. */
    $ip = $_SERVER['REMOTE_ADDR'];
    
    /* Set the join date. */
    $joinDate = date('Y-m-d H:i:s');
    
    /* Set GM Level. */
    $gmLevel = '0';

    /* Set expansion pack - Wrath of the Lich King. */
    $expansion = '2';
    
    /* Create your v and s values. */
    $client = new UserClient($username);
    $salt = $client->generateSalt();
    $verifier = $client->generateVerifier($password);

    /* Insert the data into the CMaNGOS database. */
    mysqli_query($db, "INSERT INTO account (username, v, s, gmlevel, email, joindate, last_ip, expansion) VALUES ('$username', '$verifier', '$salt',  '$gmLevel', '$email', '$joinDate', '$ip', '$expansion')");
    
    /* Do some stuff to let the user know it was a successful or unsuccessful attempt. */
}    

?>

显然,您需要进行一些错误检查和验证,但这留给了您。

<form action="/register" method="post">
    <input type="text" name="username" placeholder="Username">
    <input type="email" name="email" placeholder="Email Address">
    <input type="password" name="password" placeholder="Password">
    <?php $register = sha1(time()); ?>
    <input type="hidden" name="register" value="<?php echo $register; ?>">
    <button type="submit">Register</button>
</form>

上面的是一个非常基础的用于用户注册的 HTML 表单。

登录

<?php

/* login.php */

require_once __DIR__ . '/vendor/autoload.php';
use Laizerox\Wowemu\SRP\UserClient;

/* Connect to your CMaNGOS database. */
$db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);

/* Function to get values from MySQL. */
function getMySQLResult($query) {
    global $db;
    return $db->query($query)->fetch_object();
}

/* If the form has been submitted. */
if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    /* Get the salt and verifier from realmd.account for the user. */
    $query = "SELECT s,v FROM account WHERE username='$username'";
    $result = getMySQLResult($query);
    $saltFromDatabase = $result->s;
    $verifierFromDatabase = strtoupper($result->v);
    
    /* Setup your client and verifier values. */
    $client = new UserClient($username, $saltFromDatabase);
    $verifier = strtoupper($client->generateVerifier($password));

    /* Compare $verifierFromDatabase and $verifier. */
    if ($verifierFromDatabase === $verifier) {
        /* Do your login stuff here, like setting cookies/sessions... */
    }
    else {
        /* Do whatever you wanna do when the login has failed, send a failure message, redirect them to another page, etc... */
    }

?>

同样,您需要添加自己的错误检查和验证,但这应该能让您开始。

<form action="/login" method="post">
    <input type="text" name="username" placeholder="Username">
    <input type="password" name="password" placeholder="Password">
    <?php $login = sha1(time()); ?>
    <input type="hidden" name="login" value="<?php echo $login; ?>">
    <button type="submit">Sign In</button>
</form>

上面的是一个非常基础的用于用户登录的 HTML 表单。

如果在使用库时发现任何缺陷,请在存储库中打开一个新的问题。如果需要进一步的帮助,我们可以在 CMaNGOS Discord 服务器#offtopic 频道中帮助您。