Class: Rubius::Authenticator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rubius/authenticator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthenticator

Returns a new instance of Authenticator.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubius/authenticator.rb', line 9

def initialize
  @dictionary = Rubius::Dictionary.new
  @packet = nil
  @secret = nil
  
  @host = nil
  @port ||= Socket.getservbyname("radius", "udp")
  @port ||= 1812
  
  @timeout = 10
  
  @sock = nil
  
  @identifier = Process.pid & 0xff
end

Class Method Details

.authenticate(username, password) ⇒ Object



67
68
69
# File 'lib/rubius/authenticator.rb', line 67

def self.authenticate(username, password)
  Rubius::Authenticator.instance.authenticate(username, password)
end

Instance Method Details

#authenticate(username, password) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubius/authenticator.rb', line 50

def authenticate(username, password)
  init_packet
  
  @packet.code = Rubius::Packet::ACCESS_REQUEST
  @packet.secret = @secret
  rand_authenticator
  
  @packet.set_attribute('User-Name', username)
  @packet.set_attribute('NAS-IP-Address', @nas_ip)
  @packet.set_password(password)
  
  send_packet
  recv_packet
  
  return(@packet.code == Rubius::Packet::ACCESS_ACCEPT)
end

#init_from_config(config_file, env = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubius/authenticator.rb', line 25

def init_from_config(config_file, env=nil)
  if env.nil?
    env = defined?(::Rails) ? ::Rails.env : 'development'
  end
  
  config = YAML.load_file(config_file)
  raise Rubius::MissingEnvironmentConfiguration unless config.has_key?(env)
  
  @host = config[env]["host"]
  @port = config[env]["port"] if config[env]["port"]
  @secret = config[env]["secret"]
  
  if config[env]["dictionary"]
    dict = File.join(File.dirname(config_file), config[env]["dictionary"])
    @dictionary.load(dict) if File.exists?(dict)
  end
  
  @nas_ip = config[env]["nas_ip"] if config[env]["nas_ip"]
  @nas_ip ||= UDPSocket.open {|s| s.connect(@host, 1); s.addr.last }
  
  setup_connection
rescue Errno::ENOENT
  raise Rubius::MissingConfiguration
end