]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/aviator/feature/aviator/core/session.rb
add aimonb/aviator to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / aviator / feature / aviator / core / session.rb
1 module Aviator
2
3   class Session
4
5     class AuthenticationError < StandardError
6       def initialize(last_auth_body)
7         super("Authentication failed. The server returned #{ last_auth_body }")
8       end
9     end
10
11
12     class EnvironmentNotDefinedError < ArgumentError
13       def initialize(path, env)
14         super("The environment '#{ env }' is not defined in #{ path }.")
15       end
16     end
17
18     class InitializationError < StandardError
19       def initialize
20         super("The session could not find :session_dump, :config_file, and " \
21               ":config in the constructor arguments provided")
22       end
23     end
24
25     class InvalidConfigFilePathError < ArgumentError
26       def initialize(path)
27         super("The config file at #{ path } does not exist!")
28       end
29     end
30
31
32     class NotAuthenticatedError < StandardError
33       def initialize
34         super("Session is not authenticated. Please authenticate before proceeding.")
35       end
36     end
37
38
39     class ValidatorNotDefinedError < StandardError
40       def initialize
41         super("The validator request name is not defined for this session object.")
42       end
43     end
44
45
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])
53       else
54         raise InitializationError.new
55       end
56
57       @log_file = opts[:log_file]
58     end
59
60
61     def authenticate(&block)
62       block ||= lambda do |params|
63         environment[:auth_credentials].each do |key, value|
64           params[key] = value
65         end
66       end
67
68       response = auth_service.request environment[:auth_service][:request].to_sym, &block
69
70       if response.status == 200
71         @auth_info = response.body
72         update_services_session_data
73       else
74         raise AuthenticationError.new(response.body)
75       end
76     end
77
78
79     def authenticated?
80       !auth_info.nil?
81     end
82
83
84     def dump
85       JSON.generate({
86         :environment => environment,
87         :auth_info   => auth_info
88       })
89     end
90
91
92     def load(session_dump)
93       initialize_with_dump(session_dump)
94       update_services_session_data
95       self
96     end
97
98
99     def method_missing(name, *args, &block)
100       service_name_parts = name.to_s.match(/^(\w+)_service$/)
101
102       if service_name_parts
103         get_service_obj(service_name_parts[1])
104       else
105         super name, *args, &block
106       end
107     end
108
109
110     def self.load(session_dump, opts={})
111       opts[:session_dump] = session_dump
112
113       new(opts)
114     end
115
116
117     def validate
118       raise NotAuthenticatedError.new unless authenticated?
119       raise ValidatorNotDefinedError.new unless environment[:auth_service][:validator]
120
121       auth_with_bootstrap = auth_info.merge({ :auth_service => environment[:auth_service] })
122
123       response = auth_service.request environment[:auth_service][:validator].to_sym, :session_data => auth_with_bootstrap
124
125       response.status == 200 || response.status == 203
126     end
127
128
129     private
130
131
132     def auth_info
133       @auth_info
134     end
135
136
137     def auth_service
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
143       )
144     end
145
146
147     def environment
148       @environment
149     end
150
151
152     def get_service_obj(service_name)
153       raise NotAuthenticatedError.new unless self.authenticated?
154
155       @services ||= {}
156
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
162       )
163
164       @services[service_name]
165     end
166
167
168     def initialize_with_config(config_path, environment)
169       raise InvalidConfigFilePathError.new(config_path) unless Pathname.new(config_path).file?
170
171       config = Hashish.new(YAML.load_file(config_path))
172
173       raise EnvironmentNotDefinedError.new(config_path, environment) unless config[environment]
174
175       @environment = config[environment]
176     end
177
178
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]
183     end
184
185     def initialize_with_hash(hash_obj)
186       @environment = Hashish.new(hash_obj)
187     end
188
189     def log_file
190       @log_file
191     end
192
193
194     def update_services_session_data
195       return unless @services
196
197       @services.each do |name, obj|
198         obj.default_session_data = auth_info
199       end
200     end
201
202   end
203
204 end