global_session 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -9,6 +9,8 @@ In other words: it glues your semi-related Web apps together so they share the
9
9
  same bits of session state. This is done by putting the session itself into
10
10
  cookies.
11
11
 
12
+ Maintained by the RightScale Teal Team
13
+
12
14
  == What Is It Not?
13
15
 
14
16
  This plugin does not provide a complete solution for identity management. In
@@ -7,8 +7,8 @@ spec = Gem::Specification.new do |s|
7
7
  s.required_ruby_version = Gem::Requirement.new(">= 1.8.7")
8
8
 
9
9
  s.name = 'global_session'
10
- s.version = '2.0.1'
11
- s.date = '2012-01-04'
10
+ s.version = '2.0.2'
11
+ s.date = '2012-04-01'
12
12
 
13
13
  s.authors = ['Tony Spataro']
14
14
  s.email = 'support@rightscale.com'
@@ -92,9 +92,14 @@ module GlobalSession
92
92
  # Create a new Session, initialized against this directory and ready to
93
93
  # be used by the app.
94
94
  #
95
+ # DEPRECATED: If a cookie is provided, load an existing session from its
96
+ # serialized form. You should use #load_session for this instead.
97
+ #
98
+ # @see load_session
99
+ #
95
100
  # === Parameters
96
- # directory(Directory):: directory implementation that the session should use for various operations
97
- # cookie(String):: Optional, serialized global session cookie. If none is supplied, a new session is created.
101
+ # cookie(String):: DEPRECATED - Optional, serialized global session cookie. If none is supplied, a new session is created.
102
+ # valid_signature_digest(String):: DEPRECATED - Optional,
98
103
  #
99
104
  # === Return
100
105
  # session(Session):: the newly-initialized session
@@ -104,19 +109,41 @@ module GlobalSession
104
109
  # ExpiredSession:: if the session contained in the cookie has expired
105
110
  # MalformedCookie:: if the cookie was corrupt or malformed
106
111
  # SecurityError:: if signature is invalid or cookie is not signed by a trusted authority
107
- def create_session(*params)
112
+ def create_session(cookie=nil, valid_signature_digest=nil)
108
113
  forced_version = configuration['cookie']['version']
109
114
 
110
- case forced_version
111
- when 2
112
- Session::V2.new(self, *params)
113
- when 1
114
- Session::V1.new(self, *params)
115
+ if cookie.nil?
116
+ # Create a legitimately new session
117
+ case forced_version
118
+ when 1
119
+ Session::V1.new(self, cookie, valid_signature_digest)
120
+ else
121
+ Session.new(self, cookie, valid_signature_digest)
122
+ end
115
123
  else
116
- Session.new(self, *params)
124
+ warn "GlobalSession::Directory#create_session with an existing session is DEPRECATED -- use #load_session instead"
125
+ load_session(cookie, valid_signature_digest)
117
126
  end
118
127
  end
119
128
 
129
+ # Unserialize an existing session cookie
130
+ #
131
+ # === Parameters
132
+ # cookie(String):: Optional, serialized global session cookie. If none is supplied, a new session is created.
133
+ # valid_signature_digest(String):: Optional,
134
+ #
135
+ # === Return
136
+ # session(Session):: the newly-initialized session
137
+ #
138
+ # ===Raise
139
+ # InvalidSession:: if the session contained in the cookie has been invalidated
140
+ # ExpiredSession:: if the session contained in the cookie has expired
141
+ # MalformedCookie:: if the cookie was corrupt or malformed
142
+ # SecurityError:: if signature is invalid or cookie is not signed by a trusted authority
143
+ def load_session(cookie, valid_signature_digest=nil)
144
+ Session.new(self, cookie, valid_signature_digest)
145
+ end
146
+
120
147
  def local_authority_name
121
148
  @configuration['authority']
122
149
  end
@@ -88,18 +88,27 @@ module GlobalSession
88
88
  env['rack.cookies'] = {} unless env['rack.cookies']
