Class: RRBA::Server
- Inherits:
-
Object
- Object
- RRBA::Server
- Defined in:
- lib/rrba/server.rb
Instance Attribute Summary collapse
-
#anonymous ⇒ Object
writeonly
Sets the attribute anonymous.
-
#root ⇒ Object
writeonly
Sets the attribute root.
Instance Method Summary collapse
- #add_user(user) ⇒ Object
- #authenticate(id = nil, &block) ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #unique_ids ⇒ Object
- #user(id) ⇒ Object
Constructor Details
#initialize ⇒ Server
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
11 12 13 |
# File 'lib/rrba/server.rb', line 11 def anonymous=(value) @anonymous = value end |
#root=(value) ⇒ Object (writeonly)
Sets the attribute root
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_ids ⇒ Object
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 |