0

私の Rails 4 アプリでは、コントローラーのindexcreateshowupdate、およびdestroyメソッドにアクセスできる API を作成しています。

認証には、ユーザー名とパスワード (特定のメソッドへのアクセスにはaccess_key = usernamesecret_key = password、および) の代わりに資格情報を含む HTTP Basic を使用しています。master_secret_key = password

/ペアで , ,メソッドのみにアクセスし、 access_key/ペアでandメソッド (およびその他のメソッド)へのアクセスを許可したいと考えています。secret_keycreateupdatedestroyaccess_keymaster_secret_keyindexshow

認証は機能しますが、リクエスト メソッドに基づいて認証する方法を Rails に伝えるのに問題があります。

助言がありますか?

これが私がこれまでに持っているコードです:

class Api::V1::TestsController < ApplicationController
  before_filter { :authenticate! || :authenticate_with_master! }
  skip_before_filter :verify_authenticity_token
  respond_to :json

  def index
  end

  def create
  end

  def show
  end

  def update
  end

  def destroy
  end

  protected

  def authenticate!
    authenticate_or_request_with_http_basic do |access_key, secret_key|
      @credential = Credentials.find_by_access_key(access_key)
      (@credential.secret_key == secret_key) ? true : false
    end
  end

  def authenticate_with_master!
    authenticate_or_request_with_http_basic do |access_key, master_secret_key|
      @credential = Credentials.find_by_access_key(access_key)
      (@user.master_secret_key == master_secret_key) ? true : false
    end
  end
end
4

1 に答える 1

2

before_filter の「のみ」のパラメーターを試して、Rails 4 で before_action を使用する

before_action :authenticate!, only: [:create, :update, :destroy]

before_action :authenticate_with_master! , only: [:index, :show]

それが役立つことを願っています。ありがとう

于 2013-07-23T15:13:59.220 に答える