Class: Aurora::Server

Inherits:
Halcyon::Server::Base
  • Object
show all
Defined in:
lib/aurora/server.rb

Overview

Aurora Server

The Aurora Server handles user authentication requests, token creation and management, and permissions querying and verification.

Usage

class Aurora::Server
  def authenticate(username, password)
    username == 'test' && password == 'secret'
  end
end

This will define an authentication processor for verifying users’ authenticity. This method is only defaulted to when token authentication is not able to be used (such as for creating sessions), so its use should be minimized by token authentication.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.versionObject



41
42
43
# File 'lib/aurora/server.rb', line 41

def self.version
  VERSION.join('.')
end

Instance Method Details

#expire_tokensObject

Removes expired tokens.



253
254
255
# File 'lib/aurora/server.rb', line 253

def expire_tokens
  @db[:tokens].filter('expires_at < ?',Time.now).delete
end

#generate_expiration(lifetime = @config[:tokens][:lifetime]) ⇒ Object

Generates a new time to expire from the minutes given, defaulting to the number of minutes given as a token lifetime in the configuration file.



268
269
270
# File 'lib/aurora/server.rb', line 268

def generate_expiration(lifetime=@config[:tokens][:lifetime])
	(Time.now + (lifetime.to_i*60))
end

#initialize_permissions(username) ⇒ Object

Sets up a given user’s permissions. Overwrite this method to specify more specific or dynamic default permissions, for instance connecting to LDAP to determine department and granting permissions that way.



260
261
262
263
264
# File 'lib/aurora/server.rb', line 260

def initialize_permissions(username)
	# by default, no permissions are setup
	# the returned value is JSON-ized
	{}
end

#startupObject

Makes sure the Database server connection is created, tables migrated, and other tasks.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/aurora/server.rb', line 72

def startup
  # TODO: setup startup tasks
  
  # connect to database
  host = credentials = ''
  host = "#{@config[:db][:host]}/" unless @config[:db][:host].nil?
  credentials = "#{@config[:db][:username]}:#{@config[:db][:password]}@" unless @config[:db][:username].nil?
  @db = Sequel("#{@config[:db][:adapter]}://#{credentials}#{host}#{@config[:db][:database]}")
  @logger.info 'Connected to Database.'
  
  # run migrations if version is outdated
  current_version = Sequel::Migrator.get_current_migration_version(@db)
  latest_version = Sequel::Migrator.apply(@db, File.join(File.dirname(__FILE__),'migrations'))
  @logger.info 'Migrations loaded!' if current_version < latest_version
  
  # clean expired sessions/tokens
  expire_tokens
  @logger.info 'Expired sessions/tokens removed.'
end

#unauthorized(params = {}) ⇒ Object

The default unauthorized action which raises an Unauthorized exception

Raises:

  • (Exceptions::Unauthorized)


244
245
246
# File 'lib/aurora/server.rb', line 244

def unauthorized(params={})
  raise Exceptions::Unauthorized.new
end