Sha256: 720d3fd43ef2a754a1cf74a2eb96288d441d909f50d90648f71838b09cb4fb1e

Contents?: true

Size: 1.93 KB

Versions: 23

Compression:

Stored size: 1.93 KB

Contents

require "pact_broker/api/authorization/resource_access_rules"
require "pact_broker/api/paths"

module PactBroker
  module Api
    module Authorization
      class ResourceAccessPolicy
        include PactBroker::Api::Paths

        READ_METHODS                = %w{GET OPTIONS HEAD}.freeze
        ALL_METHODS                 = %w{GET POST PUT PATCH DELETE HEAD OPTIONS}.freeze
        POST                        = "POST".freeze
        ALL_PATHS                   = %r{.*}.freeze
        HEARTBEAT_PATH              = %r{^/diagnostic/status/heartbeat$}.freeze
        PACTS_FOR_VERIFICATION_PATH = %r{^/pacts/provider/[^/]+/for-verification$}.freeze

        PUBLIC = 0
        READ = 1
        WRITE = 2

        def initialize(resource_access_rules)
          @resource_access_rules = resource_access_rules
        end

        def public_access_allowed?(env)
          resource_access_rules.access_allowed?(env, PUBLIC)
        end

        def read_access_allowed?(env)
          resource_access_rules.access_allowed?(env, READ)
        end

        def self.build(allow_public_read_access, allow_public_access_to_heartbeat, enable_public_badge_access)
          rules = [
            [WRITE, ALL_METHODS, ALL_PATHS],
            [READ, READ_METHODS, ALL_PATHS],
            [READ, [POST], PACTS_FOR_VERIFICATION_PATH],
          ]

          if enable_public_badge_access
            rules.concat(BADGE_PATHS.collect { | badge_path | [PUBLIC, READ_METHODS, badge_path] })
          end

          if allow_public_access_to_heartbeat
            rules.unshift([PUBLIC, READ_METHODS, HEARTBEAT_PATH])
          end

          if allow_public_read_access
            rules.unshift([PUBLIC, READ_METHODS, ALL_PATHS])
            rules.unshift([PUBLIC, [POST], PACTS_FOR_VERIFICATION_PATH])
          end

          ResourceAccessPolicy.new(ResourceAccessRules.new(rules))
        end

        private

        attr_reader :resource_access_rules
      end

    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
pact_broker-2.112.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.111.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.109.1 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.109.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.108.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.107.1 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.107.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.107.0.beta.1 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.106.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.105.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.104.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.103.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.102.2 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.102.1 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.102.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.101.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.100.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.99.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.98.0 lib/pact_broker/api/authorization/resource_access_policy.rb
pact_broker-2.97.0 lib/pact_broker/api/authorization/resource_access_policy.rb