ya 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -3,6 +3,22 @@
3
3
  Ya is a simple authentication module that uses a YAML flat file as its directory
4
4
  store.
5
5
 
6
+ == Synopsis
7
+
8
+ Access = Ya.open(Rails.root + 'config' + 'access.yml')
9
+
10
+ Access.add('username', 'password')
11
+ Access.authenticate('username', 'fails')
12
+ # => false
13
+ Access.authenticate('nobody', 'fails')
14
+ # => nil
15
+ Access.authenticate('username', 'password')
16
+ # => { :username => { :salt => ?, :hash => ? } }
17
+
18
+ Access.mod('username', :meta => 'awesome')
19
+ Access.lookup('username')
20
+ # => { :username => { :salt => ?, :hash => ?, :meta => 'awesome' } }
21
+
6
22
  == Copyright
7
23
 
8
24
  Copyright (c) 2009 Carsten Nielsen. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/lib/ya/digest.rb CHANGED
@@ -14,13 +14,13 @@ module Ya
14
14
  ::Digest::SHA1.hexdigest("#{Time.now}-#{rand RANDOM_SEED}")
15
15
  end
16
16
 
17
- def generate(login, password, attrs = {})
17
+ def salt_and_hash(password)
18
18
  salt = random_hash
19
19
  hash = salt_password(salt, password)
20
- { :"#{login}" => { :salt => salt, :hash => hash }.merge(attrs) }
20
+ { :salt => salt, :hash => hash }
21
21
  end
22
22
 
23
- module_function :salt_password, :random_hash, :generate
23
+ module_function :salt_password, :random_hash, :salt_and_hash
24
24
 
25
25
  end
26
26
  end
data/lib/ya/directory.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  module Ya
2
2
  class Directory
3
3
 
4
- def initialize(access_file)
5
- @access_file = access_file
6
- FileUtils.touch(@access_file)
4
+ def initialize(directory_file)
5
+ @directory_file = directory_file
6
+ FileUtils.touch(@directory_file)
7
7
  end
8
8
 
9
9
  def lookup(login)
@@ -18,7 +18,19 @@ module Ya
18
18
  end
19
19
 
20
20
  def add(login, password, attrs = {})
21
- dump(load.merge(Digest.generate(login, password, attrs)))
21
+ dump_record(login.to_sym => Digest.salt_and_hash(password).merge(attrs))
22
+ end
23
+
24
+ def mod(login, attrs = {})
25
+ record = lookup(login)
26
+ return nil unless record
27
+ dump_record(login.to_sym => record.merge(attrs))
28
+ end
29
+
30
+ def reset(login, new_password)
31
+ record = lookup(login)
32
+ return nil unless record
33
+ dump_record(login.to_sym => record.merge(Digest.salt_and_hash(new_password)))
22
34
  end
23
35
 
24
36
  def remove(login)
@@ -28,12 +40,16 @@ module Ya
28
40
 
29
41
  private
30
42
 
43
+ def dump_record(record)
44
+ dump(load.merge(record))
45
+ end
46
+
31
47
  def load
32
- YAML.load_file(@access_file) || {}
48
+ YAML.load_file(@directory_file) || {}
33
49
  end
34
50
 
35
51
  def dump(directory)
36
- File.open(@access_file, 'w') { |f| f.puts(directory.to_yaml) }
52
+ File.open(@directory_file, 'w') { |f| f.puts(directory.to_yaml) }
37
53
  end
38
54
 
39
55
  end
data/test/test_ya.rb CHANGED
@@ -33,6 +33,42 @@ class TestYa < Test::Unit::TestCase
33
33
  FileUtils.rm('test.yml')
34
34
  end
35
35
 
36
+ context '#reset (for existing record)' do
37
+ setup { @directory.reset('heycarsten', 'passnerd') }
38
+
39
+ should 'change the password' do
40
+ assert @directory.authenticate('heycarsten', 'passnerd')
41
+ end
42
+
43
+ should 'not change anything else' do
44
+ assert_equal 'data', @directory.lookup('heycarsten')[:meta]
45
+ end
46
+ end
47
+
48
+ context '#reset (for nonexistant record)' do
49
+ setup { @directory.reset('nobody', 'password') }
50
+
51
+ should 'do nothing' do
52
+ assert_nil @directory.lookup('nobody')
53
+ end
54
+ end
55
+
56
+ context '#mod (for existing record)' do
57
+ setup { @directory.mod('heycarsten', :meta => 'face') }
58
+
59
+ should 'update the record' do
60
+ assert_equal 'face', @directory.lookup('heycarsten')[:meta]
61
+ end
62
+ end
63
+
64
+ context '#mod (for nonexistant record)' do
65
+ setup { @directory.mod('nobody', :meta => 'value') }
66
+
67
+ should 'do nothing' do
68
+ assert_nil @directory.lookup('nobody')
69
+ end
70
+ end
71
+
36
72
  context '#remove (for a record that exists)' do
37
73
  setup { @directory.remove('heycarsten') }
38
74
 
@@ -75,10 +111,9 @@ class TestYa < Test::Unit::TestCase
75
111
  context 'Ya::Digest' do
76
112
  context '.generate' do
77
113
  should 'return a hash representing a new record' do
78
- record = Ya::Digest.generate('username', 'password', :extra => 'stuff')
79
- assert_equal 40, record[:username][:salt].length
80
- assert_equal 40, record[:username][:hash].length
81
- assert_equal 'stuff', record[:username][:extra]
114
+ h = Ya::Digest.salt_and_hash('password')
115
+ assert_equal 40, h[:salt].length
116
+ assert_equal 40, h[:hash].length
82
117
  end
83
118
  end
84
119
 
data/ya.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ya}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Carsten Nielsen"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ya
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Nielsen