3

我有一个 Rails 应用程序(主要是一个 JSON API)我正在使用Devise使用来自任何来源(web、移动)的 JSON 请求进行身份验证,并且我正在使用简单令牌身份验证来使用 HTTP 标头对用户进行身份验证。

我不确定实现应该是什么样子,但我已经起草了一个几乎可以工作的实现。

只有一个问题。那就是当用户尝试注销时...通常它应该使用户的身份验证令牌无效...但事实并非如此,我不确定问题出在哪里...无论是与设计还是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
4

2 回答 2

1

从github 上的simple_authentication_token页面current_user.authentication_token,如果每次保存 current_user 时它是空白(nil),将自动生成。

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

更新

添加acts_as_token_authentication_handler_for User您的sessions_controller.rb

于 2015-01-07T02:58:13.213 回答
0

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

于 2017-11-03T17:29:43.743 回答