rack-session-cookie_store 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWVjYTIxYmI3NzAzMzkyNWMyZmFmNWI4MjEwYjFhNTBhOTBiZGYyZA==
5
+ data.tar.gz: !binary |-
6
+ YzVhMThhNDcxODc3MjNlMjE4NDQ3MTJmN2ExZTMwMzhhYWJmYTgwOA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ N2U0ODNiOGQ4ZTg0YzVmY2MxMGEzMmZlYjY1MmYzMjhhOTQ1NTAyMDNkMWM3
10
+ ZmE2NTY0MjkwN2JiMzZjMTdhZDQyMjViZGEzNjgwNmIxOTQ4NjkxYmJmNzBh
11
+ NzJjODY4YzgwNjVhY2ZiNzM2M2FkODA1ZTI4NDhhODQ2YzNkMzg=
12
+ data.tar.gz: !binary |-
13
+ ZDI3Y2U2NjI4M2Q0NDM0MzRkYWIyMzViMDM4YjU0ZTJjYzI2NjE4MDdiZDk4
14
+ ZWFiMzExNTMxMmRjNWRmOGY3Yzg5MDZiODgzMDI5ZmIwZjU4NzUzMDYxMjcx
15
+ YmIyMmI3YWQ2MjdhMDIwMDA2NjAyNmU1YjBjM2JjZWY5ZTQyMTE=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-session-cookie_store.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Aaron Qian
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rack::CookieStore
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-session-cookie_store'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-session-cookie_store
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ require "rack/session/cookie_store"
@@ -0,0 +1,107 @@
1
+ require 'digest'
2
+ require 'rack/request'
3
+ require 'rack/response'
4
+ require 'rack/session/abstract/id'
5
+ require 'rack/session/cookie_store/version'
6
+
7
+ module Rack
8
+ module Session
9
+ class CookieStore < Abstract::ID
10
+ class Signer
11
+ def sign(data, secret)
12
+ "s:#{data}.#{digest(data, secret)}"
13
+ end
14
+
15
+ def unsign(data, secret)
16
+ return nil unless data[0..1] == 's:'
17
+ str = data[2..-1]
18
+ str = str[0...str.rindex('.')]
19
+ return nil unless sign(str, secret) == data
20
+ str
21
+ end
22
+
23
+ private
24
+ def digest(data, secret)
25
+ Digest::HMAC.base64digest(data, secret, Digest::SHA256).gsub /\=+$/, ''
26
+ end
27
+ end
28
+
29
+ class Marshal
30
+ def dump(hash)
31
+ "j:#{JSON.dump(hash)}"
32
+ end
33
+
34
+ def load(str)
35
+ return nil unless str [0..1] == 'j:'
36
+ JSON.parse(str[2..-1])
37
+ rescue
38
+ nil
39
+ end
40
+ end
41
+
42
+ def initialize(app, options)
43
+ @secret = options[:secret]
44
+ @marshal = options[:marshal] ||= Marshal.new
45
+ @signer = options[:signer] ||= Signer.new
46
+
47
+ super(app, options.merge!(:cookie_only => true))
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :marshal, :signer
53
+
54
+ def load_session(env)
55
+ data = unpacked_cookie_data(env)
56
+ data = persistent_session_id!(data)
57
+ p "load session"
58
+ p data
59
+ [data["session_id"], data]
60
+ end
61
+
62
+ def unpacked_cookie_data(env)
63
+ env["rack.session.unpacked_cookie_data"] ||= begin
64
+ request = Rack::Request.new(env)
65
+ str = signer.unsign(request.cookies[@key], @secret)
66
+ marshal.load(str) || {}
67
+ end
68
+ end
69
+
70
+ def extract_session_id(env)
71
+ unpacked_cookie_data(env)["session_id"]
72
+ end
73
+
74
+ def persistent_session_id!(data, sid=nil)
75
+ data ||= {}
76
+ data["session_id"] ||= sid || generate_sid
77
+ data
78
+ end
79
+
80
+ def set_cookie(env, headers, cookie)
81
+ Utils.set_cookie_header!(headers, @key, cookie)
82
+ end
83
+
84
+ def set_session(env, session_id, session, options)
85
+ session = session.merge("session_id" => session_id)
86
+ session_data = signer.sign(marshal.dump(session), @secret)
87
+
88
+ p "set session"
89
+ p session
90
+ p session_data
91
+
92
+ if session_data.size > (4096 - @key.size)
93
+ env["rack.errors"].puts("Warning! Rack::Session::Cookie data size exceeds 4K.")
94
+ nil
95
+ else
96
+ session_data
97
+ end
98
+ end
99
+
100
+ def destroy_session(env, session_id, options)
101
+ # Nothing to do here, data is in the client
102
+ generate_sid unless options[:drop]
103
+ end
104
+ end
105
+ end
106
+ end
107
+
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ module Session
3
+ class CookieStore
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'rack/session/cookie_store/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "rack-session-cookie_store"
9
+ gem.version = Rack::Session::CookieStore::VERSION
10
+ gem.authors = ["Aaron Qian"]
11
+ gem.email = ["aq1018@gmail.com"]
12
+ gem.description = %q{ a better cookie session store for rack. }
13
+ gem.summary = %q{ Uses JSON to store session data and signed with sha256. cookie is compatible with node.js connect middleware. }
14
+ gem.homepage = ""
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency 'rack', '~> 1.4.5'
22
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-session-cookie_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Qian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.5
27
+ description: ! ' a better cookie session store for rack. '
28
+ email:
29
+ - aq1018@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - lib/rack-session-cookie_store.rb
40
+ - lib/rack/session/cookie_store.rb
41
+ - lib/rack/session/cookie_store/version.rb
42
+ - rack-session-cookie_store.gemspec
43
+ homepage: ''
44
+ licenses: []
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.0.0
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Uses JSON to store session data and signed with sha256. cookie is compatible
66
+ with node.js connect middleware.
67
+ test_files: []