使用Restler对不同类进行不同的身份验证

时间:2012-02-09 00:19:30

标签: php api authentication rest

我正在尝试构建一个REST API(使用Restler),它使用用户名和密码进行登录并生成会话密钥。生成sessionkey后,用户将能够传递此会话密钥以访问api中的其他类。是否可以获取调用__isAuthenticated函数的类的名称?

我的认证课程:

<?php
class Auth implements iAuthenticate
{
    public static $sessionKey;
    public static $currentUser;
    public $tempsesskey;

    function __isAuthenticated ()
    {
        if (isset($_GET['useremail']) && isset($_GET['userpass'])) {
            $user = $_GET['useremail'];
            $pass = $_GET['userpass'];
            $user = mysql_real_escape_string($user);
            $pass = mysql_real_escape_string($pass);
            mysql_query(
            "UPDATE `userdetail` SET lastlogin=NOW()
            WHERE useremail='$user' AND userpass=md5('$pass')");
            if (mysql_affected_rows() > 0) {
                $result = mysql_query(
                "SELECT sessionkey from usersession where TIMESTAMPDIFF(MINUTE,lastactivity,now()) < 20 and useremail='$user'");
                while ($row = mysql_fetch_assoc($result)) {
                    $tempsesskey = $row['sessionkey'];
                }
                if (strlen($tempsesskey) > 0) {
                    mysql_query(
                    "UPDATE usersession set lastactivity=now() where sessionkey='$tempsesskey'");
                } else {
                    $tempsesskey = generateKey(52);
                    mysql_query(
                    "UPDATE `usersession` set sessionkey='$tempsesskey',keyvalid='Y' where useremail='$user'");
                }
                self::$currentUser = $user;
                self::$sessionKey = $tempsesskey;
                return TRUE;
            }
        } else 
            if (isset($_GET['sessionkey'])) {
                $sesskey = $_GET['sessionkey'];
                $sesskey = mysql_real_escape_string($sesskey);
                $result = mysql_query(
                "SELECT sessionkey from usersession where sessionkey='$sesskey' and TIMESTAMPDIFF(MINUTE,lastactivity,now()) < 20");
                if (mysql_affected_rows() > 0) {
                    while ($row = mysql_fetch_assoc($result)) {
                        $tempsesskey = $row['sessionkey'];
                        self::$sessionKey = $tempsesskey;
                    }
                    return TRUE;
                }
            }
    }
}

1 个答案:

答案 0 :(得分:1)

有一种简单的方法可以通过添加自定义php doc注释/注释在Authentication类上设置属性,这在Authentication with ACL.中有解释。您也可以使用相同的技术