duandu5846 2017-08-02 04:48
浏览 43

Codeigniter Restserver - 通过API调用每个函数时的最佳实践[关闭]

I am using Codeigniter restserver for a social networking project. I want to access all my site functions (i-e login, getfriends, postcomments, uploadimages etc) through API. Now I have a question, should I put any type of authentication on API (rest_auth, key etc) or I should keep it open? How the API should be designed?

I believe by using restserver's own authentication settings in config, every function call will require authentication but in my case, I don't need authentication over login or signup functions.

Can I use my own session checks inside functions? or is there better approach as I believe restserver will definitely have something I am missing here.

  • 写回答

1条回答 默认 最新

  • doubo1871 2017-08-02 08:52
    关注

    If you wanna use your own sessions check, you can try the option session in $config['rest_auth'].

    According the docs:

    If you're tying this library into an AJAX endpoint where clients authenticate using PHP sessions then you may not like either of the digest nor basic authentication methods.

    In that case, you can tell the REST Library what PHP session variable to check for. If the variable exists, then the user is authorized. It will be up to your application to set that variable.

    You can define the variable in $config['auth_source']. Then tell the library to use a php session variable by setting $config['rest_auth'] to session.

    Anyway, I wouldn't recommend you this option because, being a Social Network website, in the future you may like to have, for example, a mobile app which will need the access to this API. And there you won't have PHP sessions to check for.

    For what I have read, OAuth2 is the best option to go for to secure our Rest APIs, but I don't know how to implement it yet.

    For now, what I'm doing on my CodeIgniter REST API is to generate API Keys for each user. Those keys will have an expiration date. When the expiration date is reach, API Key is removed from the list and the user should re-authenticate (login) to get a new API Key.

    Hope this helps you a bit. I'm also in this same search and that's the further I have get.

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染