sequel_secure_password 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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --order rand
3
+ --fail-fast
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ script: bundle exec rspec
3
+ rvm:
4
+ - 1.9.3
5
+ - 1.8.7
6
+ - jruby-18mode
7
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sequel_secure_password.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Mateusz Lenik
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,54 @@
1
+ # Sequel secure_password [![Build Status](https://secure.travis-ci.org/mlen/sequel_secure_password.png)](http://travis-ci.org/mlen/sequel_secure_password)
2
+
3
+ Plugin adds BCrypt authentication and password hashing to Sequel models.
4
+ Model using this plugin should have `password_digest` field.
5
+
6
+ This plugin was created by extracting `has_secure_password` strategy from rails.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'sequel_secure_password'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install sequel_secure_password
21
+
22
+ ## Usage
23
+
24
+ Plugin should be used in subclasses of `Sequel::Model`. The model should have
25
+ `password_digest` attribute in database.
26
+ __Always__ call super in `validate` method of your model, otherwise password
27
+ validations won't be executed.
28
+ It __does not__ `set_allowed_columns` and mass assignment policy must be managed
29
+ separately.
30
+
31
+ Example model:
32
+
33
+ class User < Sequel::Model
34
+ plugin :secure_password
35
+ end
36
+
37
+ user = User.new
38
+ user.password = "foo"
39
+ user.password_confirmation = "bar"
40
+ user.valid? # => false
41
+
42
+ user.password_confirmation = "foo"
43
+ user.valid? # => true
44
+
45
+ user.authenticate("foo") # => user
46
+ user.authenticate("bar") # => nil
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ module SequelSecurePassword
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ require "sequel_secure_password/version"
2
+ require "bcrypt"
3
+
4
+ module Sequel
5
+ module Plugins
6
+ module SecurePassword
7
+ module InstanceMethods
8
+ attr_accessor :password_confirmation
9
+ attr_reader :password
10
+
11
+ def password=(unencrypted)
12
+ @password = unencrypted
13
+ unless blank? unencrypted
14
+ self.password_digest = BCrypt::Password.create(unencrypted)
15
+ end
16
+ end
17
+
18
+ def authenticate(unencrypted)
19
+ if BCrypt::Password.new(password_digest) == unencrypted
20
+ self
21
+ end
22
+ end
23
+
24
+ def validate
25
+ super
26
+
27
+ errors.add :password_digest, 'is not present' if blank? password_digest
28
+ errors.add :password, 'has no confirmation' if password != password_confirmation
29
+ end
30
+
31
+ private
32
+ def blank?(string)
33
+ string.nil? or string == /\A\s*\z/
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sequel_secure_password/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sequel_secure_password"
8
+ gem.version = SequelSecurePassword::VERSION
9
+ gem.authors = ["Mateusz Lenik"]
10
+ gem.email = ["mt.lenik@gmail.com"]
11
+ gem.description = %q{Plugin adds authentication methods to Sequel models using BCrypt library.}
12
+ gem.summary = <<EOF
13
+ Plugin adds BCrypt authentication and password hashing to Sequel models.
14
+ Model using this plugin should have 'password_digest' field.
15
+
16
+ This plugin was created by extracting has_secure_password strategy from rails.
17
+ EOF
18
+ gem.homepage = "http://github.com/mlen/sequel_secure_password"
19
+
20
+ gem.files = `git ls-files`.split($/)
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ["lib"]
23
+
24
+ gem.add_dependency 'bcrypt-ruby', '~> 3.0.0'
25
+ gem.add_dependency 'sequel', '~> 3.40.0'
26
+
27
+ gem.add_development_dependency 'rspec', '~> 2.11.0'
28
+ gem.add_development_dependency 'rake', '~> 0.9.0'
29
+ if RUBY_PLATFORM == "java"
30
+ gem.add_development_dependency 'jdbc-sqlite3', '~> 3.7.2'
31
+ else
32
+ gem.add_development_dependency 'sqlite3', '~> 1.3.0'
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe "model with Sequel::Plugins::SecurePassword" do
4
+ subject { User.new }
5
+
6
+ it "is invalid with blank password" do
7
+ subject.password = ""
8
+ subject.should_not be_valid
9
+ end
10
+
11
+ it "is invalid with nil password" do
12
+ subject.password = nil
13
+ subject.should_not be_valid
14
+ end
15
+
16
+ it "is invalid without a password" do
17
+ subject.should_not be_valid
18
+ end
19
+
20
+ it "is valid with password matching confirmation" do
21
+ subject.password = "foo"
22
+ subject.password_confirmation = "foo"
23
+
24
+ subject.should be_valid
25
+ end
26
+
27
+ it "is invalid without password matching confirmation" do
28
+ subject.password = "foo"
29
+ subject.password_confirmation = "bar"
30
+
31
+ subject.should_not be_valid
32
+ end
33
+
34
+ it "returns user when authentication is successful" do
35
+ subject.password = "foo"
36
+ subject.authenticate("foo").should be subject
37
+ end
38
+
39
+ it "returns nil when authentication fails" do
40
+ subject.password = "foo"
41
+ subject.authenticate("bar").should be nil
42
+ end
43
+
44
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+ require 'sequel'
5
+ require 'sequel_secure_password'
6
+
7
+ adapter = RUBY_PLATFORM == "java" ? 'jdbc:sqlite::memory:' : 'sqlite:/'
8
+
9
+ RSpec.configure do |c|
10
+ c.before :suite do
11
+ Sequel::Model.plugin(:schema)
12
+ Sequel.connect adapter
13
+
14
+ class User < Sequel::Model
15
+ set_schema do
16
+ primary_key :id
17
+ varchar :password_digest
18
+ end
19
+
20
+ plugin :secure_password
21
+ end
22
+
23
+ User.create_table!
24
+ end
25
+
26
+ c.around :each do |example|
27
+ Sequel::Model.db.transaction(:rollback => :always) { example.run }
28
+ end
29
+ end
30
+
31
+
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_secure_password
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Mateusz Lenik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bcrypt-ruby
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 3.0.0
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ none: false
28
+ prerelease: false
29
+ type: :runtime
30
+ - !ruby/object:Gem::Dependency
31
+ name: sequel
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: 3.40.0
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: 3.40.0
43
+ none: false
44
+ prerelease: false
45
+ type: :runtime
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: 2.11.0
53
+ none: false
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ version: 2.11.0
59
+ none: false
60
+ prerelease: false
61
+ type: :development
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.0
69
+ none: false
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: 0.9.0
75
+ none: false
76
+ prerelease: false
77
+ type: :development
78
+ - !ruby/object:Gem::Dependency
79
+ name: jdbc-sqlite3
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ version: 3.7.2
85
+ none: false
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ~>
89
+ - !ruby/object:Gem::Version
90
+ version: 3.7.2
91
+ none: false
92
+ prerelease: false
93
+ type: :development
94
+ description: Plugin adds authentication methods to Sequel models using BCrypt library.
95
+ email:
96
+ - mt.lenik@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - .travis.yml
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - lib/sequel_secure_password.rb
109
+ - lib/sequel_secure_password/version.rb
110
+ - sequel_secure_password.gemspec
111
+ - spec/secure_password_spec.rb
112
+ - spec/spec_helper.rb
113
+ homepage: http://github.com/mlen/sequel_secure_password
114
+ licenses: []
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: !binary |-
124
+ MA==
125
+ none: false
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: !binary |-
131
+ MA==
132
+ none: false
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 1.8.24
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: Plugin adds BCrypt authentication and password hashing to Sequel models. Model using this plugin should have 'password_digest' field. This plugin was created by extracting has_secure_password strategy from rails.
139
+ test_files:
140
+ - spec/secure_password_spec.rb
141
+ - spec/spec_helper.rb