Class: LittleGhost::Network::Decision

Inherits:
Object
  • Object
show all
Defined in:
lib/little_ghost/network.rb

Overview

Trusted authorization result and tightly scoped upstream header changes.

Constant Summary collapse

HEADER_NAME =

:nodoc:

/\A[a-z0-9!#$%&'*+.^_`|~-]+\z/
FORBIDDEN_MUTATIONS =

:nodoc:

%w[host connection content-length transfer-encoding upgrade proxy-connection].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allowed:, status:, reason: nil, set_headers: {}, remove_headers: []) ⇒ Decision

Builds a normalized trusted authorization decision.



39
40
41
42
43
44
45
46
47
# File 'lib/little_ghost/network.rb', line 39

def initialize(allowed:, status:, reason: nil, set_headers: {}, remove_headers: [])
  super(
    allowed: !!allowed,
    status: Integer(status),
    reason: reason&.to_s&.freeze,
    set_headers: normalize_headers(set_headers),
    remove_headers: Array(remove_headers).map { |name| normalize_header_name(name) }.uniq.freeze
  )
end

Class Method Details

.allow(set_headers: {}, remove_headers: []) ⇒ Object

Allows a request and optionally changes its upstream headers.



29
30
31
# File 'lib/little_ghost/network.rb', line 29

def self.allow(set_headers: {}, remove_headers: [])
  new(allowed: true, status: 200, reason: nil, set_headers:, remove_headers:)
end

.deny(status: 403, reason: nil) ⇒ Object

Rejects a request with an HTTP status and optional safe reason.



34
35
36
# File 'lib/little_ghost/network.rb', line 34

def self.deny(status: 403, reason: nil)
  new(allowed: false, status:, reason:, set_headers: {}, remove_headers: [])
end