Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Verifying that authenticate is called. #55

Closed
maxcal opened this issue Apr 16, 2016 · 1 comment
Closed

Verifying that authenticate is called. #55

maxcal opened this issue Apr 16, 2016 · 1 comment

Comments

@maxcal
Copy link

maxcal commented Apr 16, 2016

It would be nice if there was built in support for verifying that authentication is enforced to lock down an application much like verify_authorized in Pundit.

Comments? Is this something you would like to see in a PR?

I'm thinking of something like:

module Knock::Authenticable
  def current_user
    @current_user ||= begin
      token = params[:token] || request.headers['Authorization'].split.last
      Knock::AuthToken.new(token: token).current_user
    rescue
      nil
    end
  end

  def authenticate
    @_authentication_performed = true
    head :unauthorized unless current_user
  end

  def authentication_performed?
    !!@_authentication_performed
  end

  def verify_authentication
    raise Knock::AuthenticationNotPerformedError unless authentication_performed?
  end
end

Usage:

class ApplicationController < ActionController::API
  include Knock::Authenticable 
  after_action :verify_authentication, except: [:show, :index]
end
@nsarno
Copy link
Owner

nsarno commented Apr 22, 2016

I'm not sure I understand the benefit of this in the context of authentication. Why isn't the call to before_action :authenticate at the start of a controller explicit enough? I feel like this is something you should test, not double check everytime at runtime. Let me know if I'm missing something.

@nsarno nsarno closed this as completed May 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants