straight_auth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in straight_auth.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Stefano Verna
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.
@@ -0,0 +1,44 @@
1
+ # StraightAuth
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'straight_auth'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ ## Usage
14
+
15
+ StraightAuth expects a `User` model with the following instance methods:
16
+
17
+ * `id`
18
+ * `password_digest=`
19
+
20
+ and the following class methods:
21
+
22
+ * find_by_email(email)
23
+ * find(id)
24
+
25
+ You need to include StraightAuth::Model in your `User` class, and call the provided
26
+ `encrypt_password` when needed. Ie.
27
+
28
+ ```ruby
29
+ class User < ActiveRecord::Base
30
+ include StraightAuth::Model
31
+ before_save :encrypt_password
32
+ end
33
+ ```
34
+
35
+ You also need to include StraightAuth::Helpers to your controllers, or, if you're
36
+ in a Sinatra environment, register `StraightAuth`.
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,61 @@
1
+ require "straight_auth/version"
2
+ require "bcrypt"
3
+
4
+ module StraightAuth
5
+ class Fail < RuntimeError; end
6
+
7
+ def self.registered(app)
8
+ app.helpers Helpers
9
+ end
10
+
11
+ module Helpers
12
+ def current_user
13
+ User.find(session[:user_id]) if session[:user_id]
14
+ end
15
+
16
+ def require_non_signed_in_user!
17
+ raise StraightAuth::Fail, "User is signed in" if current_user
18
+ end
19
+
20
+ def require_signed_in_user!
21
+ raise StraightAuth::Fail, "User is not signed in" unless current_user
22
+ end
23
+
24
+ def authenticate(email, unencrypted_password)
25
+ user = User.find_by_email(email)
26
+ if user && StraightAuth.check_digest(unencrypted_password, user.password_digest)
27
+ session[:user_id] = user.id
28
+ user
29
+ else
30
+ false
31
+ end
32
+ end
33
+ end
34
+
35
+ module Model
36
+ def self.included(klass)
37
+ klass.class_eval do
38
+ attr_accessor :password
39
+ end
40
+ end
41
+
42
+ def encrypt_password
43
+ if password && password.size > 0
44
+ self.password_digest = StraightAuth.digest(self.password)
45
+ end
46
+ end
47
+ end
48
+
49
+ def self.check_digest(password, digest)
50
+ if digest && digest.size > 0
51
+ BCrypt::Password.new(digest) == password
52
+ end
53
+ end
54
+
55
+ def self.digest(password)
56
+ if password && password.size > 0
57
+ BCrypt::Password.create(password)
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1,3 @@
1
+ module StraightAuth
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,90 @@
1
+ require "straight_auth"
2
+
3
+ class User
4
+ attr_accessor :password_digest
5
+ include StraightAuth::Model
6
+ end
7
+
8
+ describe StraightAuth do
9
+
10
+ describe StraightAuth::Helpers do
11
+ let(:controller) { Object.new.tap {|o| o.extend StraightAuth::Helpers } }
12
+ let(:user) { double('User', password_digest: 'digest', id: 'ID') }
13
+
14
+
15
+ describe "require_non_signed_in_user!" do
16
+ it "raises error if signed in" do
17
+ controller.stub(:current_user).and_return('user')
18
+ expect { controller.require_non_signed_in_user! }.to raise_error(StraightAuth::Fail)
19
+ end
20
+ end
21
+
22
+ describe "require_signed_in_user!" do
23
+ it "raises error if not signed in" do
24
+ controller.stub(:current_user).and_return(nil)
25
+ expect { controller.require_signed_in_user! }.to raise_error(StraightAuth::Fail)
26
+ end
27
+ end
28
+
29
+ describe '.authenticate!' do
30
+ context 'when email does not exist' do
31
+ it 'returns false' do
32
+ User.stub(:find_by_email).with('foo').and_return(nil)
33
+ controller.authenticate('foo', 'pass').should be_false
34
+ end
35
+ end
36
+
37
+ context 'when digest is incorrect' do
38
+ it 'returns false' do
39
+ User.stub(:find_by_email).with('foo').and_return(user)
40
+ StraightAuth.stub(:check_digest).with('pass', 'digest').and_return(false)
41
+ controller.authenticate('foo', 'pass').should be_false
42
+ end
43
+ end
44
+
45
+ context 'else' do
46
+ let(:session) { Hash.new }
47
+
48
+ before do
49
+ User.stub(:find_by_email).with('foo').and_return(user)
50
+ StraightAuth.stub(:check_digest).with('pass', 'digest').and_return(true)
51
+ controller.stub(:session).and_return(session)
52
+ end
53
+
54
+ it 'sets the user id inside session' do
55
+ controller.authenticate('foo', 'pass')
56
+ session[:user_id].should == 'ID'
57
+ end
58
+
59
+ it 'returns the user' do
60
+ controller.authenticate('foo', 'pass').should == user
61
+ end
62
+ end
63
+ end
64
+
65
+ describe StraightAuth::Model do
66
+ let(:user) { User.new }
67
+
68
+ describe ".encrypt_password" do
69
+ it "computes and stores password digest" do
70
+ StraightAuth.stub(:digest).with('foobar').and_return('digest')
71
+ user.password = 'foobar'
72
+ user.encrypt_password
73
+ user.password_digest.should == 'digest'
74
+ end
75
+ end
76
+ end
77
+
78
+ describe 'digest creation/check' do
79
+ example do
80
+ empty_digest = StraightAuth.digest(nil)
81
+ wrong_digest = StraightAuth.digest('barfoo')
82
+ digest = StraightAuth.digest('foobar')
83
+ StraightAuth.check_digest('foobar', digest).should be_true
84
+ StraightAuth.check_digest('foobar', empty_digest).should be_false
85
+ StraightAuth.check_digest('foobar', wrong_digest).should be_false
86
+ end
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'straight_auth/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "straight_auth"
8
+ gem.version = StraightAuth::VERSION
9
+ gem.authors = ["Stefano Verna"]
10
+ gem.email = ["stefano.verna@welaika.com"]
11
+ gem.description = %q{The dumbest authentication system you could think of}
12
+ gem.summary = %q{The dumbest authentication system you could think of}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "bcrypt-ruby"
21
+ gem.add_development_dependency "rspec"
22
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: straight_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stefano Verna
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bcrypt-ruby
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ description: The dumbest authentication system you could think of
47
+ email:
48
+ - stefano.verna@welaika.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/straight_auth.rb
59
+ - lib/straight_auth/version.rb
60
+ - spec/straight_auth_spec.rb
61
+ - straight_auth.gemspec
62
+ homepage: ''
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.23
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: The dumbest authentication system you could think of
86
+ test_files:
87
+ - spec/straight_auth_spec.rb
88
+ has_rdoc: