【问题标题】:Luracast Restler AuthenticationLuracast Restler 身份验证
【发布时间】:2011-12-15 14:44:02
【问题描述】:

我正在使用 Luracast restler,我正在尝试通过实现 iAuthenticate 接口来实现一些身份验证。

问题是,我的身份验证代码需要查询我的数据库以检索用户私钥。此私钥将始终在 url 请求中提供(散列)。

我只想为每个请求打开一个数据库连接,因此我需要将 db 连接变量传递给实现 iAuthenticate 的类以及处理所有请求的其他类。但我不知道如何将变量传递给实现 iAuthenticate 的类。

有可能吗?

供参考,这里是the luracast examples

提前考虑。

【问题讨论】:

    标签: php api restful-authentication


    【解决方案1】:

    为您的 API 和身份验证类使用单一数据库连接

    创建一个名为 config.php 的 php 文件,并将所有 db 信息连同 db 连接和选择一起放置。

    例如

    <?php
    define('DB_SERVER', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'password');
    define('DB_NAME', 'mysql_db');
    //initalize connection to use everywhere
    //including auth class and api classes
    mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_NAME);
    

    在 Authentication 类和 API 类中使用 require_once 包含此函数,类似于(为简单起见,我没有在此处加密密码)

    <?php
    require_once 'config.php';
    class BasicAuthentication implements iAuthenticate{
        const REALM = 'Restricted API';
        public static $currentUser;
    
        function __isAuthenticated(){
            if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){
                $user = $_SERVER['PHP_AUTH_USER'];
                $pass = $_SERVER['PHP_AUTH_PW'];
                $user = mysql_real_escape_string($user);
                $pass = mysql_real_escape_string($pass);
    
                mysql_query("UPDATE `login` SET logged=NOW()
                    WHERE user='$user' AND pass='$pass'");
                // echo mysql_affected_rows();
                if(mysql_affected_rows()>0){
                    self::$currentUser = $user;
                    return TRUE;
                }
            }
            header('WWW-Authenticate: Basic realm="'.self::REALM.'"');
            throw new RestException(401, 'Basic Authentication Required');
        }
    }
    

    您的 API 类可以有一个受保护的方法来查询相同的数据库,它可以是使用相同连接返回数据的不同表。为简单起见,我在这里使用同一张表。

    <?php
    require_once 'config.php';
    class Simple {
        function index() {
            return 'public api result';
        }
        protected function restricted() {
            $query = mysql_query("SELECT * FROM login");
            $result = array();
            while ($row = mysql_fetch_assoc($query)) {
                $result[]=$row;
            }
            return $result;
        }
    }
    

    使用require_once 确保php 文件在第一次遇到时只包含一次。即使我们稍后停止使用 auth 类,我们的 api 也会继续运行

    假设使用下面的 SQL 来创建我们的 db 表

    --
    -- Database: `mysql_db`
    --
    
    --
    -- Table structure for table `login`
    --
    
    CREATE TABLE IF NOT EXISTS `login` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `logged` datetime DEFAULT NULL,
      `user` varchar(10) DEFAULT NULL,
      `pass` varchar(10) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
    
    --
    -- Dumping data for table `login`
    --
    
    INSERT INTO `login` (`id`, `logged`, `user`, `pass`) VALUES
    (1, '2011-11-01 22:50:05', 'arul', 'mypass'),
    (2, '2011-11-01 23:43:25', 'paulo', 'hispass');
    

    还有 index.php 如下

    <?php
    require_once '../../restler/restler.php';
    
    #set autoloader
    #do not use spl_autoload_register with out parameter
    #it will disable the autoloading of formats
    spl_autoload_register('spl_autoload');
    
    $r = new Restler();
    
    $r->addAPIClass('Simple','');
    $r->addAuthenticationClass('BasicAuthentication');
    $r->handle();
    

    结果

    如果您在浏览器中打开index.php/restricted 并输入正确的用户名和密码组合,您将看到以下结果:)

    [
      {
        "id": "1",
        "logged": "2011-11-01 22:50:05",
        "user": "arul",
        "pass": "mypass"
      },
      {
        "id": "2",
        "logged": "2011-11-01 23:43:25",
        "user": "paulo",
        "pass": "hispass"
      }
    ]
    

    【讨论】:

    • 嘿,这太酷了!不过还有一个问题...您如何将上述方法扩展到 AWS 之类的身份验证 (docs.amazonwebservices.com/AmazonCloudFront/latest/…),其中包含一个 Date 标头(例如“Date: Thu, 14 Aug 2008 17:08:48 GMT”)身份验证信息根据哪个散列?
    【解决方案2】:

    想通了!

    echo mysql_affected_rows();
    

    这一行导致输出为 text/html 格式。将其评论出来,我很高兴。

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      相关资源
      最近更新 更多