gregbeech/xenon

View on GitHub
xenon-routing/lib/xenon/routing/security_directives.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'xenon/routing/route_directives'

module Xenon
  module Routing
    module SecurityDirectives
      include RouteDirectives

      def authenticate(authenticator)
        extract_request(authenticator) do |user|
          if user
            yield user
          else
            reject :unauthorized, { scheme: authenticator.scheme }.merge(authenticator.auth_params)
          end
        end
      end

      def optional_authenticate(authenticator)
        extract_request(authenticator) do |user|
          yield user
        end
      end

      def authorize(check)
        if check.respond_to?(:call)
          extract_request(check) do |authorized|
            authorize(authorized) do
              yield
            end
          end
        elsif check
          yield
        else
          reject :forbidden
        end
      end

    end
  end
end