cookie_monster 0.1.1 → 0.1.2

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDlhOTM2ZjFmYjJlOTVlMjYzNzhhOWU1NzAwZTlhNDBlNzE2ZDlkYw==
4
+ ZDc0OThjMzRkMWM5ODZhOTNiMTBlOGFlMTAyZTY3ZmE5NjhhZDA2Mw==
5
5
  data.tar.gz: !binary |-
6
- NWIzZGEzMTYyMjFjYzdlZjIyN2Q0MWRmZTk4MTk3MGM4NTI0ODE5MA==
6
+ NjgxNmEyZDM5MTVlZmYzNjhmYTVhYWVjODdjZDM1Y2E5YjcyY2Y5Ng==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YzJhZTM0ZDNlOGMyYmJhYWI1YTNmZDhkMGYyMGZmMTNhZDMzNzIxMzEzNGZi
10
- YjUzNGIxODAzNDE1MzRjOWE4Mjk3ZDQ1ODEyNzFhYWJlYzhmM2VmZTY5Y2M0
11
- MTU1OTk0ZDM3OWY1ODhmNWQ3YjhjYzdiYjgzOTZmODBlYTg2ZTU=
9
+ ZTBiMjI5Zjc0ZWYxNWE0NGU1MzJmNjFhMjI1Y2VhMzI1MTEwYWQ3M2Q2Nzgx
10
+ Y2E0OGQ4ZTAxMmU0NmQ2ZGNkYjg2Njk3NGQ5NDQyNmQzNDQ5NTQ4OWI0Nzkx
11
+ ZTg1ODRkMTFhNzlkODQ4ZGYyMTkwOTMyNmNmNTExMDdmNzlmZTU=
12
12
  data.tar.gz: !binary |-
