sessionvoc-open 1.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.7.3
data/config.yml.sample ADDED
@@ -0,0 +1,5 @@
1
+ protocol: http
2
+ host: localhost
3
+ port: 8208
4
+ strict_mode: true
5
+ auth: simple
@@ -0,0 +1,31 @@
1
+ CREATE TABLE `users` (
2
+ `id` int(11) NOT NULL AUTO_INCREMENT,
3
+ `username` varchar(255) DEFAULT NULL,
4
+ PRIMARY KEY (`id`)
5
+ ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
6
+
7
+ CREATE TABLE `passwords` (
8
+ `id` int(11) NOT NULL,
9
+ `password` varchar(255),
10
+ PRIMARY KEY (`id`)
11
+ ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
12
+
13
+ CREATE TABLE `user_data` (
14
+ `id` int(11) NOT NULL,
15
+ `name` varchar(255),
16
+ `surname` varchar(255),
17
+ `mobile` varchar(255),
18
+ PRIMARY KEY (`id`)
19
+ ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
20
+
21
+ CREATE TABLE `user_preferences` (
22
+ `id` int(11) NOT NULL,
23
+ `font_size` int(11),
24
+ `sort_order` varchar(255),
25
+ PRIMARY KEY (`id`)
26
+ ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
27
+
28
+ INSERT INTO users (username) VALUES("testuser");
29
+ INSERT INTO passwords (id, password) VALUES(1, md5('tester'));
30
+ INSERT INTO user_data (id, name, surname, mobile) VALUES(1, "Tes", "Testmann", "0111-11111111");
31
+ INSERT INTO user_preferences (id, font_size, sort_order) VALUES(1, 14, "DESC");
@@ -0,0 +1,110 @@
1
+ <SESSION id="sessionVOC">
2
+ <!--////////////////////////////////////////////////////////////////////////////////////-->
3
+ <!-- LOGIN SECTION -->
4
+ <!--////////////////////////////////////////////////////////////////////////////////////-->
5
+ <login-data>
6
+ <authentications>
7
+ <authenticate>
8
+ <database id="1">
9
+ <type>mysql</type> <!--the type of database we are using to store the user data -->
10
+ <host>localhost</host> <!--the machine name where server resides -->
11
+ <port>3306</port> <!--port number -->
12
+ <db>sessionvoc</db> <!-- the name of the database to use -->
13
+ <username>root</username> <!-- the name of the user used to log onto the database - if left blank can be set via the server at startup -->
14
+ <password></password> <!-- the password used to log onto the database - if left blank can be set via the server at startup -->
15
+ </database>
16
+ <database-communication>
17
+ <database-hash>MD5</database-hash>
18
+ <hash-select>MD5</hash-select>
19
+ <select>select passwords.password from passwords left join users on users.id = passwords.id where users.username = ?</select>
20
+ </database-communication>
21
+ </authenticate>
22
+ </authentications>
23
+ <client-server>
24
+ <authentication-method>SIMPLE</authentication-method>
25
+ </client-server>
26
+ </login-data>
27
+
28
+ <!--////////////////////////////////////////////////////////////////////////////////////-->
29
+ <!-- USER DATA SECTION -->
30
+ <!--////////////////////////////////////////////////////////////////////////////////////-->
31
+ <user-data>
32
+ <database id="1"/>
33
+ <attributes>
34
+ <attribute>
35
+ <name>name</name>
36
+ <type>string</type>
37
+ </attribute>
38
+
39
+ <attribute>
40
+ <name>surname</name>
41
+ <type>string</type>
42
+ </attribute>
43
+
44
+ <attribute>
45
+ <name>mobile</name>
46
+ <type>string</type>
47
+ <read-only/>
48
+ </attribute>
49
+
50
+ <load>
51
+ select name, surname, mobile from user_data left join users on users.id = user_data.id where users.username = ?
52
+ </load>
53
+
54
+ <save>
55
+ update user_data left join users on user_data.id = users.id set user_data.name = ?, user_data.surname = ?, user_data.mobile = ? where users.username = ?
56
+ </save>
57
+ </attributes>
58
+
59
+ <attributes>
60
+
61
+ <attribute>
62
+ <name>font_size</name>
63
+ <type>integer</type>
64
+ </attribute>
65
+
66
+ <attribute>
67
+ <name>sort_order</name>
68
+ <type>string</type>
69
+ </attribute>
70
+
71
+ <load>
72
+ select font_size, sort_order from user_preferences left join users on users.id = user_preferences.id where users.username = ?
73
+ </load>
74
+
75
+ <save>
76
+ update user_preferences left join users on user_preferences.id = users.id set user_preferences.font_size = ?, user_preferences.sort_order = ? where users.username = ?
77
+ </save>
78
+ </attributes>
79
+
80
+ <update-method>write-through</update-method>
81
+
82
+ </user-data>
83
+
84
+ <!--////////////////////////////////////////////////////////////////////////////////////-->
85
+ <!-- TRANSIENT DATA SECTION -->
86
+ <!--////////////////////////////////////////////////////////////////////////////////////-->
87
+ <transient-data>
88
+ <attributes>
89
+ <attribute>
90
+ <name>_csrf_token</name> <!-- Required for use with Ruby on Rails! -->
91
+ <type>string</type>
92
+ </attribute>
93
+
94
+ <attribute>
95
+ <name>message</name>
96
+ <type>string</type>
97
+ </attribute>
98
+
99
+ <attribute>
100
+ <name>ipAddress</name>
101
+ <type>string</type>
102
+ </attribute>
103
+
104
+ <attribute>
105
+ <name>superAttribute</name>
106
+ <type>variant</type>
107
+ </attribute>
108
+ </attributes>
109
+ </transient-data>
110
+ </SESSION>
data/init.rb ADDED
@@ -0,0 +1,6 @@
1
+ # Copyright:: 2011 triAGENS GmbH
2
+ # Author:: Oliver Kiessler (mailto:kiessler@inceedo.com)
3
+ require "sessionvoc-store/open/railtie"
4
+ require "sessionvoc-store/open/controller_methods"
5
+
6
+ ActionController::Base.send :include, ::ControllerMethods::InstanceMethods
data/install.rb ADDED
@@ -0,0 +1,4 @@
1
+ # Copyright:: 2011 triAGENS GmbH
2
+ # Author:: Oliver Kiessler (mailto:kiessler@inceedo.com)
3
+
4
+ # Install hook code here
@@ -0,0 +1,26 @@
1
+ # Copyright:: 2011 triAGENS GmbH
2
+ # Author:: Oliver Kiessler (mailto:kiessler@inceedo.com)
3
+ require 'rubygems'
4
+ gem 'httparty'
5
+ require 'httparty'
6
+ gem 'json'
7
+ require 'json'
8
+ require 'logger'
9
+ require 'yaml'
10
+ require 'digest'
11
+ require 'base64'
12
+
13
+ require File.dirname(__FILE__) + '/sessionvoc/open/configuration.rb'
14
+ require File.dirname(__FILE__) + '/sessionvoc/open/exceptions.rb'
15
+ require File.dirname(__FILE__) + '/sessionvoc/open/session.rb'
16
+ require File.dirname(__FILE__) + '/sessionvoc/open/authentification.rb'
17
+ require File.dirname(__FILE__) + '/sessionvoc/open/form_data.rb'
18
+ require File.dirname(__FILE__) + '/sessionvoc/open/meta_data.rb'
19
+ require File.dirname(__FILE__) + '/sessionvoc/open/data_conversion.rb'
20
+ require File.dirname(__FILE__) + '/sessionvoc/open/base.rb'
21
+ require File.dirname(__FILE__) + '/sessionvoc/open/client.rb'
22
+
23
+ if defined?(Rails)
24
+ require File.dirname(__FILE__) + '/sessionvoc-store/open/railtie.rb'
25
+ require File.dirname(__FILE__) + '/sessionvoc-store/open/sessionvoc_store.rb'
26
+ end
@@ -0,0 +1,10 @@
1
+ module ControllerMethods
2
+ module InstanceMethods
3
+ # Workaround used to create a new rack session because rack sessions are being created
4
+ # lazily and the usage of some of the sessionvoc methods might fail if no rack session
5
+ # exists.
6
+ def init_sessionvoc
7
+ session['sessionvoc-init'] = true
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ # Copyright:: 2011 triAGENS GmbH
2
+ # Author:: Oliver Kiessler (mailto:kiessler@inceedo.com)
3
+ require 'sessionvoc-open'
4
+ require "sessionvoc-store/open/controller_methods"
5
+ require "rails"
6
+
7
+ module SessionvocStore
8
+ class Railtie < Rails::Railtie
9
+ rake_tasks do
10
+ # not used at the moment
11
+ end
12
+
13
+ initializer "setup sessionvoc session store" do |app|
14
+ ActionController::Base.send :include, ::ControllerMethods::InstanceMethods
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,299 @@
1
+ # Copyright:: 2011 triAGENS GmbH
2
+ # Author:: Oliver Kiessler (mailto:kiessler@inceedo.com)
3
+ module ActionDispatch
4
+ module Session
5
+ # Monkey patch to include SessionVOC specific methods into the session context.
6
+ # Convenience methods for the SessionVOC client.
7
+ class AbstractStore::SessionHash
8
+ include Sessionvoc::Open::DataConversion
9
+
10
+ # Overriden method to incept session hash access.
11
+ # === Parameters
12
+ # * key = Session key
13
+ def [](key)
14
+ if key == :_csrf_token
15
+ self['transData']['_csrf_token'] if self['transData']
16
+ else
17
+ load_for_read!
18
+ super(key.to_s)
19
+ end
20
+ end
21
+
22
+ # Overriden method to incept session hash access.
23
+ # === Parameters
24
+ # * key = Session key
25
+ # * value = Value
26
+ def []=(key, value)
27
+ if key == :_csrf_token
28
+ self['transData'] = {} unless self['transData']
29
+ self['transData']['_csrf_token'] = value
30
+ ActionDispatch::Session::SessionvocStore::Session.client.update(self['sid'], self)
31
+ else
32
+ load_for_write!
33
+ super(key.to_s, value)
34
+ end
35
+ end
36
+
37
+ # Adds a key/value pair to the transData context of a SessionVOC session.
38
+ # === Parameters
39
+ # * sid = Session Id
40
+ # * key = Key
41
+ # * value = Value
42
+ # * options
43
+ def set_trans_data(sid, key, value, options = {})
44
+ Rails.logger.debug("AbstractStore::SessionHash#set_trans_data")
45
+ enforce_value_type("transData", key.to_s, value, self)
46
+ ActionDispatch::Session::SessionvocStore::Session.client.update(sid, self)
47
+ end
48
+
49
+ # Adds a key/value pair to the userData context of a SessionVOC session.
50
+ # === Parameters
51
+ # * sid = Session Id
52
+ # * key = Key
53
+ # * value = Value
54
+ # * options
55
+ def set_user_data(sid, key, value, options = {})
56
+ Rails.logger.debug("AbstractStore::SessionHash#set_user_data")
57
+ enforce_value_type("userData", key.to_s, value, self)
58
+ ActionDispatch::Session::SessionvocStore::Session.client.update(sid, self)
59
+ end
60
+
61
+ # Creates a new form context within this session.
62
+ def new_form
63
+ Rails.logger.debug("AbstractStore::SessionHash#new_form")
64
+ ActionDispatch::Session::SessionvocStore::Session.client.create_form_data(self['sid'])
65
+ end
66
+
67
+ # Updates/replaces the form data identified by a sid in SessionVOC.
68
+ # === Parameters
69
+ # * fid = Form Id
70
+ # * data = Form data hash
71
+ # * options
72
+ def set_form_data(fid, data, options = {})
73
+ Rails.logger.debug("Sessionvoc#set_form_data")
74
+ ActionDispatch::Session::SessionvocStore::Session.client.update_form_data(self['sid'], fid, data, options)
75
+ end
76
+
77
+ # Returns a form context from SessionVOC identified by a fid.
78
+ # === Parameters
79
+ # * fid = Form Id
80
+ # * options
81
+ def get_form_data(fid, options = {})
82
+ Rails.logger.debug("Sessionvoc#get_form_data")
83
+ ActionDispatch::Session::SessionvocStore::Session.client.get_form_data(self['sid'], fid, options)
84
+ end
85
+
86
+ # Deletes a form context in SessionVOC
87
+ # === Parameters
88
+ # * fid = Form Id
89
+ # * options
90
+ def delete_form_data(fid, options = {})
91
+ Rails.logger.debug("Sessionvoc#delete_form_data")
92
+ ActionDispatch::Session::SessionvocStore::Session.client.delete_form_data(self['sid'], fid, options)
93
+ end
94
+
95
+ # Performs an authentification against SessionVOC.
96
+ # === Parameters
97
+ # * sid = Session Id
98
+ # * uid = User
99
+ # * password = User password
100
+ # * options
101
+ def login(sid, uid, password, options = {})
102
+ Rails.logger.debug("Sessionvoc#login")
103
+ client = ActionDispatch::Session::SessionvocStore::Session.client; response = nil
104
+ options = options.merge(:no_exception => true)
105
+ if client.configuration.options["auth"] == 'none' or client.configuration.options["auth"] == 'simple'
106
+ response = client.simple(sid, uid, password, options)
107
+ elsif client.configuration.options["auth"] == 'challenge'
108
+ response = client.challenge(sid, uid, password, options)
109
+ end
110
+ if response and response['userData']
111
+ self['userData'] = response['userData']
112
+ else
113
+ return false
114
+ end
115
+ true
116
+ end
117
+
118
+ # Performs a user logout.
119
+ # === Parameters
120
+ # * sid = Session Id
121
+ # * options
122
+ def logout(sid, options = {})
123
+ Rails.logger.debug("Sessionvoc#logout")
124
+ ActionDispatch::Session::SessionvocStore::Session.client.logout(sid, options)
125
+ end
126
+
127
+ # Creates a one time use nonce.
128
+ # === Parameters
129
+ # * options
130
+ def create_nonce(options = {})
131
+ nonce = ActionDispatch::Session::SessionvocStore::Session.client.create_nonce(nil, nil, :no_encode => true)
132
+ Base64.encode64(nonce)
133
+ end
134
+
135
+ # Checks if the nonce is still valid and has not been used yet.
136
+ # === Parameters
137
+ # * nonce = Nonce string
138
+ # * options
139
+ def nonce_still_valid?(nonce, options = {})
140
+ ActionDispatch::Session::SessionvocStore::Session.client.get_nonce(nonce, options)
141
+ end
142
+ end
143
+
144
+ # Wrapper class for holding the SessionVOC session data.
145
+ class SessionvocStore < AbstractStore
146
+ class Session
147
+ attr_accessor :data, :sid, :options
148
+
149
+ # Creates a new session data wrapper.
150
+ # === Parameters
151
+ # * sid = Session Id
152
+ # * data = SessionVOC data
153
+ # * options
154
+ def initialize(sid, data, options = {})
155
+ self.sid = sid; self.data = data; self.options = options
156
+ end
157
+
158
+ # Creates a new session id returned from SessionVOC.
159
+ def self.generate_sid
160
+ svoc_session_sid = client.new_session
161
+ Rails.logger.debug("SessionVOC Sid: #{svoc_session_sid}")
162
+ svoc_session_sid
163
+ end
164
+
165
+ # Returns session data from SessionVOC.
166
+ # === Parameters
167
+ # * sid = Session Id
168
+ def self.get(sid)
169
+ session_data = nil
170
+ begin
171
+ session_data = client.get_session(sid)
172
+ rescue Sessionvoc::Open::InvalidSidException
173
+ sid = client.new_session
174
+ session_data = client.get_session(sid)
175
+ end
176
+ if sid and session_data
177
+ return Session.new(sid, session_data)
178
+ else
179
+ raise "Could not get the session!"
180
+ end
181
+ end
182
+
183
+ # Updates session data in SessionVOC.
184
+ # === Parameters
185
+ # * session_data = Session data
186
+ # * options
187
+ def set(session_data, options)
188
+ Rails.logger.debug("Session#set")
189
+ self.data = session_data
190
+ Session.client.update(sid, session_data)
191
+ sid
192
+ end
193
+
194
+ # Destroy the SessionVOC session.
195
+ def destroy
196
+ Rails.logger.debug("Session#destroy")
197
+ Session.client.delete_session(self.sid)
198
+ end
199
+
200
+ # Returns the SessionVOC client.
201
+ def self.client
202
+ return @@sessionvoc_client if defined?(@@sessionvoc_client) and @@sessionvoc_client
203
+
204
+ if File.exists?("#{Rails.root.to_s}/config/sessionvoc.yml")
205
+ Rails.logger.info("Using configuration from config/sessionvoc.yml")
206
+ @@sessionvoc_client = Sessionvoc::Open::Client.new(YAML.load(File.read("#{Rails.root.to_s}/config/sessionvoc.yml")))
207
+ else
208
+ Rails.logger.warn("No configuration file found in Rails. Trying global configuration files...")
209
+ @@sessionvoc_client = Sessionvoc::Open::Client.new
210
+ end
211
+ end
212
+
213
+ # Custom to string method.
214
+ def to_s
215
+ "#{self.sid} => #{self.data.inspect}"
216
+ end
217
+ end
218
+
219
+ cattr_accessor :session_class
220
+ self.session_class = Session
221
+
222
+ ### Abstract rack session method implementations
223
+
224
+ # Creates a new rack session.
225
+ # === Parameters
226
+ # * app
227
+ # * options
228
+ def initialize(app, options = {})
229
+ super
230
+ Rails.logger.info("Initializing SessionVOC Session Store...")
231
+ end
232
+
233
+ private
234
+ # Finder method for a session.
235
+ # === Parameters
236
+ # * id = Session Id
237
+ def find_session(id)
238
+ @@session_class.get(id)
239
+ end
240
+
241
+ # Getter for session.
242
+ # === Parameters
243
+ # * env = Rack environment
244
+ # * sid = Session Id
245
+ def get_session(env, sid)
246
+ Rails.logger.debug("SessionvocStore#get_session")
247
+ sid = @@session_class.generate_sid unless sid
248
+ session = find_session(sid).data
249
+ [sid, session]
250
+ end
251
+
252
+ # Setter for session
253
+ # === Parameters
254
+ # * env = Rack environment
255
+ # * sid = Session Id
256
+ # * session_data = Session data to be updated
257
+ def set_session(env, sid, session_data)
258
+ Rails.logger.debug("SessionvocStore#set_session")
259
+ options = env['rack.session.options']
260
+ find_session(sid).set(session_data, options)
261
+ end
262
+
263
+ # Destroy rack session.
264
+ # === Parameters
265
+ # * env = Rack environment
266
+ def destroy(env)
267
+ Rails.logger.debug("SessionvocStore#destroy")
268
+ if sid = current_session_id(env)
269
+ return find_session(sid).destroy
270
+ end
271
+ false
272
+ end
273
+
274
+ # Returns meta data from SessionVOC.
275
+ def self.meta_data
276
+ @@meta_data ||= nil
277
+ @@meta_data = client.datainfo unless @@meta_data
278
+ @@meta_data
279
+ end
280
+
281
+ private
282
+ # Returns the SessionVOC client.
283
+ def self.client
284
+ @@sessionvoc_client ||= nil
285
+ unless @@sessionvoc_client
286
+ if File.exists?("#{Rails.root.to_s}/config/sessionvoc.yml")
287
+ Rails.logger.info("Using configuration from config/sessionvoc.yml")
288
+ @@sessionvoc_client = Sessionvoc::Open::Client.new(YAML.load(File.read("#{Rails.root.to_s}/config/sessionvoc.yml")))
289
+ else
290
+ Rails.logger.warn("No configuration file found in Rails. Trying global configuration files...")
291
+ @@sessionvoc_client = Sessionvoc::Open::Client.new
292
+ end
293
+ meta_data
294
+ end
295
+ @@sessionvoc_client
296
+ end
297
+ end
298
+ end
299
+ end