Sha256: 415c5ccf824eb5494b7341f9d5046e9ac52a39aaac00aa294e075dee906e94be

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

module Trestle
  module Auth
    # Trestle::Auth::Constraint is a Rails routing constraint that can be used to protect
    # segments of your application that aren't regular Trestle admins or resources
    # (all of which are automatically protected).
    #
    # Note that when using a Rails routing constraint like this one, any unauthenticated
    # requests will return a 404 (Not Found) rather than a redirect to the login page.
    #
    # Examples
    #
    #   mount Sidekiq::Web => "/sidekiq/web", constraints: Trestle::Auth::Constraint.new
    #
    #   constraints Trestle::Auth::Constraint.new do
    #     get "/custom/action", to 'custom#action'
    #   end
    #
    class Constraint
      def matches?(request)
        backend = authentication_backend_for(request)
        backend.authenticate
        backend.logged_in?
      end

    private
      def authentication_backend_for(request)
        Trestle.config.auth.backend.new(controller: self, request: request, session: request.session, cookies: request.cookie_jar)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trestle-auth-0.5.0 lib/trestle/auth/constraint.rb
trestle-auth-0.5.0.pre2 lib/trestle/auth/constraint.rb
trestle-auth-0.5.0.pre lib/trestle/auth/constraint.rb
trestle-auth-0.4.4 lib/trestle/auth/constraint.rb
trestle-auth-0.4.3 lib/trestle/auth/constraint.rb