2012-06-12 36 views
6

目前,devise配置为通过URL接受令牌认证和卷曲效果很好如何配置设计在HTTP标头中接受身份验证令牌?

curl 'http://localhost/index.json?auth_token=TOKENVALUE' 

现在,我想通过通过HTTP标头,而不是URL的TOKENVALUE,我该怎么配置设计获得从TOKENVALUE 要么 HTTP标头 URL?这样,以上的和下面的卷曲请求将工作:如this railscast所示

curl 'http://localhost/index.json' -H 'Authorization: Token token="TOKENVALUE"' 

回答

1

首先添加到您的Gemfile https://github.com/stvp/devise_header_token那么你就可以在你的配置添加配置为它/初始化/ devise.rb

# Configuration for :token_authenticatable 
# Defines name of the authentication token params key 
config.token_authentication_key = 'AUTH-TOKEN' 
1

设计允许通过基本验证身份验证令牌身份验证。如果你看看source你会看到这一点:

页眉,您可以使用基本身份验证传递令牌作为用户名和 空白密码。由于某些客户端可能需要密码,因此您可以将“X”作为密码传递给 ,它将被忽略。

1

事情已经改变,因为这个问题是问,能想出不再有令牌认证功能,内置。它被提取出来一个单独的宝石,devise-token_authenticatable。我正在使用那个宝石,并且想要和提问的人一样。

我想通了,我必须设置这在我的config /初始化/ devise.rb:

config.http_authenticatable = true

我通过卷曲尝试过了,它的工作。在我的RSpec测试中,我能够将令牌放入HTTP头中,如下所示:

user = FactoryGirl.create(:user) 
header = ActionController::HttpAuthentication::Token.encode_credentials(
    user.authentication_token) 
get "/api/v1/your_url", 
    { }, 
    { 
     'Accept' => 'application/json', 
     'Authorization' => header 
    } 

希望这可以帮助那里的人!

相关问题