door_code 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .DS_Store
6
+ *.DS_Store
data/README.md CHANGED
@@ -11,7 +11,7 @@ Rubygems:
11
11
 
12
12
  Bundler:
13
13
 
14
- gem 'door_code', '~> 0.0.6'
14
+ gem 'door_code', '~> 0.0.8'
15
15
 
16
16
  ## Configuration
17
17
 
@@ -24,7 +24,6 @@ Optional options:
24
24
  use DoorCode::RestrictedAccess,
25
25
  :code => '12345', # set a single valid code
26
26
  :codes => ['12345','6789'], # set multiple valid codes
27
- :salt => "my super secret code" # use a custom salt for cookie encryption
28
27
 
29
28
  In application.rb (Rails3) or environment.rb (Rails2):
30
29
 
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "door_code"
6
- s.version = '0.0.7'
6
+ s.version = '0.0.8'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Mike Fulcher", "Alex Neill", "Spencer Steffen"]
9
9
  s.email = ["mike@plan9design.co.uk", "alex.neill@gmail.com", "spencer@citrusme.com"]
@@ -1,4 +1,20 @@
1
1
  module DoorCode
2
+
3
+ class << self
4
+
5
+ # Returns the salt or a random one
6
+ def salt
7
+ @salt ||= generate_random_salt
8
+ end
9
+
10
+ # Generate a random salt for the encryption
11
+ def generate_random_salt
12
+ o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
13
+ string = (0..50).map{ o[rand(o.length)] }.join
14
+ end
15
+
16
+ end
17
+
2
18
  class RestrictedAccess
3
19
 
4
20
  MIN_LENGTH = 3
@@ -8,7 +24,6 @@ module DoorCode
8
24
 
9
25
  def initialize app, options={}
10
26
  @app = app
11
- @salt = parse_salt(options[:salt])
12
27
  # The code or codes can be supplied as either a single string or an array using either
13
28
  # the ":code" or ":codes" key. ":codes" trumps ":code" if both are supplied
14
29
  @codes = options[:codes] ? parse_codes(options[:codes]) : parse_codes(options[:code])
@@ -24,7 +39,7 @@ module DoorCode
24
39
  parsed_codes << DEFAULT_CODE
25
40
  p "DoorCode: no valid codes detected - activating default code"
26
41
  end
27
- parsed_codes.compact.uniq.map { |c| Digest::SHA1.hexdigest("--#{@salt}--#{c}--") }
42
+ parsed_codes.compact.uniq.map { |c| Digest::SHA1.hexdigest("--#{salt}--#{c}--") }
28
43
  end
29
44
 
30
45
  # Checks that the code provided is valid, returning nil if not
@@ -43,12 +58,9 @@ module DoorCode
43
58
  parsed_code
44
59
  end
45
60
 
46
- # Ensures a salt is supplied, otherwise set to default
47
- def parse_salt(salt)
48
- if 0 < salt.to_s.length
49
- salt = Digest::SHA1.hexdigest("_door_code_secret_key")
50
- end
51
- salt
61
+ # Returns the salt or creates one
62
+ def salt
63
+ @salt ||= DoorCode.salt
52
64
  end
53
65
 
54
66
  # Name of the cookie
@@ -74,7 +86,7 @@ module DoorCode
74
86
 
75
87
  # Encrypted code supplied from user
76
88
  def supplied_code
77
- Digest::SHA1.hexdigest("--#{@salt}--#{request.params['code']}--")
89
+ Digest::SHA1.hexdigest("--#{salt}--#{request.params['code']}--")
78
90
  end
79
91
 
80
92
  # Is the supplied code valid for the current area
@@ -1,7 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  # '12345' encrypted with the default salt
4
- DEFAULT_CODE = '9fa483ac55e30318a84f0046365a21021a409117'
4
+ DEFAULT_CODE = Digest::SHA1.hexdigest("--#{DoorCode.salt}--#{DoorCode::RestrictedAccess::DEFAULT_CODE}--")
5
5
 
6
6
  class TestRestrictedAccess < Test::Unit::TestCase
7
7
 
@@ -45,7 +45,7 @@ class TestRestrictedAccess < Test::Unit::TestCase
45
45
  assert last_response.body.include?("Logged In")
46
46
  end
47
47
 
48
- should "logout" do
48
+ should "logout clearing cookie" do
49
49
  get "/logout"
50
50
  assert_equal 302, last_response.status
51
51
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: door_code
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.7
5
+ version: 0.0.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mike Fulcher
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2011-02-22 00:00:00 +00:00
15
+ date: 2011-03-06 00:00:00 +00:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  requirements: []
106
106
 
107
107
  rubyforge_project: door_code
108
- rubygems_version: 1.5.1
108
+ rubygems_version: 1.5.2
109
109
  signing_key:
110
110
  specification_version: 3
111
111
  summary: Restrict access to your site with a 3-6 digit PIN code