Class: Liveness::Access

Inherits:
Object
  • Object
show all
Defined in:
lib/liveness/access.rb

Overview

Access Control

Since:

  • 0.3.0

Constant Summary collapse

LOCAL_IPV4 =

Since:

  • 0.3.2

IPAddr.new('127.0.0.1')
LOCAL_IPV6 =

Since:

  • 0.3.0

IPAddr.new('::1')

Instance Method Summary collapse

Constructor Details

#initialize(request, config:) ⇒ Access

Returns a new instance of Access.

Parameters:

Since:

  • 0.3.0



18
19
20
21
# File 'lib/liveness/access.rb', line 18

def initialize(request, config:)
  @request = request
  @config = config
end

Instance Method Details

#allowed?Boolean

Is allowed to access

Returns:

  • (Boolean)

Since:

  • 0.3.0



28
29
30
# File 'lib/liveness/access.rb', line 28

def allowed?
  local? || (whitelist? && valid_token?)
end

#local?Boolean

Is from localhost

Returns:

  • (Boolean)

Since:

  • 0.3.2



48
49
50
# File 'lib/liveness/access.rb', line 48

def local?
  LOCAL_IPV4.include?(@request.ip) || LOCAL_IPV6.include?(@request.ip)
end

#valid_token?Boolean

Is token valid

Returns:

  • (Boolean)

Since:

  • 0.3.0



37
38
39
40
41
# File 'lib/liveness/access.rb', line 37

def valid_token?
  return true if @config.token.nil?

  @config.token == @request.params['token']
end

#whitelist?Boolean

Is ip in whitelist

Returns:

  • (Boolean)

Since:

  • 0.3.0



57
58
59
60
61
62
63
64
# File 'lib/liveness/access.rb', line 57

def whitelist?
  return true if @config.ip_whitelist.empty?

  @config
    .ip_whitelist
    .map { |ip| IPAddr.new(ip) }
    .reduce(true) { |curr, addr| curr & addr.include?(@request.ip) }
end