5 class AuthenticationError < StandardError
6 def initialize(last_auth_body)
7 super("Authentication failed. The server returned #{ last_auth_body }")
12 class EnvironmentNotDefinedError < ArgumentError
13 def initialize(path, env)
14 super("The environment '#{ env }' is not defined in #{ path }.")
18 class InitializationError < StandardError
20 super("The session could not find :session_dump, :config_file, and " \
21 ":config in the constructor arguments provided")
25 class InvalidConfigFilePathError < ArgumentError
27 super("The config file at #{ path } does not exist!")
32 class NotAuthenticatedError < StandardError
34 super("Session is not authenticated. Please authenticate before proceeding.")
39 class ValidatorNotDefinedError < StandardError
41 super("The validator request name is not defined for this session object.")
46 def initialize(opts={})
47 if opts.has_key? :session_dump
48 initialize_with_dump(opts[:session_dump])
49 elsif opts.has_key? :config_file
50 initialize_with_config(opts[:config_file], opts[:environment])
51 elsif opts.has_key? :config
52 initialize_with_hash(opts[:config])
54 raise InitializationError.new
57 @log_file = opts[:log_file]
61 def authenticate(&block)
62 block ||= lambda do |params|
63 environment[:auth_credentials].each do |key, value|
68 response = auth_service.request environment[:auth_service][:request].to_sym, &block
70 if response.status == 200
71 @auth_info = response.body
72 update_services_session_data
74 raise AuthenticationError.new(response.body)
86 :environment => environment,
87 :auth_info => auth_info
92 def load(session_dump)
93 initialize_with_dump(session_dump)
94 update_services_session_data
99 def method_missing(name, *args, &block)
100 service_name_parts = name.to_s.match(/^(\w+)_service$/)
102 if service_name_parts
103 get_service_obj(service_name_parts[1])
105 super name, *args, &block
110 def self.load(session_dump, opts={})
111 opts[:session_dump] = session_dump
118 raise NotAuthenticatedError.new unless authenticated?
119 raise ValidatorNotDefinedError.new unless environment[:auth_service][:validator]
121 auth_with_bootstrap = auth_info.merge({ :auth_service => environment[:auth_service] })
123 response = auth_service.request environment[:auth_service][:validator].to_sym, :session_data => auth_with_bootstrap
125 response.status == 200 || response.status == 203
138 @auth_service ||= Service.new(
139 :provider => environment[:provider],
140 :service => environment[:auth_service][:name],
141 :default_session_data => { :auth_service => environment[:auth_service] },
142 :log_file => log_file
152 def get_service_obj(service_name)
153 raise NotAuthenticatedError.new unless self.authenticated?
157 @services[service_name] ||= Service.new(
158 :provider => environment[:provider],
159 :service => service_name,
160 :default_session_data => auth_info,
161 :log_file => log_file
164 @services[service_name]
168 def initialize_with_config(config_path, environment)
169 raise InvalidConfigFilePathError.new(config_path) unless Pathname.new(config_path).file?
171 config = Hashish.new(YAML.load_file(config_path))
173 raise EnvironmentNotDefinedError.new(config_path, environment) unless config[environment]
175 @environment = config[environment]
179 def initialize_with_dump(session_dump)
180 session_info = Hashish.new(JSON.parse(session_dump))
181 @environment = session_info[:environment]
182 @auth_info = session_info[:auth_info]
185 def initialize_with_hash(hash_obj)
186 @environment = Hashish.new(hash_obj)
194 def update_services_session_data
195 return unless @services
197 @services.each do |name, obj|
198 obj.default_session_data = auth_info