Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to config devise to accept auth token in HTTP header?

Currently, devise is configured to accept token authentication via URL and curl works well

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

Now I'd like to pass the TOKENVALUE via HTTP header instead of URL, how can I config devise to get the TOKENVALUE from either HTTP header or URL? Such that both the above and following curl requests will work:

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

as shown in this railscast.

like image 934
ohho Avatar asked Jun 12 '12 11:06

ohho


People also ask

How do I add auth token in header?

The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value. For added security, store it in a variable and reference the variable by name.

How do I pass the Authorization header in GET request?

To send a GET request with a Bearer Token authorization header, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.

What is token in header?

The bearer token is a cryptic string, usually generated by the server in response to a login request. The client must send this token in the Authorization header when making requests to protected resources: Authorization: Bearer <token>


2 Answers

It seems there isn't such config in devise. But there is a solution by other person. Please see Using auth_token from request headers instead from POST/PUT parameters with Rails 3 / devise

like image 63
guanxiaohua2k6 Avatar answered Oct 09 '22 04:10

guanxiaohua2k6


First add this to your gemfile https://github.com/stvp/devise_header_token then you can add a configuration for it in your config/initializers/devise.rb

# Configuration for :token_authenticatable
# Defines name of the authentication token params key
config.token_authentication_key = 'AUTH-TOKEN'
like image 25
Lin Qiu Avatar answered Oct 09 '22 05:10

Lin Qiu