dm-is-authenticatable 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.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown --title 'dm-is-authenticatable Documentation' --protected --files ChangeLog.md,LICENSE.txt
data/ChangeLog.md ADDED
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2010-09-04
2
+
3
+ * Initial release.
4
+
data/Gemfile ADDED
@@ -0,0 +1,151 @@
1
+ # If you're working on more than one datamapper gem at a time, then it's
2
+ # recommended to create a local Gemfile and use this instead of the git
3
+ # sources. This will make sure that you are developing against your
4
+ # other local datamapper sources that you currently work on. Gemfile.local
5
+ # will behave identically to the standard Gemfile apart from the fact that
6
+ # it fetches the datamapper gems from local paths. This means that you can
7
+ # use the same environment variables, like ADAPTER(S) or PLUGIN(S) when
8
+ # running
9
+ # bundle commands. Gemfile.local is added to .gitignore, so you don't need
10
+ # to worry about accidentally checking local development paths into git.
11
+ # In order to create a local Gemfile, all you need to do is run:
12
+ #
13
+ # bundle exec rake local_gemfile
14
+ #
15
+ # This will give you a Gemfile.local file that points to your local clones
16
+ # of the various datamapper gems. It's assumed that all datamapper repo
17
+ # clones reside in the same directory. You can use the Gemfile.local like
18
+ # so for running any bundle command:
19
+ #
20
+ # BUNDLE_GEMFILE=Gemfile.local bundle foo
21
+ #
22
+ # You can also specify which adapter(s) should be part of the bundle by
23
+ # setting an environment variable. This of course also works when using the
24
+ # Gemfile.local
25
+ #
26
+ # bundle foo # dm-sqlite-adapter
27
+ #
28
+ # ADAPTER=mysql bundle foo # dm-mysql-adapter
29
+ #
30
+ # ADAPTERS=sqlite,mysql bundle foo # dm-sqlite-adapter, dm-mysql-adapter
31
+ #
32
+ # Of course you can also use the ADAPTER(S) variable when using the
33
+ # Gemfile.local and running specs against selected adapters.
34
+ #
35
+ # For easily working with adapters supported on your machine, it's
36
+ # recommended that you first install all adapters that you are planning to
37
+ # use or work on by doing something like
38
+ #
39
+ # ADAPTERS=sqlite,mysql,postgres bundle install
40
+ #
41
+ # This will clone the various repositories and make them available to
42
+ # bundler. Once you have them installed you can easily switch between
43
+ # adapters for the various development tasks. Running something like
44
+ #
45
+ # ADAPTER=mysql bundle exec rake spec
46
+ #
47
+ # will make sure that the dm-mysql-adapter is part of the bundle, and will
48
+ # be used when running the specs.
49
+ #
50
+ # You can also specify which plugin(s) should be part of the bundle by
51
+ # setting an environment variable. This also works when using the
52
+ # Gemfile.local
53
+ #
54
+ # bundle foo # dm-migrations
55
+ #
56
+ # PLUGINS=dm-validations bundle foo # dm-migrations,
57
+ # # dm-validations
58
+ #
59
+ # PLUGINS=dm-validations,dm-types bundle foo # dm-migrations,
60
+ # # dm-validations,
61
+ # # dm-types
62
+ #
63
+ # Of course you can combine the PLUGIN(S) and ADAPTER(S) env vars to run
64
+ # specs for certain adapter/plugin combinations.
65
+ #
66
+ # Finally, to speed up running specs and other tasks, it's recommended to
67
+ # run
68
+ #
69
+ # bundle lock
70
+ #
71
+ # after running 'bundle install' for the first time. This will make
72
+ # 'bundle exec' run a lot faster compared to the unlocked version. With an
73
+ # unlocked bundle you would typically just run 'bundle install' from time
74
+ # to time to fetch the latest sources from upstream. When you locked your
75
+ # bundle, you need to run
76
+ #
77
+ # bundle install --relock
78
+ #
79
+ # to make sure to fetch the latest updates and then lock the bundle again.
80
+ # Gemfile.lock is added to the .gitignore file, so you don't need to worry
81
+ # about accidentally checking it into version control.
82
+
83
+ source :rubygems
84
+
85
+ DATAMAPPER = 'http://github.com/datamapper'
86
+ DM_VERSION = '~> 1.0.0'
87
+ DO_VERSION = '~> 0.10.2'
88
+ DM_DO_ADAPTERS = %w[ sqlite postgres mysql oracle sqlserver ]
89
+ RAILS = 'http://github.com/rails/rails.git'
90
+
91
+ if ENV['EXTLIB']
92
+ gem 'extlib', '~> 0.9.15', :git => '#{DATAMAPPER}/extlib.git'
93
+ else
94
+ gem 'activesupport', '~> 3.0.0', :git => RAILS,
95
+ :branch => '3-0-stable',
96
+ :require => nil
97
+ end
98
+
99
+ gem 'bcrypt-ruby', '~> 2.1.0'
100
+
101
+ gem 'dm-core', DM_VERSION, :git => "#{DATAMAPPER}/dm-core.git"
102
+ gem 'dm-types', DM_VERSION, :git => "#{DATAMAPPER}/dm-types.git"
103
+ gem 'dm-validations', DM_VERSION, :git => "#{DATAMAPPER}/dm-validations.git"
104
+
105
+ group :development do
106
+ case RUBY_PLATFORM
107
+ when 'java'
108
+ gem 'maruku', '~> 0.6.0'
109
+ else
110
+ gem 'rdiscount', '~> 1.6.3'
111
+ end
112
+
113
+ gem 'rake', '~> 0.8.7'
114
+ gem 'ore', '~> 0.2.0'
115
+ gem 'ore-tasks', '~> 0.1.2'
116
+ gem 'rspec', '~> 2.0.0'
117
+ gem 'yard', '~> 0.6.0'
118
+ end
119
+
120
+ group :datamapper do
121
+ # We need this because we want to pin these dependencies to their git
122
+ # master sources
123
+
124
+ adapters = ENV['ADAPTER'] || ENV['ADAPTERS']
125
+ adapters = adapters.to_s.tr(',', ' ').split.uniq - %w[ in_memory ]
126
+
127
+ if (do_adapters = DM_DO_ADAPTERS & adapters).any?
128
+ options = {}
129
+ options[:git] = "#{DATAMAPPER}/do.git" if ENV['DO_GIT'] == 'true'
130
+
131
+ gem 'data_objects', DO_VERSION, options.dup
132
+
133
+ do_adapters.each do |adapter|
134
+ adapter = 'sqlite3' if adapter == 'sqlite'
135
+ gem "do_#{adapter}", DO_VERSION, options.dup
136
+ end
137
+
138
+ gem 'dm-do-adapter', DM_VERSION, :git => "#{DATAMAPPER}/dm-do-adapter.git"
139
+ end
140
+
141
+ adapters.each do |adapter|
142
+ gem "dm-#{adapter}-adapter", DM_VERSION, :git => "#{DATAMAPPER}/dm-#{adapter}-adapter.git"
143
+ end
144
+
145
+ plugins = ENV['PLUGINS'] || ENV['PLUGIN']
146
+ plugins = plugins.to_s.tr(',', ' ').split.push('dm-migrations').uniq
147
+
148
+ plugins.each do |plugin|
149
+ gem plugin, DM_VERSION, :git => "#{DATAMAPPER}/#{plugin}.git"
150
+ end
151
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2010 Hal Brodigan
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,69 @@
1
+ # dm-is-authenticatable
2
+
3
+ * [github.com/postmodern/dm-is-authenticatable](http://github.com/postmodern/dm-is-authenticatable)
4
+ * [github.com/postmodern/dm-is-authenticatable/issues](http://github.com/postmodern/dm-is-authenticatable/issues)
5
+ * Postmodern (postmodern.mod3 at gmail.com)
6
+
7
+ ## Description
8
+
9
+ A DataMapper plugin for adding authentication and encrypted passwords to
10
+ your DataMapper models. Ideal for use with
11
+ [warden](http://github.com/hassox/warden) or
12
+ [sinatra_warden](http://github.com/jsmestad/sinatra_warden).
13
+
14
+ ## Example
15
+
16
+ require 'dm-core'
17
+ require 'dm-is-authenticatable'
18
+
19
+ class User
20
+
21
+ include DataMapper::Resource
22
+
23
+ is :authenticatable
24
+
25
+ # Name of the Licence
26
+ property :name, String
27
+
28
+ end
29
+
30
+ user = User.new(:name => 'bob')
31
+ user.password = 'secret'
32
+ # => "secret"
33
+
34
+ Validates confirmation of the password:
35
+
36
+ user.valid?
37
+ # => false
38
+
39
+ user.password_confirmation = 'secret'
40
+ # => "secret"
41
+
42
+ user.valid?
43
+ # => true
44
+
45
+ Uses BCryptHash by default:
46
+
47
+ user.encrypted_password
48
+ # => "$2a$10$kC./7/ClA7mJwqqWhO02hu7//ybbsn7QKi4p5PZN0R1.XeQ/oYBAC"
49
+
50
+ Handles finding and authenticating resources in the database:
51
+
52
+ user.save
53
+ # => true
54
+
55
+ User.authenticate(:name => 'bob', :password => 'secret')
56
+ # => #<User: ...>
57
+
58
+ ## Requirements
59
+
60
+ * [dm-core](http://github.com/datamapper/dm-core/) ~> 1.0.0
61
+
62
+ ## Install
63
+
64
+ $ sudo gem install dm-is-authenticatable
65
+
66
+ ## License
67
+
68
+ See {file:LICENSE.txt} for license information.
69
+
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+
3
+ begin
4
+ require 'bundler'
5
+ rescue LoadError => e
6
+ STDERR.puts e.message
7
+ STDERR.puts "Run `gem install bundler` to install Bundler."
8
+ exit e.status_code
9
+ end
10
+
11
+ begin
12
+ Bundler.setup(:development)
13
+ rescue Bundler::BundlerError => e
14
+ STDERR.puts e.message
15
+ STDERR.puts "Run `bundle install` to install missing gems"
16
+ exit e.status_code
17
+ end
18
+
19
+ require 'rake'
20
+
21
+ require 'ore/tasks'
22
+ Ore::Tasks.new
23
+
24
+ require 'rspec/core/rake_task'
25
+ RSpec::Core::RakeTask.new
26
+ task :default => :spec
27
+
28
+ require 'yard'
29
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,10 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ begin
4
+ Ore::Specification.new do |gemspec|
5
+ # custom logic here
6
+ end
7
+ rescue NameError
8
+ STDERR.puts "The 'dm-is-authenticatable.gemspec' file requires Ore."
9
+ STDERR.puts "Run `gem install ore` to install Ore."
10
+ end
data/gemspec.yml ADDED
@@ -0,0 +1,22 @@
1
+ name: dm-is-authenticatable
2
+ version: 0.1.0
3
+ summary: DataMapper plugin for adding authentication to models.
4
+ description:
5
+ A DataMapper plugin for adding authentication and encrypted passwords to
6
+ your DataMapper models.
7
+
8
+ license: MIT
9
+ authors: Postmodern
10
+ email: postmodern.mod3@gmail.com
11
+ homepage: http://github.com/postmodern/dm-is-authenticatable
12
+ has_yard: true
13
+
14
+ dependencies:
15
+ bcrypt-ruby: ~> 2.1.0
16
+ dm-core: ~> 1.0.0
17
+ dm-types: ~> 1.0.0
18
+ dm-validations: ~> 1.0.0
19
+
20
+ development_dependencies:
21
+ bundler: ~> 1.0.0
22
+ yard: ~> 0.6.0
@@ -0,0 +1,80 @@
1
+ require 'dm-types/bcrypt_hash'
2
+ require 'dm-validations'
3
+
4
+ module DataMapper
5
+ module Is
6
+ module Authenticatable
7
+ class UnknownResource < RuntimeError
8
+ end
9
+
10
+ def is_authenticatable
11
+ # The encrypted password
12
+ property :encrypted_password, DataMapper::Property::BCryptHash
13
+
14
+ extend DataMapper::Is::Authenticatable::ClassMethods
15
+ include DataMapper::Is::Authenticatable::InstanceMethods
16
+
17
+ validates_confirmation_of :password
18
+ end
19
+
20
+ module ClassMethods
21
+ #
22
+ # Finds and authenticates a resource.
23
+ #
24
+ # @param [Hash] attributes
25
+ # The attributes to search with.
26
+ #
27
+ # @option attributes [String] :password
28
+ # The clear-text password to authenticate with.
29
+ #
30
+ # @return [DataMapper::Resource]
31
+ # The authenticated resource.
32
+ #
33
+ # @raise [UnknownResource]
34
+ # The authenticatable resource could not be found in the repository.
35
+ #
36
+ # @raise [ArgumentError]
37
+ # The `:password` option was not specified.
38
+ #
39
+ def authenticate(attributes)
40
+ password = attributes.delete(:password)
41
+ resource = self.first(attributes)
42
+
43
+ unless resource
44
+ raise(UnknownResource,"could not find the authenticatable resource",caller)
45
+ end
46
+
47
+ unless password
48
+ raise(ArgumentError,"must specify the :password option",caller)
49
+ end
50
+
51
+ if resource.encrypted_password == password
52
+ resource
53
+ end
54
+ end
55
+ end
56
+
57
+ module InstanceMethods
58
+ # The clear-text password
59
+ attr_reader :password
60
+
61
+ # The confirmed clear-text password
62
+ attr_accessor :password_confirmation
63
+
64
+ #
65
+ # Updates the password of the resource.
66
+ #
67
+ # @param [String] new_password
68
+ # The new password for the resource.
69
+ #
70
+ # @return [String]
71
+ # The new password of the resource.
72
+ #
73
+ def password=(new_password)
74
+ self.encrypted_password = new_password
75
+ @password = new_password
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,4 @@
1
+ require 'dm-core'
2
+ require 'dm-is-authenticatable/is/authenticatable'
3
+
4
+ DataMapper::Model.append_extensions DataMapper::Is::Authenticatable
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ require 'integration/models/user'
4
+
5
+ describe DataMapper::Is::Authenticatable do
6
+ before(:all) do
7
+ User.auto_migrate!
8
+ end
9
+
10
+ let(:password) { 'secret' }
11
+
12
+ subject { User.new(:name => 'bob') }
13
+
14
+ it "should define the encrypted_password property" do
15
+ subject.class.properties.should be_named(:encrypted_password)
16
+ end
17
+
18
+ it "should not have a default password" do
19
+ subject.password.should be_nil
20
+ end
21
+
22
+ it "should not have an encrypted password by default" do
23
+ subject.encrypted_password.should be_nil
24
+ end
25
+
26
+ it "should allow setting the password" do
27
+ subject.password = password
28
+ subject.password.should == password
29
+ end
30
+
31
+ it "should update the encrypted password when setting the password" do
32
+ subject.password = password
33
+ subject.encrypted_password.should == password
34
+ end
35
+
36
+ it "should require password confirmation" do
37
+ subject.password = password
38
+ subject.should_not be_valid
39
+ end
40
+
41
+ it "should require the confirmation password match the password" do
42
+ subject.password = password
43
+ subject.password_confirmation = 'fail'
44
+
45
+ subject.should_not be_valid
46
+ end
47
+
48
+ it "should validate confirmed passwords" do
49
+ subject.password = password
50
+ subject.password_confirmation = password
51
+
52
+ subject.should be_valid
53
+ end
54
+
55
+ describe "authenticate" do
56
+ before(:all) do
57
+ user = User.new(:name => 'joe')
58
+ user.password = password
59
+ user.save!
60
+ end
61
+
62
+ let(:name) { 'joe' }
63
+
64
+ subject { User }
65
+
66
+ it "should not allow authenticating with unknown resources" do
67
+ lambda {
68
+ subject.authenticate(:name => 'alice', :password => password)
69
+ }.should raise_error(DataMapper::Is::Authenticatable::UnknownResource)
70
+ end
71
+
72
+ it "should allow authenticating with a password" do
73
+ user = subject.authenticate(:name => name, :password => password)
74
+ user.name.should == name
75
+ end
76
+
77
+ it "should not authenticate with an incorrect password" do
78
+ user = subject.authenticate(:name => name, :password => 'fail')
79
+
80
+ user.should be_nil
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,14 @@
1
+ require 'dm-core'
2
+ require 'dm-is-authenticatable'
3
+
4
+ class User
5
+
6
+ include DataMapper::Resource
7
+
8
+ is :authenticatable
9
+
10
+ property :id, Serial
11
+
12
+ property :name, String
13
+
14
+ end
@@ -0,0 +1,11 @@
1
+ require 'rspec'
2
+ require 'dm-core/spec/setup'
3
+ require 'dm-core/spec/lib/adapter_helpers'
4
+
5
+ require 'dm-is-authenticatable'
6
+
7
+ DataMapper::Spec.setup
8
+
9
+ RSpec.configure do |config|
10
+ config.extend(DataMapper::Spec::Adapters::Helpers)
11
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-is-authenticatable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Postmodern
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-05 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bcrypt-ruby
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 1
30
+ - 0
31
+ version: 2.1.0
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: dm-core
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 0
45
+ - 0
46
+ version: 1.0.0
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: dm-types
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 0
60
+ - 0
61
+ version: 1.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: dm-validations
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 1
74
+ - 0
75
+ - 0
76
+ version: 1.0.0
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: bundler
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 1
89
+ - 0
90
+ - 0
91
+ version: 1.0.0
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: yard
97
+ requirement: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ segments:
103
+ - 0
104
+ - 6
105
+ - 0
106
+ version: 0.6.0
107
+ type: :development
108
+ prerelease: false
109
+ version_requirements: *id006
110
+ description: A DataMapper plugin for adding authentication and encrypted passwords to your DataMapper models.
111
+ email: postmodern.mod3@gmail.com
112
+ executables: []
113
+
114
+ extensions: []
115
+
116
+ extra_rdoc_files:
117
+ - README.md
118
+ files:
119
+ - .rspec
120
+ - .yardopts
121
+ - ChangeLog.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - dm-is-authenticatable.gemspec
127
+ - gemspec.yml
128
+ - lib/dm-is-authenticatable.rb
129
+ - lib/dm-is-authenticatable/is/authenticatable.rb
130
+ - spec/integration/authenticatable_spec.rb
131
+ - spec/integration/models/user.rb
132
+ - spec/spec_helper.rb
133
+ has_rdoc: yard
134
+ homepage: http://github.com/postmodern/dm-is-authenticatable
135
+ licenses:
136
+ - MIT
137
+ post_install_message:
138
+ rdoc_options: []
139
+
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ segments:
148
+ - 0
149
+ version: "0"
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ segments:
156
+ - 1
157
+ - 3
158
+ - 6
159
+ version: 1.3.6
160
+ requirements: []
161
+
162
+ rubyforge_project: dm-is-authenticatable
163
+ rubygems_version: 1.3.7
164
+ signing_key:
165
+ specification_version: 3
166
+ summary: DataMapper plugin for adding authentication to models.
167
+ test_files:
168
+ - spec/integration/authenticatable_spec.rb