Class: Certmeister::Policy::IP

Inherits:
Object
  • Object
show all
Defined in:
lib/certmeister/policy/ip.rb

Instance Method Summary collapse

Constructor Details

#initialize(networks) ⇒ IP

Returns a new instance of IP.



10
11
12
# File 'lib/certmeister/policy/ip.rb', line 10

def initialize(networks)
  @networks = networks.map { |n| IPAddr.new(n) }
end

Instance Method Details

#authenticate(request) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/certmeister/policy/ip.rb', line 14

def authenticate(request)
  begin
    if !request[:ip]
      Certmeister::Policy::Response.new(false, "missing ip")
    else
      ip = IPAddr.new(request[:ip])
      if @networks.any? { |n| n.include?(ip) }
        Certmeister::Policy::Response.new(true, nil)
      else
        Certmeister::Policy::Response.new(false, "unauthorized ip")
      end
    end
  rescue IPAddr::Error
    Certmeister::Policy::Response.new(false, "invalid ip")
  end
end