私の Rails 4 アプリでは、コントローラーのindex
、create
、show
、update
、およびdestroy
メソッドにアクセスできる API を作成しています。
認証には、ユーザー名とパスワード (特定のメソッドへのアクセスにはaccess_key = username
、secret_key = password
、および) の代わりに資格情報を含む HTTP Basic を使用しています。master_secret_key = password
/ペアで , ,メソッドのみにアクセスし、 access_key
/ペアでandメソッド (およびその他のメソッド)へのアクセスを許可したいと考えています。secret_key
create
update
destroy
access_key
master_secret_key
index
show
認証は機能しますが、リクエスト メソッドに基づいて認証する方法を 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