在PHP symfony的2令牌身份验证基础 [英] Token based authentication in php symfony 2

查看:228
本文介绍了在PHP symfony的2令牌身份验证基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个REST API和Web应用程序中的symfony 2.我的REST API将通过移动应用来消费,它不是一个公共的API。我使用基于令牌的身份验证与数据库中存储的令牌。

我的表结构如下所示:

  | ---用户---- | | ---身份--------- |
| PK ID | | PK ID |
|电子邮件| |令牌|
|密码| |设备|
| ------------ | | FK USER_ID |
                   | token_createdDate |
                   | ---------------------- |

我的认证过程包括以下步骤:


  1. 有一个与Android中app.App用户名和密码登录表单发送与用户代理请求头的设备名称来我的PHP API和URL一起登录请求(POST)看起来像这样:/ API / V1 /登录。如果认证成功,我送回来令牌的JSON响应。样品JSON响应如下所示:

{
    令牌:1325asd3434asfFF34d65
          用户:{
                ID:32,
                电子邮件:sample@eg.com
                角色:客户
          }
 }

<醇开始=2>

  • 现在,Android应用程序通过包括请求头中它来访问受保护的API资源使用此令牌。我受保护的资源的URL是这样的:/ API / V1 /打招呼。 PHP API读取令牌和用户代理请求头,然后验证,如果它存在于我的表身份。用户代理请求头映射到设备列在我的表身份。

  • 什么是我在这里的认证体系,为REST API的漏洞?

    更新:
    我碰到FosOAuthBundle但我很困惑,是否可用于我的方案。我的API将不会公开。我的Andr​​oid应用程序将提供登录界面,登录名和密码的形式。是否有可能与FOSOAuthBundle做到这一点?


    解决方案

    不要重新发明轮子,你可以建立现代宁静的API,使用带的 FOSRestBundle FOSOAuthServerBundle 并的 FOSUserBundle 包。

    I am building a rest api and web app in symfony 2. My rest api will be consumed by mobile application and it is not a public api. I am using token based authentication with tokens stored in a database.

    My table schema is shown below:

    |---users----|     |---identities---------|
    |pk id       |     |pk  id                |
    |   email    |     |    token             |
    |   password |     |    device            |
    |------------|     |fk  user_id           |
                       |    token_createdDate |
                       |----------------------|
    

    My authentication process involves following steps:

    1. There is a login form with username and password in android app.App sends login request(POST) along with the device name in user-agent request header to My php api and url looks like this:/api/v1/login. If authentication is successful, I send back a json response with token. sample json response is shown below:

    {
        "token":"1325asd3434asfFF34d65",
              "user" :{
                    "id":32,
                    "email":"sample@eg.com",
                    "role" :"customer" 
              }
     }
    

    1. Now android app uses this token by including it within request header to access secured api resource. My protected resource url is like this: /api/v1/hello. php api reads the token and user-agent request headers and then validates if it exists in my table "identities". user-agent request header maps to device column in my table "identities".

    What are the vulnerabilities of my authentication system here for the rest api ?

    update: I came across FosOAuthBundle but I am confused whether it can be used for my scenario. My api will not be public. My android app will provide login interface with login and password form. Is it possible to do this with FOSOAuthBundle ?

    解决方案

    Don't reinvent the wheel you can build modern restful api that using OAuth2 authentication with FOSRestBundle, FOSOAuthServerBundle and FOSUserBundle bundles.

    这篇关于在PHP symfony的2令牌身份验证基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