【问题标题】:Code Igniter - Consuming my own API and authorisationCodeigniter - 使用我自己的 API 和授权
【发布时间】:2014-06-19 14:39:51
【问题描述】:

我正在尝试将我的 HMVC codeigniter 安装构建到以 API 为中心的 HMVC 中(是的,我知道!)

我目前有 ion_auth 作为授权系统。

我的设置方式是

MODELS
CONTROLLERS 
    - API CONTROLLER
    - CONTROLLER
VIEWS

API 控制器接受 JSON 编码的输入并发送 JSON 编码的输出。

现在 - 一切正常 -> 我可以通过调用 controller/api 访问 API,并可以将其传递给 JSON 并接收回 JSON。

然后我只需从我的普通controller 中调用控制器/api

我的问题现在与授权有关。

如果没有通过 Ion_auth 登录,则没有人可以访问 API(因此它是安全的)但是

然后我如何公开 API?

我认为我需要走 O Auth 路线,但我已经陷入困境,试图弄清楚如何将 O Auth 用于 API,并且在通过我的访问时不影响应用程序的性能控制器。

这归结于不了解 O Auth 是如何完全工作的(我可以实现它并理解握手等但细节)-> 如果我找到了通过 O Auth 验证用户的某种方法(我的意思是站点用户不是 API 用户)这如何传递到我的控制器 - 它是否存储在会话中?我可以给我的控制器授权吗? (我需要吗)

或者 - 有没有我没听说过的 Ion Auth 方法?

为了清晰

我希望我自己的应用程序能够使用它自己的 API,但不知道如何设置授权,以便应用程序本身可以直接以及在本地使用 API(当用户使用网站时)

帮助!!!!

提前致谢!

【问题讨论】:

    标签: php api codeigniter authentication


    【解决方案1】:

    也许您可以删除 API 的 O auth 并创建一个访问密钥表。当有人调用 API 时,他需要一个在表中注册的密钥。没有?

    【讨论】:

    • O Auth 只是我的“首选”解决方案 -> 所以您是在为想要使用 API 的人说静态访问密钥?这样做安全吗?我认为 128 位访问密钥将很难破解。那么我是否给我的应用程序一个单一的、永久的访问密钥呢?说是硬连线到系统中的 256 位?
    • 很多这样的API函数。当然,您必须使用高级别的加密,尤其是在您拥有敏感数据的情况下。
    【解决方案2】:

    使用 Promo 的回答,加上在他指出我所做的一切显而易见之后的一点思考:

    public function __construct()
       {
            parent::__construct();
            //see if they logged in -> if they are no problem as they are obviously using the page through my application, if they aren't then we do checks for the API
            if (!$this->authentication->logged_in())
            {
    
                $this->token = $this->input->get_post('token');
                //API should have a token set before anything can happen, this is for every request
                    if($this->token){
                        //Check that the token is valid
                        if (!$this->checkToken($this->token)){
                            $this->error('Token did not match');
                        }
                        //all clear if we get to here - token matched
    
    
                    }else{ //no token was found
                        $this->error('No Token was sent');
                    } 
            }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-09
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多