Class: Liveness::Access
- Inherits:
-
Object
- Object
- Liveness::Access
- Defined in:
- lib/liveness/access.rb
Overview
Access Control
Constant Summary collapse
- LOCAL_IPV4 =
IPAddr.new('127.0.0.1')
- LOCAL_IPV6 =
IPAddr.new('::1')
Instance Method Summary collapse
-
#allowed? ⇒ Boolean
Is allowed to access.
-
#initialize(request, config:) ⇒ Access
constructor
A new instance of Access.
-
#local? ⇒ Boolean
Is from localhost.
-
#valid_token? ⇒ Boolean
Is token valid.
-
#whitelist? ⇒ Boolean
Is ip in whitelist.
Constructor Details
#initialize(request, config:) ⇒ Access
Returns a new instance of Access.
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
28 29 30 |
# File 'lib/liveness/access.rb', line 28 def allowed? local? || (whitelist? && valid_token?) end |
#local? ⇒ Boolean
Is from localhost
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
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
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 |