Class: RRBA::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rrba/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



12
13
14
# File 'lib/rrba/server.rb', line 12

def initialize
	@users = []
end

Instance Attribute Details

#anonymous=(value) ⇒ Object (writeonly)

Sets the attribute anonymous

Parameters:

  • value

    the value to set the attribute anonymous to.



11
12
13
# File 'lib/rrba/server.rb', line 11

def anonymous=(value)
  @anonymous = value
end

#root=(value) ⇒ Object (writeonly)

Sets the attribute root

Parameters:

  • value

    the value to set the attribute root to.



11
12
13
# File 'lib/rrba/server.rb', line 11

def root=(value)
  @root = value
end

Instance Method Details

#add_user(user) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rrba/server.rb', line 15

def add_user(user)
	id = user.unique_id
	if(@users.any? { |usr| usr.unique_id == id })
		raise "Duplicate ID #{id}"
	end
	@users.push(user).last
end

#authenticate(id = nil, &block) ⇒ Object



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

def authenticate(id=nil, &block)
	challenge = Digest::MD5.hexdigest(rand(2**32).to_s)[0,20]
	signature = block.call(challenge)
	if(@anonymous && signature == :anonymous)
		return @anonymous.new_session
	end
	if(@root && @root.authenticate(challenge, signature))
		return @root.new_session
	end
	begin
		if(id)
			if((user = user(id)) \
				&& user.authenticate(challenge, signature))
				return user.new_session
			end
		else
			@users.each { |user|
				if(user.authenticate(challenge, signature))
					return user.new_session
				end
			}
		end
	rescue RuntimeError
	end
	raise AuthenticationError, 'Authentication failed'
end

#unique_idsObject



53
54
55
56
57
# File 'lib/rrba/server.rb', line 53

def unique_ids
	@users.collect { |user|
		user.unique_id
	}
end

#user(id) ⇒ Object



48
49
50
51
52
# File 'lib/rrba/server.rb', line 48

def user(id)
	@users.select { |user|  
		user.unique_id == id 
	}.first or raise "Unknown User: #{id}"
end