13
- MjhkZTIyMjdhMTE2NWVlMGYzN2UxYTg1ZWM2NDM4MjRiOWM1NDYzM2NhODM0
14
- N2NlM2NmMmNkNmEyYTY0MDMyMGViYzYwMjEwM2YzNmY4MmE4MWZjYWJmYjQ4
15
- NTA5MjA0MmU2NGMxM2JkZjk1MTc1NjQ3ZmQ0MDYzODZhODUyZTY=
13
+ YzA2MGMxZTUwZjRkMTVlNzBkOTIxMTI3MDBiZWY2MDZjMWE1NjA0OTBkYzZj
14
+ YWZmNTUxNWZiNzE4MTgyZGI3YzY1YjdiNzFiZGFlNjRjNDQyZGNkMWFkMjIx
15
+ YmRmZmVjYjBlNTA5YzI5NGM0NTgzYTNjOTIzMzAxODQzMzU2MDM=
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - jruby-19mode
5
+ before_install:
6
+ - gem update --system
7
+ script: "bundle exec rake"
8
+ jdk:
9
+ - openjdk6
10
+ branches:
11
+ only:
12
+ - master
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cookie_monster (0.1.1)
5
+ activesupport (>= 3.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activesupport (3.2.13)
11
+ i18n (= 0.6.1)
12
+ multi_json (~> 1.0)
13
+ i18n (0.6.1)
14
+ metaclass (0.0.1)
15
+ mocha (0.14.0)
16
+ metaclass (~> 0.0.1)
17
+ multi_json (1.7.7)
18
+ rake (10.0.4)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ cookie_monster!
25
+ mocha (~> 0.14.0)
26
+ rake (~> 10.0.4)
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ copyright (c) 2013 Dylan Griffin and Shareaholic, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # CookieMonster
2
+ [![Build Status](https://secure.travis-ci.org/griffindy/cookie_monster.png)](http://travis-ci.org/griffindy/cookie_monster?branch=master)
2
3
 
3
4
  This library exists to help you handle encrypted cookies in your app(s). The
4
5
  goal is for the implementation to be simple and dependency free so that it can
5
6
  be used anywhere.
6
7
 
7
- Install
8
+ Installation
8
9
  --------
9
10
 
10
11
  ```shell
@@ -17,16 +18,53 @@ gem 'cookie_monster'
17
18
  ```
18
19
  and run `bundle install` from your shell.
19
20
 
21
+ Configuration
22
+ -------------
23
+
24
+ `CookieMonster` is configured by passing a block to `CookieMonster.configure`
25
+ like the following:
26
+
27
+ ```ruby
28
+ CookieMonster.configure do |config|
29
+ config.key = '6e14e0255e472f84ef99df899cf9158aa3215a919db1ba0fa460eb928da3b34265a98d93bb4593762b0404494c7f1ab60f62b75eb'
30
+ end
31
+ ```
32
+
33
+ The object yielded by `CookieMonster.configure` accepts two methods: `key` and
34
+ `cipher_type`. `key` is the key that will unlock all the cookies, so keep this
35
+ secret! It should also be long. If you're using `CookieMonster` across multiple
36
+ apps, make sure that the key is the same. `cipher_type` defaults to
37
+ `'AES-256-CBC'`, but can be anything in the list of `OpenSSL::Cipher.ciphers`
38
+
20
39
  Usage
21
40
  -----
41
+ ### Rails
22
42
 
23
- If you're on rails, using CookieMonster is as easy as:
43
+ If you're on rails, using CookieMonster is as easy as including the appropriate
44
+ module into your controller:
24
45
 
25
46
  ```ruby
26
47
  include CookieMonster::Rails
27
48
  ```
28
49
 
29
- This includes a method `cookie_monster` which behaves just like `cookies`.
50
+ This includes a method `cookie_monster` which behaves just like `cookies`. You
51
+ can access cookies with `[]` and set them with `[]=`:
52
+
53
+ ```ruby
54
+ class SecretsController < ApplicationController
55
+ # ...
56
+ def update
57
+ cookie_monster[:super_secret_thing] = 'hopefully no one reads this'
58
+ end
59
+
60
+ def show
61
+ @secret = cookie_monster[:super_secret_thing]
62
+ end
63
+ # ...
64
+ end
65
+ ```
66
+
67
+ ### Another Ruby application
30
68
  Otherwise, you need to instantiate a new `CookieMonster::Jar` object with a
31
69
  `request` and `response` objects. `request` and `response` should both respond
32
70
  to `cookies`, and `response` should also respond to `set_cookie`. You can also
@@ -37,3 +75,30 @@ def cookie_monster
37
75
  @cookie_monster ||= CookieMonster::Jar.new(request: request, response: response, expires: 1.day.from_now)
38
76
  end
39
77
  ```
78
+
79
+ Supported Ruby Versions
80
+ -----------------------
81
+
82
+ `CookieMonster` requires at least Ruby 1.9. It is tested against Ruby 1.9.2,
83
+ \1.9.3, and JRuby in 19-mode.
84
+
85
+ Contributing
86
+ ------------
87
+
88
+ Contributing to `CookieMonster`:
89
+
90
+ 1. Fork the [official repository](https://github.com/shareaholic/cookie_monster/tree/master).
91
+ 2. Make your changes in a topic branch.
92
+ 3. Send a pull request.
93
+
94
+ Notes:
95
+
96
+ * Contributions without tests won't be accepted.
97
+ * Please don't update the Gem version.
98
+
99
+ License
100
+ -------
101
+
102
+ cookie_monster is Copyright © 2013 Dylan Griffin and Shareaholic, Inc.
103
+ It is free software, and may be redistributed under the terms specified
104
+ in the [LICENSE](https://github.com/shareaholic/cookie_monster/blob/master/LICENSE) file.
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH << File.expand_path("../lib", __FILE__)
2
2
  require 'cookie_monster/version'
3
+ require 'date'
3
4
 
4
5
  Gem::Specification.new do |s|
5
6
  s.name = 'cookie_monster'
@@ -10,5 +11,10 @@ Gem::Specification.new do |s|
10
11
  s.authors = ["Dylan Griffin"]
11
12
  s.email = 'dylan@dylangriff.in'
12
13
  s.files = `git ls-files`.split("\n")
13
- s.homepage = 'http://dylangriff.in'
14
+ s.homepage = 'https://github.com/shareaholic/cookie_monster'
15
+
16
+ s.add_dependency "activesupport", ">= 3.0.0"
17
+
18
+ s.add_development_dependency 'mocha', '~> 0.14.0'
19
+ s.add_development_dependency 'rake', '~> 10.0.4'
14
20
  end
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: ../../../cookie_monster
3
3
  specs:
4
- cookie_monster (0.0.0)
4
+ cookie_monster (0.1.1)
5
+ activesupport (>= 3.0.0)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
@@ -1,4 +1,3 @@
1
1
  CookieMonster.configure do |config|
2
2
  config.key = '47644314d297f3678dfd3ce757299b7cfa39181becd984c255728687c091d91df5e23e550f3d0d6625b9435e8b04ac804a27ea1978384b71ea2e1d678be90b9b'
3
- config.iv = '0.2767140515573705'
4
3
  end
@@ -1,9 +1,13 @@
1
1
  module CookieMonster
2
2
  class Configuration
3
- attr_accessor :key, :iv, :cipher_type
3
+ attr_accessor :key, :cipher_type
4
4
 
5
5
  def initialize
6
6
  @cipher_type = 'AES-256-CBC'
7
7
  end
8
+
9
+ def iv
10
+ rand.to_s
11
+ end
8
12
  end
9
13
  end
@@ -8,19 +8,45 @@ module CookieMonster
8
8
  end
9
9
 
10
10
  def encrypt
11
+ iv = configuration.iv
11
12
  @aes.encrypt
13
+ @aes.iv = iv
12
14
  @aes.key = @key
13
- @aes.update(@payload) + @aes.final
15
+ encrypted = iv + ':' + @aes.update(json_serialized_payload) + @aes.final
16
+ Base64.encode64 encrypted
14
17
  end
15
18
 
16
19
  def decrypt
20
+ payload = Base64.decode64 @payload
21
+ iv, payload = payload.split(':', 2)
17
22
  @aes.decrypt
23
+ @aes.iv = iv
18
24
  @aes.key = @key
19
- @aes.update(@payload) + @aes.final
25
+ decrypted = @aes.update(payload) + @aes.final
26
+ json_parsed_payload decrypted
20
27
  rescue OpenSSL::Cipher::CipherError
21
28
  raise PasswordInvalid, "Password incorrect!"
22
29
  end
23
30
 
31
+ private
32
+
33
+ def json_serialized_payload
34
+ if @payload.is_a? String
35
+ @payload
36
+ else
37
+ @payload.to_json
38
+ end
39
+ end
40
+
41
+ def json_parsed_payload(payload)
42
+ begin
43
+ parsed = JSON.parse payload
44
+ parsed.with_indifferent_access if parsed.is_a? Hash
45
+ rescue JSON::ParserError
46
+ payload
47
+ end
48
+ end
49
+
24
50
  class PasswordInvalid < Exception
25
51
  def initialize(s); s; end
26
52
  end
@@ -9,12 +9,13 @@ module CookieMonster
9
9
  end
10
10
 
11
11
  def [](key)
12
- key = key.to_s
12
+ response_cookies = @response.respond_to?(:cookies) ? @response.cookies.with_indifferent_access : {}
13
+ request_cookies = @request.respond_to?(:cookies) ? @request.cookies.with_indifferent_access : {}
13
14
 
14
- if @response.cookies[key]
15
- cookie = @response.cookies[key]
16
- elsif @request.cookies[key]
17
- cookie = @request.cookies[key]
15
+ if response_cookies[key]
16
+ cookie = response_cookies[key]
17
+ elsif request_cookies[key]
18
+ cookie = request_cookies[key]
18
19
  else
19
20
  return nil
20
21
  end
@@ -30,7 +31,7 @@ module CookieMonster
30
31
  def []=(key, value)
31
32
  encrypted_value = Encryption.new(value).encrypt
32
33
 
33
- @response.set_cookie key.to_s, {
34
+ @response.set_cookie key, {
34
35
  :value => encrypted_value,
35
36
  :httponly => @options[:httponly],
36
37
  :expires => @options[:expires],
@@ -1,3 +1,3 @@
1
1
  module CookieMonster
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -1,4 +1,9 @@
1
1
  require 'openssl'
2
+ require 'json'
3
+
4
+ require 'active_support'
5
+ require 'active_support/core_ext/hash'
6
+
2
7
  require 'cookie_monster/base'
3
8
  require 'cookie_monster/configuration'
4
9
  require 'cookie_monster/encryption'
@@ -7,4 +7,12 @@ class EncryptionTest < Test::Unit::TestCase
7
7
 
8
8
  assert_equal 'payload', decryptor
9
9
  end
10
+
11
+ def test_more_complicated_data_types
12
+ payload = { a: 'this is a hash' }.with_indifferent_access
13
+ encrypted = CookieMonster::Encryption.new(payload).encrypt
14
+ decryptor = CookieMonster::Encryption.new(encrypted).decrypt
15
+
16
+ assert_equal payload, decryptor
17
+ end
10
18
  end
@@ -29,9 +29,10 @@ class JarTest < Test::Unit::TestCase
29
29
  end
30
30
 
31
31
  def test_setting_a_cookie
32
+ CookieMonster.configuration.expects(:iv).at_least_once.returns('x' * 16)
32
33
  @jar[:hello] = 'testing a cookie'
33
34
  assert_equal CookieMonster::Encryption.new('testing a cookie').encrypt,
34
- @jar.response.cookies['hello'][:value]
35
+ @jar.response.cookies[:hello][:value]
35
36
  end
36
37
 
37
38
  def test_reading_a_cookie_set_in_response_first
@@ -42,4 +43,14 @@ class JarTest < Test::Unit::TestCase
42
43
  def test_reading_a_cookie_from_request
43
44
  assert_equal 'already set', @jar[:a_cookie]
44
45
  end
46
+
47
+ def test_indifferent_access
48
+ assert_equal @jar[:a_cookie], @jar['a_cookie']
49
+ end
50
+
51
+ def test_setting_a_more_complicated_data_type
52
+ hash = { a: 'this is a hash' }.with_indifferent_access
53
+ @jar[:something] = hash
54
+ assert_equal hash, @jar[:something]
55
+ end
45
56
  end
@@ -4,14 +4,16 @@ class CookieMonsterTest < Test::Unit::TestCase
4
4
  def test_configuration
5
5
  CookieMonster.configure do |config|
6
6
  config.key = 'a key'
7
- config.iv = 'iv'
8
7
  end
9
8
 
10
9
  assert_equal 'a key', CookieMonster.configuration.key
11
- assert_equal 'iv', CookieMonster.configuration.iv
12
10
  end
13
11
 
14
12
  def test_default_cipher_type
15
13
  assert_equal 'AES-256-CBC', CookieMonster.configuration.cipher_type
16
14
  end
15
+
16
+ def test_iv_not_the_same
17
+ assert_not_equal CookieMonster.configuration.iv, CookieMonster.configuration.iv
18
+ end
17
19
  end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,9 @@
1
+ $LOAD_PATH << File.expand_path("../../lib", __FILE__)
2
+
1
3
  require 'test/unit'
4
+ require 'mocha/setup'
2
5
  require 'cookie_monster'
3
6
 
4
7
  CookieMonster.configure do |config|
5
8
  config.key = 'a key'
6
- config.iv = 'an iv'
7
9
  end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cookie_monster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Griffin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-17 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2013-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: mocha
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.14.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.14.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 10.0.4
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 10.0.4
13
55
  description: A simple gem to encrypt cookies for ruby applications.
14
56
  email: dylan@dylangriff.in
15
57
  executables: []
@@ -17,6 +59,10 @@ extensions: []
17
59
  extra_rdoc_files: []
18
60
  files:
19
61
  - .gitignore
62
+ - .travis.yml
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE
20
66
  - README.md
21
67
  - Rakefile
22
68
  - cookie_monster.gemspec
@@ -81,7 +127,7 @@ files:
81
127
  - test/cookie_monster/test_jar.rb
82
128
  - test/test_cookie_monster.rb
83
129
  - test/test_helper.rb
84
- homepage: http://dylangriff.in
130
+ homepage: https://github.com/shareaholic/cookie_monster
85
131
  licenses: []
86
132
  metadata: {}
87
133
  post_install_message: