Simple Token Authentication Signout for Rails JSON API



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

我不确定实现应该是什么样子,但我起草了一个几乎有效的实现。

只有一个问题。 那就是当用户尝试注销时...通常,它应该使用户的身份验证令牌无效...但它没有,我不确定问题到底在哪里......无论是使用设计还是简单令牌身份验证...因此,非常感谢任何帮助。

但这是代码

  # 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

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

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

更新

sessions_controller.rb中添加acts_as_token_authentication_handler_for User

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

最新更新