entrance 0.2.0 → 0.2.1
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/README.md +15 -9
- data/entrance.gemspec +1 -1
- data/examples/rails-app/Gemfile.lock +2 -2
- data/examples/rails-app/README.rdoc +7 -26
- data/examples/rails-app/app/controllers/sessions_controller.rb +3 -3
- data/examples/rails-app/config/initializers/entrance.rb +0 -4
- data/lib/entrance/controller.rb +4 -2
- data/lib/entrance/model.rb +13 -4
- data/lib/entrance/version.rb +1 -1
- data/lib/entrance.rb +3 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -19,6 +19,7 @@ Entrance.configure do |config|
|
|
19
19
|
config.password_attr = 'password_hash' # make sure you map the right attribute name
|
20
20
|
config.access_denied_message_key = 'messages.access_denied'
|
21
21
|
config.remember_for = 1.month
|
22
|
+
config.cookie_secure = Rails.env.production?
|
22
23
|
end
|
23
24
|
|
24
25
|
# in your controller
|
@@ -52,30 +53,31 @@ class SessionsController < ApplicationController
|
|
52
53
|
end
|
53
54
|
|
54
55
|
def create
|
55
|
-
if user = authenticate_and_login(params[:email], params[:password], params[:remember_me])
|
56
|
+
if user = authenticate_and_login(params[:email], params[:password], params[:remember_me] == 'on')
|
56
57
|
redirect_to '/app'
|
57
58
|
else
|
58
|
-
redirect_to
|
59
|
+
redirect_to '/login', :notice => "Invalid credentials."
|
59
60
|
end
|
60
61
|
end
|
61
62
|
|
62
63
|
end
|
63
64
|
```
|
64
65
|
|
65
|
-
If you need more control, you can call directly the model's `.authenticate` method.
|
66
|
+
If you need more control, -- like checking a users state before letting him in -- you can call directly the model's `.authenticate` method, and then call the `login!` method once you're ready.
|
66
67
|
|
67
68
|
``` rb
|
68
69
|
def create
|
69
70
|
if user = User.authenticate(params[:email], params[:password]) and user.active?
|
70
|
-
|
71
|
-
login!(user, remember)
|
71
|
+
login!(user, params[:remember_me] == '1')
|
72
72
|
redirect_to '/app'
|
73
73
|
else
|
74
|
-
redirect_to
|
74
|
+
redirect_to '/login', :notice => "Invalid credentials."
|
75
75
|
end
|
76
76
|
end
|
77
77
|
```
|
78
78
|
|
79
|
+
As you can see, Entrance comes with out-of-box support for the "remember me" option. It also supports the usual 'reset password' token/email logic, but that's it. That's as far as Entrance goes -- we want to keep things simple and lean.
|
80
|
+
|
79
81
|
## Entrance::Config
|
80
82
|
|
81
83
|
All available options, along with their defaults.
|
@@ -83,7 +85,7 @@ All available options, along with their defaults.
|
|
83
85
|
``` rb
|
84
86
|
Entrance.configure do |config|
|
85
87
|
config.model = 'User'
|
86
|
-
config.cipher = Ciphers::BCrypt
|
88
|
+
config.cipher = Entrance::Ciphers::BCrypt # can also be Entrance::Ciphers::SHA1
|
87
89
|
config.secret = nil
|
88
90
|
config.stretches = 10
|
89
91
|
config.salt_attr = nil
|
@@ -129,6 +131,11 @@ Provides:
|
|
129
131
|
- #remember_me! and #forget_me!
|
130
132
|
- #password and #password=(value)
|
131
133
|
- #request_password_reset!
|
134
|
+
|
135
|
+
Examples
|
136
|
+
========
|
137
|
+
|
138
|
+
Thought you might ask. There's a full example Rails app in the examples folder. Check it out.
|
132
139
|
|
133
140
|
Author
|
134
141
|
======
|
@@ -138,5 +145,4 @@ Written by Tomás Pollak.
|
|
138
145
|
Copyright
|
139
146
|
=========
|
140
147
|
|
141
|
-
(c) Fork, Ltd. MIT Licensed.
|
142
|
-
|
148
|
+
(c) Fork, Ltd. MIT Licensed.
|
data/entrance.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.rubyforge_project = "entrance"
|
16
16
|
|
17
17
|
s.add_runtime_dependency "bcrypt", "~> 3.0"
|
18
|
-
s.add_runtime_dependency "activesupport", "
|
18
|
+
s.add_runtime_dependency "activesupport", ">= 3.0"
|
19
19
|
|
20
20
|
s.files = `git ls-files`.split("\n")
|
21
21
|
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
@@ -1,28 +1,9 @@
|
|
1
|
-
==
|
1
|
+
== Example Rails App with Entrance
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
git clone https://github.com/tomas/entrance
|
4
|
+
cd entrance/examples/rails-app
|
5
|
+
bundle install
|
6
|
+
bin/rake db:migrate
|
7
|
+
bin/rails s
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
* Ruby version
|
9
|
-
|
10
|
-
* System dependencies
|
11
|
-
|
12
|
-
* Configuration
|
13
|
-
|
14
|
-
* Database creation
|
15
|
-
|
16
|
-
* Database initialization
|
17
|
-
|
18
|
-
* How to run the test suite
|
19
|
-
|
20
|
-
* Services (job queues, cache servers, search engines, etc.)
|
21
|
-
|
22
|
-
* Deployment instructions
|
23
|
-
|
24
|
-
* ...
|
25
|
-
|
26
|
-
|
27
|
-
Please feel free to use a different markup language if you do not plan to run
|
28
|
-
<tt>rake doc:app</tt>.
|
9
|
+
And ready-o. Then point your browser to localhost:3000 and sign up, then sign in using your credentials.
|
@@ -7,8 +7,8 @@ class SessionsController < ApplicationController
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def create
|
10
|
-
|
11
|
-
|
10
|
+
# boolean flag that determines whether we'll log the user automatically if the browser is closed
|
11
|
+
remember = ['on', 'true', '1'].include?(params[:remember_me])
|
12
12
|
if user = authenticate_and_login(params[:email], params[:password], remember)
|
13
13
|
redirect_to :root
|
14
14
|
else
|
@@ -22,4 +22,4 @@ class SessionsController < ApplicationController
|
|
22
22
|
redirect_to :login, :notice => 'Logged out! See you soon.'
|
23
23
|
end
|
24
24
|
|
25
|
-
end
|
25
|
+
end
|
@@ -1,9 +1,5 @@
|
|
1
|
-
puts 'Initializing Entrance...'
|
2
|
-
|
3
1
|
Entrance.configure do |config|
|
4
2
|
config.remember_for = 1.month
|
5
|
-
config.cipher = Entrance::Ciphers::SHA1
|
6
|
-
config.secret = 'somethingveryveryveryveryverysecret'
|
7
3
|
config.access_denied_redirect_to = '/login'
|
8
4
|
config.cookie_secure = Rails.env.production?
|
9
5
|
end
|
data/lib/entrance/controller.rb
CHANGED
@@ -77,7 +77,9 @@ module Entrance
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def login_from_session
|
80
|
-
|
80
|
+
query = {}
|
81
|
+
query[Entrance.config.unique_key] = session[:user_id]
|
82
|
+
self.current_user = Entrance.model.where(query).first if session[:user_id]
|
81
83
|
end
|
82
84
|
|
83
85
|
def login_from_cookie
|
@@ -108,7 +110,7 @@ module Entrance
|
|
108
110
|
|
109
111
|
def set_remember_cookie
|
110
112
|
values = {
|
111
|
-
:expires => Entrance.config.remember_for.from_now,
|
113
|
+
:expires => Entrance.config.remember_for.to_i.from_now,
|
112
114
|
:httponly => Entrance.config.cookie_httponly,
|
113
115
|
:path => Entrance.config.cookie_path,
|
114
116
|
:secure => Entrance.config.cookie_secure,
|
data/lib/entrance/model.rb
CHANGED
@@ -30,12 +30,21 @@ module Entrance
|
|
30
30
|
|
31
31
|
%w(remember reset).each do |what|
|
32
32
|
if field = Entrance.config.send("#{what}_token_attr")
|
33
|
+
until_field = Entrance.config.send("#{what}_until_attr")
|
33
34
|
|
34
35
|
unless fields.include?(field.to_sym)
|
35
36
|
raise "No #{Entrance.config.send("#{what}_token_attr")} field found. \
|
36
37
|
Set the config.#{what}_token_attr option to nil to disable the #{what} option."
|
37
38
|
end
|
38
39
|
|
40
|
+
if until_field
|
41
|
+
unless fields.include?(until_field.to_sym)
|
42
|
+
raise "Couldn't find a #{Entrance.config.send("#{what}_until_attr")} field. Cannot continue."
|
43
|
+
end
|
44
|
+
else
|
45
|
+
puts "Disabling expiration timestamp for the #{what} option. This is a VERY bad idea."
|
46
|
+
end
|
47
|
+
|
39
48
|
Entrance.config.can?(what, true)
|
40
49
|
include what.to_sym == :remember ? RememberMethods : ResetMethods
|
41
50
|
end
|
@@ -94,17 +103,17 @@ module Entrance
|
|
94
103
|
|
95
104
|
def remember_me!(until_date = nil)
|
96
105
|
update_attribute(Entrance.config.remember_token_attr, Entrance.generate_token)
|
97
|
-
update_remember_token_expiration!(until_date)
|
106
|
+
update_remember_token_expiration!(until_date) if Entrance.config.remember_until_attr
|
98
107
|
end
|
99
108
|
|
100
109
|
def update_remember_token_expiration!(until_date = nil)
|
101
|
-
|
102
|
-
update_attribute(Entrance.config.remember_until_attr,
|
110
|
+
seconds = (until_date || Entrance.config.remember_for).to_i
|
111
|
+
update_attribute(Entrance.config.remember_until_attr, seconds.from_now)
|
103
112
|
end
|
104
113
|
|
105
114
|
def forget_me!
|
106
115
|
update_attribute(Entrance.config.remember_token_attr, nil)
|
107
|
-
update_attribute(Entrance.config.remember_until_attr, nil)
|
116
|
+
update_attribute(Entrance.config.remember_until_attr, nil) if Entrance.config.remember_until_attr
|
108
117
|
end
|
109
118
|
|
110
119
|
end
|
data/lib/entrance/version.rb
CHANGED
data/lib/entrance.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Tom\xC3\xA1s Pollak"
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2015-01-
|
17
|
+
date: 2015-01-08 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -35,7 +35,7 @@ dependencies:
|
|
35
35
|
prerelease: false
|
36
36
|
requirement: &id002 !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
segments:
|
41
41
|
- 3
|