di-simple_auth 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +54 -0
- data/VERSION.yml +4 -0
- data/lib/simple_auth/authenticated.rb +109 -0
- data/lib/simple_auth/authentication.rb +122 -0
- data/lib/simple_auth.rb +2 -0
- data/simple_auth.gemspec +59 -0
- data/test/helper.rb +10 -0
- data/test/test_simple_auth.rb +7 -0
- metadata +98 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Dieinzige
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= simple_auth
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Dieinzige. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "di-simple_auth"
|
8
|
+
gem.summary = %Q{ one-line summary of your gem}
|
9
|
+
gem.description = %Q{ longer description of your gem}
|
10
|
+
gem.email = "dieinzige@me.com"
|
11
|
+
gem.homepage = "http://github.com/Dieinzige/simple_auth"
|
12
|
+
gem.authors = ["Dieinzige"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
gem.add_dependency "bcrypt-ruby"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "simple_auth #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'bcrypt'
|
2
|
+
module SimpleAuth
|
3
|
+
module Authenticated
|
4
|
+
# Hook for all Clearance::User modules.
|
5
|
+
#
|
6
|
+
# If you need to override parts of Clearance::User,
|
7
|
+
# extend and include à la carte.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# extend ClassMethods
|
11
|
+
# include InstanceMethods
|
12
|
+
# include AttrAccessor
|
13
|
+
# include Callbacks
|
14
|
+
#
|
15
|
+
# @see ClassMethods
|
16
|
+
# @see InstanceMethods
|
17
|
+
# @see AttrAccessible
|
18
|
+
# @see AttrAccessor
|
19
|
+
# @see Validations
|
20
|
+
# @see Callbacks
|
21
|
+
def self.included(model)
|
22
|
+
model.extend(ClassMethods)
|
23
|
+
model.send(:include , AttrAccessor)
|
24
|
+
model.send(:include, InstanceMethods)
|
25
|
+
model.send(:include, Callbacks)
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
module Callbacks
|
31
|
+
def self.included(model)
|
32
|
+
model.class_eval do
|
33
|
+
before_save :initialize_salt,
|
34
|
+
:encrypt_password
|
35
|
+
before_create :generate_confirmation_token , :generate_remember_token
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
module AttrAccessor
|
42
|
+
# Hook for attr_accessor virtual attributes.
|
43
|
+
#
|
44
|
+
# :password
|
45
|
+
def self.included(model)
|
46
|
+
model.class_eval do
|
47
|
+
attr_accessor :password
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
module InstanceMethods
|
52
|
+
|
53
|
+
def authenticated?(password)
|
54
|
+
encrypted_password == encrypt(password)
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
def confirm!
|
60
|
+
self.confirmed = true
|
61
|
+
self.confirmation_token = nil
|
62
|
+
save(:validate => false)
|
63
|
+
end
|
64
|
+
|
65
|
+
def reset_remember_token!
|
66
|
+
generate_remember_token
|
67
|
+
save(:validate => false)
|
68
|
+
end
|
69
|
+
|
70
|
+
def forgot_password!
|
71
|
+
generate_confirmation_token
|
72
|
+
save(:validate => false)
|
73
|
+
end
|
74
|
+
|
75
|
+
protected
|
76
|
+
|
77
|
+
def initialize_salt
|
78
|
+
if new_record?
|
79
|
+
self.password_salt = BCrypt::Engine.generate_salt
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def generate_remember_token
|
84
|
+
self.remember_token = encrypt("#{Time.now} -- #{Time.now}")
|
85
|
+
end
|
86
|
+
|
87
|
+
def generate_confirmation_token
|
88
|
+
self.confirmation_token = encrypt("#{Time.now} -- #{Time.now}")
|
89
|
+
end
|
90
|
+
|
91
|
+
def encrypt_password
|
92
|
+
self.encrypted_password = encrypt(password) unless password.blank?
|
93
|
+
end
|
94
|
+
|
95
|
+
def encrypt(password)
|
96
|
+
BCrypt::Engine.hash_secret(password,password_salt)
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
module ClassMethods
|
103
|
+
def authenticate(login, password)
|
104
|
+
return nil unless user = find_by_login(login)
|
105
|
+
return user if user.authenticated?(password)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module SimpleAuth
|
2
|
+
module Authentication
|
3
|
+
|
4
|
+
def self.included(controller) # :nodoc:
|
5
|
+
controller.send(:include, InstanceMethods)
|
6
|
+
controller.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def self.extended(controller)
|
11
|
+
controller.helper_method :current_user, :signed_in?, :signed_out?
|
12
|
+
controller.hide_action :current_user, :current_user=,
|
13
|
+
:signed_in?, :signed_out?,
|
14
|
+
:sign_in, :sign_out,
|
15
|
+
:authenticate, :deny_access
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module InstanceMethods
|
21
|
+
# User in the current cookie
|
22
|
+
#
|
23
|
+
# @return [User, nil]
|
24
|
+
def current_user
|
25
|
+
@_current_user ||= user_from_cookie
|
26
|
+
end
|
27
|
+
|
28
|
+
# Set the current user
|
29
|
+
#
|
30
|
+
# @param [User]
|
31
|
+
def current_user=(user)
|
32
|
+
@_current_user = user
|
33
|
+
end
|
34
|
+
|
35
|
+
# Is the current user signed in?
|
36
|
+
#
|
37
|
+
# @return [true, false]
|
38
|
+
def signed_in?
|
39
|
+
! current_user.nil?
|
40
|
+
end
|
41
|
+
|
42
|
+
# Is the current user signed out?
|
43
|
+
#
|
44
|
+
# @return [true, false]
|
45
|
+
def signed_out?
|
46
|
+
current_user.nil?
|
47
|
+
end
|
48
|
+
|
49
|
+
# Deny the user access if they are signed out.
|
50
|
+
#
|
51
|
+
# @example
|
52
|
+
# before_filter :authenticate
|
53
|
+
def authenticate
|
54
|
+
deny_access unless signed_in?
|
55
|
+
end
|
56
|
+
|
57
|
+
# Sign user in to cookie.
|
58
|
+
#
|
59
|
+
# @param [User]
|
60
|
+
#
|
61
|
+
# @example
|
62
|
+
# sign_in(@user)
|
63
|
+
def sign_in(user)
|
64
|
+
if user
|
65
|
+
cookies[:remember_token] = {
|
66
|
+
:value => user.remember_token,
|
67
|
+
:expires => 1.year.from_now.utc
|
68
|
+
}
|
69
|
+
current_user = user
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Sign user out of cookie.
|
74
|
+
#
|
75
|
+
# @example
|
76
|
+
# sign_out
|
77
|
+
def sign_out
|
78
|
+
current_user.reset_remember_token! if current_user
|
79
|
+
cookies.delete(:remember_token)
|
80
|
+
current_user = nil
|
81
|
+
end
|
82
|
+
|
83
|
+
# Store the current location and redirect to sign in.
|
84
|
+
# Display a failure flash message if included.
|
85
|
+
#
|
86
|
+
# @param [String] optional flash message to display to denied user
|
87
|
+
def deny_access(flash_message = nil)
|
88
|
+
store_location
|
89
|
+
flash[:failure] = flash_message if flash_message
|
90
|
+
redirect_to(signup_url)
|
91
|
+
end
|
92
|
+
|
93
|
+
protected
|
94
|
+
|
95
|
+
def user_from_cookie
|
96
|
+
if token = cookies[:remember_token]
|
97
|
+
::User.find_by_remember_token(token)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def store_location
|
102
|
+
if request.get?
|
103
|
+
session[:return_to] = request.request_uri
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def redirect_back_or(default)
|
108
|
+
redirect_to(return_to || default)
|
109
|
+
clear_return_to
|
110
|
+
end
|
111
|
+
|
112
|
+
def return_to
|
113
|
+
session[:return_to] || params[:return_to]
|
114
|
+
end
|
115
|
+
|
116
|
+
def clear_return_to
|
117
|
+
session[:return_to] = nil
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/lib/simple_auth.rb
ADDED
data/simple_auth.gemspec
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{simple_auth}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Dieinzige"]
|
12
|
+
s.date = %q{2010-03-03}
|
13
|
+
s.description = %q{ longer description of your gem}
|
14
|
+
s.email = %q{dieinzige@me.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION.yml",
|
26
|
+
"lib/simple_auth.rb",
|
27
|
+
"lib/simple_auth/authenticated.rb",
|
28
|
+
"lib/simple_auth/authentication.rb",
|
29
|
+
"simple_auth.gemspec",
|
30
|
+
"test/helper.rb",
|
31
|
+
"test/test_simple_auth.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/Dieinzige/simple_auth}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.6}
|
37
|
+
s.summary = %q{one-line summary of your gem}
|
38
|
+
s.test_files = [
|
39
|
+
"test/helper.rb",
|
40
|
+
"test/test_simple_auth.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
49
|
+
s.add_runtime_dependency(%q<bcrypt-ruby>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
52
|
+
s.add_dependency(%q<bcrypt-ruby>, [">= 0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
56
|
+
s.add_dependency(%q<bcrypt-ruby>, [">= 0"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: di-simple_auth
|
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
|
+
- Dieinzige
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-03 00:00:00 +03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: thoughtbot-shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: bcrypt-ruby
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :runtime
|
43
|
+
version_requirements: *id002
|
44
|
+
description: " longer description of your gem"
|
45
|
+
email: dieinzige@me.com
|
46
|
+
executables: []
|
47
|
+
|
48
|
+
extensions: []
|
49
|
+
|
50
|
+
extra_rdoc_files:
|
51
|
+
- LICENSE
|
52
|
+
- README.rdoc
|
53
|
+
files:
|
54
|
+
- .document
|
55
|
+
- .gitignore
|
56
|
+
- LICENSE
|
57
|
+
- README.rdoc
|
58
|
+
- Rakefile
|
59
|
+
- VERSION.yml
|
60
|
+
- lib/simple_auth.rb
|
61
|
+
- lib/simple_auth/authenticated.rb
|
62
|
+
- lib/simple_auth/authentication.rb
|
63
|
+
- simple_auth.gemspec
|
64
|
+
- test/helper.rb
|
65
|
+
- test/test_simple_auth.rb
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: http://github.com/Dieinzige/simple_auth
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options:
|
72
|
+
- --charset=UTF-8
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
requirements: []
|
90
|
+
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.3.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: one-line summary of your gem
|
96
|
+
test_files:
|
97
|
+
- test/helper.rb
|
98
|
+
- test/test_simple_auth.rb
|