89
89
 
90
90
  begin
91
+ err = nil
91
92
  read_cookie(env)
92
- rescue Exception => e
93
- env['global_session'] = @directory.create_session
94
- handle_error('reading session cookie', env, e)
93
+ rescue Exception => read_err
94
+ err = read_err
95
+
96
+ # Catch "double whammy" errors
97
+ begin
98
+ env['global_session'] = @directory.create_session
99
+ rescue Exception => create_err
100
+ err = create_err
101
+ end
102
+
103
+ handle_error('reading session cookie', env, err)
95
104
  end
96
105
 
97
106
  tuple = nil
98
107
 
99
108
  begin
100
109
  tuple = @app.call(env)
101
- rescue Exception => e
102
- handle_error('processing request', env, e)
110
+ rescue Exception => read_err
111
+ handle_error('processing request', env, read_err)
103
112
  return tuple
104
113
  else
105
114
  renew_cookie(env)
@@ -109,16 +118,16 @@ module GlobalSession
109
118
  end
110
119
 
111
120
  protected
112
-
121
+
113
122
  # Read a cookie from the Rack environment.
114
123
  #
115
124
  # === Parameters
116
125
  # env(Hash): Rack environment.
117
126
  def read_cookie(env)
118
- if env['rack.cookies'].has_key?(@cookie_name)
119
- env['global_session'] = @directory.create_session(env['rack.cookies'][@cookie_name])
120
- elsif @cookie_retrieval && cookie = @cookie_retrieval.call(env)
121
- env['global_session'] = @directory.create_session(cookie)
127
+ if @cookie_retrieval && (cookie = @cookie_retrieval.call(env))
128
+ env['global_session'] = @directory.load_session(cookie)
129
+ elsif env['rack.cookies'].has_key?(@cookie_name)
130
+ env['global_session'] = @directory.load_session(env['rack.cookies'][@cookie_name])
122
131
  else
123
132
  env['global_session'] = @directory.create_session
124
133
  end
@@ -131,7 +140,7 @@ module GlobalSession
131
140
  # === Parameters
132
141
  # env(Hash): Rack environment
133
142
  def renew_cookie(env)
134
- return unless env['global_session'].directory.local_authority_name
143
+ return unless @directory.local_authority_name
135
144
  return if env['global_session.req.renew'] == false
136
145
 
137
146
  if (renew = @configuration['renew']) && env['global_session'] &&
@@ -145,7 +154,7 @@ module GlobalSession
145
154
  # === Parameters
146
155
  # env(Hash): Rack environment
147
156
  def update_cookie(env)
148
- return unless env['global_session'].directory.local_authority_name
157
+ return unless @directory.local_authority_name
149
158
  return if env['global_session.req.update'] == false
150
159
 
151
160
  domain = @configuration['cookie']['domain'] || env['SERVER_NAME']
@@ -179,7 +188,7 @@ module GlobalSession
179
188
  # === Parameters
180
189
  # env(Hash): Rack environment
181
190
  def wipe_cookie(env)
182
- return unless env['global_session'].directory.local_authority_name
191
+ return unless @directory.local_authority_name
183
192
  return if env['global_session.req.update'] == false
184
193
 
185
194
  domain = @configuration['cookie']['domain'] || env['SERVER_NAME']
@@ -24,12 +24,8 @@ require 'set'
24
24
  require 'zlib'
25
25
 
26
26
  module GlobalSession::Session
27
- # Ladies and gentlemen: the one and only, star of the show, GLOBAL SESSION!
28
- #
29
- # Session is designed to act as much like a Hash as possible. You can use
30
- # most of the methods you would use with Hash: [], has_key?, each, etc. It has a
31
- # few additional methods that are specific to itself, mostly involving whether
32
- # it's expired, valid, supports a certain key, etc.
27
+ # Global session V1 uses JSON serialization and Zlib compression. Its encoding looks something
28
+ # like this:
33
29
  #
