Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails API token authentication, what is the realm="Application" section of the www-Authenticate header?

I have a rails app with this controller:

class EpisodesController < ApplicationController
  before_action :authenticate

  def index
    episodes = Episode.all
    render json: episodes, status: 200
  end

  protected
    def authenticate
      authenticate_or_request_with_http_token do |token, options|
        User.find_by(auth_token: token)
      end
    end
end

If I send this curl request, I get back this response with these headers:

$ curl -IH "Authorization: Token token=fake" http://localhost:3000/episodes.json
HTTP/1.1 401 Unauthorized 
Content-Type: text/html; charset=utf-8
WWW-Authenticate: Token realm="Application"

What is the www-authenticate header used for? Is it just convention? What is the realm="application" used for? I read this:

The Token part means that the given resource uses token authentication. The resource under that URI is currently part of the “Application” realm. The realm value allows protected resources to be partitioned into different sets of protection spaces, each with its own access policies.

But I don't get it...

like image 486
Jwan622 Avatar asked Sep 28 '15 14:09

Jwan622


People also ask

What is the WWW Authenticate header?

The HTTP WWW-Authenticate response header defines the HTTP authentication methods ("challenges") that might be used to gain access to a specific resource. Note: This header is part of the General HTTP authentication framework, which can be used with a number of authentication schemes.

What is www authenticate basic realm?

The 'Basic' Authentication Scheme. The Basic authentication scheme is based on the model that the client needs to authenticate itself with a user-id and a password for each protection space ("realm"). The realm value is a free-form string that can only be compared for equality with other realms on that server.

How does API authentication work in Rails?

The token-based verification method works simply. The user enters his details and sends the request to the server. If the information is correct, the server creates a unique HMACSHA256 encoded token, also known as the JSON (JWT) web token.

What is www authenticate bearer?

Bearer authentication (also called token authentication) is an HTTP authentication scheme that involves security tokens called bearer tokens.


1 Answers

The WWW-Authenticate Header must be included with 401 Unauthorized responses (see HTTP 1.1 RFC) so it's not only a convention.

With the value you can indicate which authentication mechanism is supported (in this case Token, another auth scheme could be Basic for Basic Authentication). The realm can be set to any value you want and should identify the secure area. In case of Basic Auth this value will be displayed on the login dialog.

like image 114
jayeff Avatar answered Nov 09 '22 19:11

jayeff