dongshi2458 2015-08-02 18:03
浏览 47
已采纳

阻止用户生成身份验证令牌

So I have an API with an endpoint like so POST http://localhost:8080/api/authenticate. Go to that and you POST an email and password and you get an authentication token. Seems solid so far.

Now what about registering. I'd need to create some sort of request token, but how would I prevent random users from creating a request token through the API? I was thinking of generating the token from http://localhost:8080/api/request then returning it and checking if it existed when a user went to register. The thing is anybody can go to http://localhost:8080/api/request, even if they're not planning on registering. Any idea?

  • 写回答

1条回答 默认 最新

  • dongyulian5801 2015-08-02 18:24
    关注

    Authenticate the user before you generate an auth-token(cookie)... so write a query to pull the id from the user table where email/password matches... so only if the query returns an id then you know the user exists and is "authenticated", therefore you can now generate a auth-token(cookie) setting it to the usrid...

    However setting the cookie to the usrid is not a good practice! since anyone on the client-side could easily set/pass a cookie with a number in a POST request... you need implement some mechanism of encryption to generate a "session" which you can then use as a unique hack-proof user identifier.

    I wrote a simple encryption function in the code below which you can use.

    <?php
    //CHECK IF POST DATA IS EMPTY IF SO KILL THE SCRIPT
    if(empty($_POST['email']) || empty($_POST['pass'])){
        //echo("error msg"); 
        die();
    }
    $mysqli = new mysqli('localhost', 'root', 'root', 'accounts');
    $pass = md5($_POST['pass']);
    
    $query = "SELECT `id` FROM `users` WHERE `email` = '" 
        . $mysqli->real_escape_string($_POST['email']) 
        . "' AND `password` = '" 
        . $mysqli->real_escape_string($_POST['pass']) . "'"; 
    
    //INITIATE DB REQUEST
    $result = $mysqli->query($query);
    //SELECT ID(OBJ) FROM THE RESULT OBJECT
    $usrid = $result->fetch_object()->id;
    //IF $USRID IS TRUE... USER DOES EXIST (AUTHENTICATED) 
    if ($usrid) {
        $sesh = create_session($usrid);
        update_session($usrid, $sesh);
        setcookie("usersesh", $sesh, 0,"/");
    } else {
        //echo("error msg");
    }
    
    $result->free();
    $mysqli->close();
    function create_session($usrid) {
        $time = microtime();
        $hash = substr($time, 2, 3);
        $key = $usrid * $hash;
        $sesh = md5($key);
        return $sesh;   
    }
    function update_session($usrid, $sesh) {
        $mysqli = new mysqli('localhost', 'root', 'root', 'accounts');
        $query = "UPDATE `users` SET `session` = '" 
            . $mysqli->real_escape_string($sesh) . "' WHERE `id` = '" 
            . $mysqli->real_escape_string($usrid) . "'"; 
        $mysqli->query($query);
        $mysqli->close();   
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)