Rails JSON API的简单令牌认证签名

时间:2015-01-06 17:55:53

标签: ruby-on-rails json authentication devise http-token-authentication

我有一个Rails应用程序(主要是一个JSON API) 我使用Devise使用来自任何来源(网络,移动设备)的JSON请求进行身份验证 我使用Simple Token Authentication使用HTTP标头验证用户。

我不确定实现应该如何,但我已经起草了一个几乎可行的实现。

只有一个问题。那就是当用户试图退出时...通常它应该使用户的身份验证令牌无效...但它不会,我不确定问题到底在哪里......是否& #39;使用Devise或Simple Token Auth ...所以非常感谢任何帮助。

但这是代码

  # router
  devise_for :users, controllers: { sessions: 'sessions',
                                    registrations: 'registrations' }
  api vendor_string: 'app', default_version: 1, path: '', format: 'json' do
    version 1 do
      cache as: 'v1' do
        resource :some_resource
      end
    end

会话控制器就像这样

class SessionsController < Devise::SessionsController  
  respond_to :json
  skip_filter :verify_signed_out_user, only: :destroy

  def create
    self.resource = warden.authenticate!(scope: resource_name)
    render :create, status: :created
  end

  def destroy
    current_user.authentication_token = nil
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ take note of this line
    current_user.save!
    super
  end
end  

前面提到的行似乎有问题...当用户提供错误的令牌标头时,它仍在工作,当前用户指的是不应该在第一个用户身份验证的用户地方..例如这里有2个来电,规格

describe 'DELETE sessions#destroy' do
    let(:user) { Fabricate(:confirmed_user) }
    let(:auth_token) { user.authentication_token }
    describe 'with request headers' do
      context 'valid credentials' do
        it 'Returns 204' do
          delete '/users/sign_out', {}, {
            HTTP_CONTENT_TYPE: 'application/json',
            HTTP_ACCEPT: "application/vnd.app+json; version=1",
            "X-User-Email" => user.email,
            "X-User-Token" => user.authentication_token
          }

          user.reload
          expect(response.status).to eq 204
          expect(user.authentication_token).not_to eq auth_token
          #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is ok cause it's the valid user
        end
      end

      context 'invalid credentials' do
        it 'Returns 204' do
          delete '/users/sign_out', {}, {
            HTTP_CONTENT_TYPE: 'application/json',
            HTTP_ACCEPT: "application/vnd.app+json; version=1",
            "X-User-Email" => user.email,
            "X-User-Token" => 'Invalid'
          }

          user.reload
          expect(response.status).to eq 204
          expect(user.authentication_token).to eq auth_token
          #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this FAILS
          # why did the user get new auth token when didn't sign out ????
        end
      end
    end

这也在Github

上报告

并且为了完整性,这里是应用程序控制器

class ApplicationController < ActionController::Base
  # The API responds only to JSON
  respond_to :json

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  # default to protect_from_forgery with: :exception
  protect_from_forgery with: :null_session

  # Token Authenticatable for API
  acts_as_token_authentication_handler_for User
end

2 个答案:

答案 0 :(得分:1)

从github上的simple_authentication_token page开始,current_user.authentication_token会在每次保存current_user时自动生成({1}}。

  

假设用户是User的一个实例,它是令牌可验证的:每次用户都会被保存,而user.authentication_token.blank?它接收一个新的唯一身份验证令牌(通过Devise.friendly_token)。

<强>更新

acts_as_token_authentication_handler_for User

中添加sessions_controller.rb

答案 1 :(得分:0)

请阅读https://github.com/gonzalo-bulnes/simple_token_authentication/issues/224。我认为这是正常的行为。您需要删除客户端(设备)上的令牌