34
30
  class V1 < Abstract
35
31
  # Utility method to decode a cookie; good for console debugging. This performs no
@@ -49,7 +49,7 @@ module GlobalSession::Session
49
49
  # ExpiredSession:: if the session contained in the cookie has expired
50
50
  # MalformedCookie:: if the cookie was corrupt or malformed
51
51
  # SecurityError:: if signature is invalid or cookie is not signed by a trusted authority
52
- def initialize(directory, cookie=nil, unused=nil)
52
+ def initialize(directory, cookie=nil)
53
53
  super(directory)
54
54
  @configuration = directory.configuration
55
55
  @schema_signed = Set.new((@configuration['attributes']['signed']))
@@ -24,15 +24,15 @@ require 'global_session/session/v2'
24
24
  # by the different versions; it is responsible for detecting the version of
25
25
  # a given cookie, then instantiating a suitable session object.
26
26
  module GlobalSession::Session
27
- def self.new(*args)
28
- V2.new(*args)
29
- rescue GlobalSession::MalformedCookie => e
30
- V1.new(*args)
31
- end
32
-
33
27
  def self.decode_cookie(*args)
34
28
  V2.decode_cookie(*args)
35
29
  rescue GlobalSession::MalformedCookie => e
36
30
  V1.decode_cookie(*args)
37
31
  end
32
+
33
+ def self.new(directory, cookie=nil, valid_signature_digest=nil)
34
+ V2.new(directory, cookie)
35
+ rescue GlobalSession::MalformedCookie => e
36
+ V1.new(directory, cookie, valid_signature_digest)
37
+ end
38
38
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: global_session
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 1
10
- version: 2.0.1
9
+ - 2
10
+ version: 2.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tony Spataro
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-04 00:00:00 -08:00
18
+ date: 2012-04-01 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -30,9 +30,9 @@ dependencies:
30
30
  - 5
31
31
  version: "2.5"
32
32
  requirement: *id001
33
+ type: :runtime
33
34
  name: right_support
34
35
  prerelease: false
35
- type: :runtime
36
36
  - !ruby/object:Gem::Dependency
37
37
  version_requirements: &id002 !ruby/object:Gem::Requirement
38
38
  none: false
@@ -46,9 +46,9 @@ dependencies:
46
46
  - 0
47
47
  version: 0.2.0
48
48
  requirement: *id002
49
+ type: :runtime
49
50
  name: simple_uuid
50
51
  prerelease: false
51
- type: :runtime
52
52
  - !ruby/object:Gem::Dependency
53
53
  version_requirements: &id003 !ruby/object:Gem::Requirement
54
54
  none: false
@@ -61,9 +61,9 @@ dependencies:
61
61
  - 4
62
62
  version: "1.4"
63
63
  requirement: *id003
64
+ type: :runtime
64
65
  name: json
65
66
  prerelease: false
66
- type: :runtime
67
67
  - !ruby/object:Gem::Dependency
68
68
  version_requirements: &id004 !ruby/object:Gem::Requirement
69
69
  none: false
@@ -76,9 +76,9 @@ dependencies:
76
76
  - 4
77
77
  version: "0.4"
78
78
  requirement: *id004
79
+ type: :runtime
79
80
  name: msgpack
80
81
  prerelease: false
81
- type: :runtime
82
82
  - !ruby/object:Gem::Dependency
83
83
  version_requirements: &id005 !ruby/object:Gem::Requirement
84
84
  none: false
@@ -91,9 +91,9 @@ dependencies:
91
91
  - 0
92
92
  version: "1.0"
93
93
  requirement: *id005
94
+ type: :runtime
94
95
  name: rack-contrib
95
96
  prerelease: false
96
- type: :runtime
97
97
  description: This plugin for Rails allows several web apps in an authentication domain to share session state, facilitating single sign-on in a distributed web app. It only provides session sharing and does not concern itself with authentication or replication of the user database.
98
98
  email: support@rightscale.com
99
99
  executables: []