class Yus::Server

Public Class Methods

new(persistence, config, logger) click to toggle source
# File lib/yus/server.rb, line 12
def initialize(persistence, config, logger)
  @needle = Needle::Registry.new
  @needle.register(:persistence) { persistence }
  @needle.register(:config) { config }
  @needle.register(:logger) { logger }
  @sessions = []
  run_cleaner
end

Public Instance Methods

autosession(domain, &block) click to toggle source
# File lib/yus/server.rb, line 20
def autosession(domain, &block)
  session = AutoSession.new(@needle, domain)
  block.call(session)
end
login(name, password, domain) click to toggle source
# File lib/yus/server.rb, line 24
def login(name, password, domain)
  @needle.logger.info(self.class) { 
    sprintf('Login attempt for %s from %s', name, domain)
  }
  hash = @needle.config.digest.hexdigest(password.to_s)
  session = login_root(name, hash, domain) \
    || login_entity(name, hash, domain) # raises YusError
  @sessions.push(session)
  session
end
login_entity(*args) click to toggle source
# File lib/yus/persistence/odba.rb, line 66
def login_entity(*args)
  if(entity = odba_login_entity(*args))
    ODBA::DRbWrapper.new(entity)
  end
end
Also aliased as: odba_login_entity
login_root(*args) click to toggle source
# File lib/yus/persistence/odba.rb, line 71
def login_root(*args)
  if(root = odba_login_root(*args))
    ODBA::DRbWrapper.new(root)
  end
end
Also aliased as: odba_login_root
login_token(name, token, domain) click to toggle source
# File lib/yus/server.rb, line 34
def login_token(name, token, domain)
  entity = authenticate_token(name, token)
  entity.login(domain)
  @needle.persistence.save_entity(entity)
  timeout = entity.get_preference("session_timeout", domain) \
    || @needle.config.session_timeout
  TokenSession.new(@needle, entity, domain)
end
logout(session) click to toggle source
# File lib/yus/server.rb, line 42
def logout(session)
  @needle.logger.info(self.class) { 
    sprintf('Logout for %s', session)
  }
  @sessions.delete(session)
  if(session.respond_to?(:destroy!))
    session.destroy! 
  end
end
odba_login_entity(*args)
Alias for: login_entity
odba_login_root(*args)
Alias for: login_root
ping() click to toggle source
# File lib/yus/server.rb, line 51
def ping
  true
end

Private Instance Methods

authenticate(name, passhash) click to toggle source
# File lib/yus/server.rb, line 55
def authenticate(name, passhash)
  user = @needle.persistence.find_entity(name) \
    or raise UnknownEntityError, "Unknown Entity '#{name}'"
  user.authenticate(passhash) \
    or raise AuthenticationError, "Wrong password"
  @needle.logger.info(self.class) { 
    sprintf('Authentication succeeded for %s', name)
  }
  user
rescue YusError
  @needle.logger.warn(self.class) { 
    sprintf('Authentication failed for %s', name)
  }
  raise
end
authenticate_token(name, token) click to toggle source
# File lib/yus/server.rb, line 70
def authenticate_token(name, token)
  user = @needle.persistence.find_entity(name) \
    or raise UnknownEntityError, "Unknown Entity '#{name}'"
  user.authenticate_token(token) \
    or raise AuthenticationError, "Wrong token or token expired"
  @needle.logger.info(self.class) { 
    sprintf('Token-Authentication succeeded for %s', name)
  }
  user
rescue YusError
  @needle.persistence.save_entity(user) if user
  @needle.logger.warn(self.class) { 
    sprintf('Token-Authentication failed for %s', name)
  }
  raise
end
clean() click to toggle source
# File lib/yus/server.rb, line 86
def clean
  @sessions.delete_if { |session| session.expired? }
end
run_cleaner() click to toggle source
# File lib/yus/server.rb, line 106
def run_cleaner
  @cleaner = Thread.new {
    loop {
      sleep(@needle.config.cleaner_interval)
      clean 
    }
  }
end