secret_token_migration 0.0.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.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +12 -0
- data/lib/secret_token_migration/action_dispatch/cookies.rb +34 -0
- data/lib/secret_token_migration/active_support/message_verifier.rb +19 -0
- data/lib/secret_token_migration/railtie.rb +19 -0
- data/lib/secret_token_migration/railties/application.rb +14 -0
- data/lib/secret_token_migration/version.rb +3 -0
- data/lib/secret_token_migration.rb +5 -0
- data/secret_token_migration.gemspec +24 -0
- data/test/action_dispatch/cookies_test.rb +41 -0
- data/test/railties/application_test.rb +26 -0
- data/test/test_helper.rb +14 -0
- metadata +96 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Tieg Zaharia
|
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,68 @@
|
|
1
|
+
# SecretTokenMigration
|
2
|
+
|
3
|
+
```
|
4
|
+
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
5
|
+
★ WARNING: this is intended for maintenance. If your `secret_token` was compromised, REPLACE IT instead of using this ★
|
6
|
+
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
7
|
+
```
|
8
|
+
|
9
|
+
The most basic convention for maintaining session state in Rails apps has been this:
|
10
|
+
|
11
|
+
``` ruby
|
12
|
+
def login(user)
|
13
|
+
session[:user_id] = user.id
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
The Rails session is just a cookie that includes a signed digest of the contents, using a `secret_token`.
|
18
|
+
|
19
|
+
But sometimes you need to change your `secret_token`, which would in these most basic cases logout most of your users.
|
20
|
+
|
21
|
+
This gem provides a graceful way to change your `secret_token` without logging out **most** of your active users.
|
22
|
+
|
23
|
+
## Example Situations
|
24
|
+
|
25
|
+
* You use the same `secret_token` in all of your environments, but decide that you want to separate them.
|
26
|
+
* Your `secret_token` was found logged in some third-party software, **but** you have no evidence that it was compromised, and want to change it.
|
27
|
+
* Regular swapping of `secret_token` as a precaution.
|
28
|
+
|
29
|
+
## How It Works
|
30
|
+
|
31
|
+
Regularly, you set a `MyApp::Application.config.secret_token` in your Rails app. By requiring this gem, setting `secret_token` to a new value, and including a `MyApp::Application.config.deprecated_secret_token` set to the old value, users with sessions digested with the old value will be gracefully transitioned to a session digested with the new value. When you feel comfortable that most active users have transitioned, you can remove this gem and the `deprecated_secret_token` config variable.
|
32
|
+
|
33
|
+
## Installation
|
34
|
+
|
35
|
+
Add this line to your application's Gemfile:
|
36
|
+
|
37
|
+
gem 'secret_token_migration'
|
38
|
+
|
39
|
+
And then execute:
|
40
|
+
|
41
|
+
$ bundle
|
42
|
+
|
43
|
+
Or install it yourself as:
|
44
|
+
|
45
|
+
$ gem install secret_token_migration
|
46
|
+
|
47
|
+
## Usage
|
48
|
+
|
49
|
+
1. In your `config/initializers/secret_token.rb` file, set the `secret_token` to a new, secure value.
|
50
|
+
2. Add this line:
|
51
|
+
`MyApp::Application.config.deprecated_secret_token = [THE_OLD_SECRET_TOKEN_VALUE]`
|
52
|
+
3. Optionally, add some instrumentation (described below) to track how many users have been transitioned.
|
53
|
+
|
54
|
+
## Instrumentation
|
55
|
+
|
56
|
+
You can also subscribe to `MessageVerifier` digesting to track when a deprecated secret_token is used (eg, for graphing):
|
57
|
+
|
58
|
+
``` ruby
|
59
|
+
ActiveSupport::Notifications.subscribe "deprecated_secret.active_support" do
|
60
|
+
Rails.logger.info "deprecated secret_token used"
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
## Reminder
|
65
|
+
|
66
|
+
If your `secret_token` **has** been compromised, don't bother with this library. You need to switch it ASAP and deal with all users being logged out.
|
67
|
+
|
68
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActionDispatch
|
2
|
+
class Cookies
|
3
|
+
DEPRECATED_TOKEN_KEY = "action_dispatch.deprecated_secret_token".freeze
|
4
|
+
|
5
|
+
class CookieJar
|
6
|
+
class << self
|
7
|
+
def build_with_deprecated_secret(request)
|
8
|
+
build_without_deprecated_secret(request).tap do |hash|
|
9
|
+
hash.instance_variable_set(:@deprecated_secret, request.env[DEPRECATED_TOKEN_KEY])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
alias_method_chain :build, :deprecated_secret # apologies
|
13
|
+
end
|
14
|
+
|
15
|
+
def signed
|
16
|
+
@signed ||= if @deprecated_secret
|
17
|
+
ActionDispatch::Cookies::SignedCookieWithDeprecatedTokenJar.new(self, @secret, {:deprecated_secret => @deprecated_secret})
|
18
|
+
else
|
19
|
+
ActionDispatch::Cookies::SignedCookieJar.new(self, @secret)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class SignedCookieWithDeprecatedTokenJar < ActionDispatch::Cookies::SignedCookieJar
|
25
|
+
def initialize(parent_jar, secret, options={})
|
26
|
+
ensure_secret_secure(secret)
|
27
|
+
ensure_secret_secure(options[:deprecated_secret])
|
28
|
+
@parent_jar = parent_jar
|
29
|
+
@verifier = ActiveSupport::MessageVerifier.new(secret, options)
|
30
|
+
@verifier.instance_variable_set(:@deprecated_secret, options[:deprecated_secret])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class ActiveSupport::MessageVerifier
|
2
|
+
def verify(signed_message)
|
3
|
+
raise InvalidSignature if signed_message.blank?
|
4
|
+
|
5
|
+
data, digest = signed_message.split("--")
|
6
|
+
if data.present? && digest.present? && secure_compare(digest, generate_digest(data))
|
7
|
+
@serializer.load(::Base64.decode64(data))
|
8
|
+
elsif data.present? && digest.present? && @deprecated_secret && secure_compare(digest, generate_deprecated_digest(data))
|
9
|
+
ActiveSupport::Notifications.instrument("deprecated_secret.active_support")
|
10
|
+
@serializer.load(::Base64.decode64(data))
|
11
|
+
else
|
12
|
+
raise InvalidSignature
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate_deprecated_digest(data)
|
17
|
+
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@digest).new, @deprecated_secret, data)
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'secret_token_migration'
|
2
|
+
|
3
|
+
module SecretTokenMigration
|
4
|
+
require 'rails'
|
5
|
+
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
initializer 'secret_token_migration.insert_into_application' do
|
8
|
+
ActiveSupport.on_load :before_configuration do
|
9
|
+
SecretTokenMigration::Railtie.insert
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.insert
|
14
|
+
require 'secret_token_migration/railties/application'
|
15
|
+
require 'secret_token_migration/action_dispatch/cookies'
|
16
|
+
require 'secret_token_migration/active_support/message_verifier'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Rails
|
2
|
+
class Application
|
3
|
+
class Configuration
|
4
|
+
attr_accessor :deprecated_secret_token
|
5
|
+
end
|
6
|
+
|
7
|
+
def env_config_with_deprecated_secret_token
|
8
|
+
@env_config ||= env_config_without_deprecated_secret_token.merge({
|
9
|
+
"action_dispatch.deprecated_secret_token" => config.deprecated_secret_token
|
10
|
+
})
|
11
|
+
end
|
12
|
+
alias_method_chain :env_config, :deprecated_secret_token
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'secret_token_migration/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "secret_token_migration"
|
8
|
+
gem.version = SecretTokenMigration::VERSION
|
9
|
+
gem.authors = ["Tieg Zaharia"]
|
10
|
+
gem.email = ["tieg.zaharia@gmail.com"]
|
11
|
+
gem.description = %q{A gem for the maintaneance of secret_tokens.}
|
12
|
+
gem.summary = %q{Easy way to switch to a secret_token while not
|
13
|
+
logging out most of your users.}
|
14
|
+
gem.homepage = "https://github.com/tiegz/secret_token_migration"
|
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 "rails", "~> 3.2.11"
|
22
|
+
|
23
|
+
gem.add_development_dependency "shoulda", "~> 3.3.2"
|
24
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
class CookiesTest < ActiveSupport::TestCase
|
4
|
+
context "with only regular token" do
|
5
|
+
setup do
|
6
|
+
@old_cookies = ActionDispatch::Cookies::CookieJar.build(ActionController::TestRequest.new({
|
7
|
+
"action_dispatch.secret_token" => "old" * 50
|
8
|
+
}))
|
9
|
+
|
10
|
+
@deprecated_cookies = ActionDispatch::Cookies::CookieJar.build(ActionController::TestRequest.new({
|
11
|
+
"action_dispatch.secret_token" => "new" * 50,
|
12
|
+
"action_dispatch.deprecated_secret_token" => "old" * 50
|
13
|
+
}))
|
14
|
+
|
15
|
+
@new_cookies = ActionDispatch::Cookies::CookieJar.build(ActionController::TestRequest.new({
|
16
|
+
"action_dispatch.secret_token" => "new" * 50,
|
17
|
+
}))
|
18
|
+
|
19
|
+
@old_cookies.signed["foo"] = "bar"
|
20
|
+
end
|
21
|
+
|
22
|
+
should "use correct class for signed cookies" do
|
23
|
+
assert_equal ActionDispatch::Cookies::SignedCookieJar,
|
24
|
+
@old_cookies.signed.class
|
25
|
+
assert_equal ActionDispatch::Cookies::SignedCookieWithDeprecatedTokenJar,
|
26
|
+
@deprecated_cookies.signed.class
|
27
|
+
assert_equal ActionDispatch::Cookies::SignedCookieJar,
|
28
|
+
@new_cookies.signed.class
|
29
|
+
end
|
30
|
+
|
31
|
+
should "read/write the old value with a deprecated token cookie" do
|
32
|
+
@deprecated_cookies["foo"] = @old_cookies["foo"] # without 'signed', fetches the digested value
|
33
|
+
assert_equal "bar", @deprecated_cookies.signed["foo"]
|
34
|
+
end
|
35
|
+
|
36
|
+
should "not read/write the old value with a new token cookie" do
|
37
|
+
@new_cookies["foo"] = @old_cookies["foo"] # without 'signed', fetches the digested value
|
38
|
+
assert_equal nil, @new_cookies.signed["foo"]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
class RailsApplicationTest < ActiveSupport::TestCase
|
4
|
+
context "a rails application" do
|
5
|
+
subject do
|
6
|
+
Class.new(Rails::Application).tap do |app|
|
7
|
+
app.config.deprecated_secret_token = "b"*128
|
8
|
+
app.config.secret_token = "a"*128
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
teardown do
|
13
|
+
Rails.application = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have secret_token" do
|
17
|
+
assert subject.env_config["action_dispatch.secret_token"].present?
|
18
|
+
assert_equal 128, subject.env_config["action_dispatch.secret_token"].length
|
19
|
+
end
|
20
|
+
|
21
|
+
should "have deprecated_secret_token" do
|
22
|
+
assert subject.env_config["action_dispatch.deprecated_secret_token"].present?
|
23
|
+
assert_equal 128, subject.env_config["action_dispatch.deprecated_secret_token"].length
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rails'
|
5
|
+
require 'active_support/test_case'
|
6
|
+
require 'action_controller/test_case'
|
7
|
+
require 'action_dispatch/testing/integration'
|
8
|
+
require 'shoulda'
|
9
|
+
require 'secret_token_migration'
|
10
|
+
|
11
|
+
# the railtie logic
|
12
|
+
require 'secret_token_migration/railties/application'
|
13
|
+
require 'secret_token_migration/action_dispatch/cookies'
|
14
|
+
require 'secret_token_migration/active_support/message_verifier'
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: secret_token_migration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tieg Zaharia
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.11
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.11
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: shoulda
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.3.2
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.3.2
|
46
|
+
description: A gem for the maintaneance of secret_tokens.
|
47
|
+
email:
|
48
|
+
- tieg.zaharia@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/secret_token_migration.rb
|
59
|
+
- lib/secret_token_migration/action_dispatch/cookies.rb
|
60
|
+
- lib/secret_token_migration/active_support/message_verifier.rb
|
61
|
+
- lib/secret_token_migration/railtie.rb
|
62
|
+
- lib/secret_token_migration/railties/application.rb
|
63
|
+
- lib/secret_token_migration/version.rb
|
64
|
+
- secret_token_migration.gemspec
|
65
|
+
- test/action_dispatch/cookies_test.rb
|
66
|
+
- test/railties/application_test.rb
|
67
|
+
- test/test_helper.rb
|
68
|
+
homepage: https://github.com/tiegz/secret_token_migration
|
69
|
+
licenses: []
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.24
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Easy way to switch to a secret_token while not logging out most of your
|
92
|
+
users.
|
93
|
+
test_files:
|
94
|
+
- test/action_dispatch/cookies_test.rb
|
95
|
+
- test/railties/application_test.rb
|
96
|
+
- test/test_helper.rb
|