entrance 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +8 -4
- data/examples/rails-app/app/models/user.rb +2 -0
- data/examples/sinatra-app/README.md +12 -0
- data/examples/sinatra-app/app/models.rb +3 -2
- data/examples/sinatra-app/app/views/login.erb +1 -1
- data/examples/sinatra-app/app/views/signup.erb +3 -3
- data/lib/entrance/model.rb +41 -39
- data/lib/entrance/sinatra.rb +61 -0
- data/lib/entrance/version.rb +2 -2
- metadata +36 -52
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9db48b6f4e950ecfecf95a8c51d17fd8e29296cc
|
4
|
+
data.tar.gz: 7cd2766497f3634f0ba201d1360055a11201e80d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bbde9df5134233c9eee47139dd28643ed09653fd1f4509db82551e5d85b76fa6dc62bcce43e888dc55130f15525322af62920db80b5a378c34effe333b2fb18e
|
7
|
+
data.tar.gz: 6a2da9b7a78dcb87c790a9d1833f7d8591fb99e5bc604f5a51c02002ec70dc39655b78f436df11c5b62b4b5c54d76c5e14a7d83948cf60270d8d9d365c617487
|
data/README.md
CHANGED
@@ -35,7 +35,9 @@ end
|
|
35
35
|
class User
|
36
36
|
include Entrance::Model
|
37
37
|
|
38
|
-
...
|
38
|
+
... (setup fields)
|
39
|
+
|
40
|
+
validate_entrance! # ensures fields for authentication/remember/reset are present
|
39
41
|
end
|
40
42
|
```
|
41
43
|
|
@@ -125,15 +127,17 @@ And the following helpers:
|
|
125
127
|
|
126
128
|
Provides:
|
127
129
|
|
130
|
+
- .validate_entrance!
|
128
131
|
- .authenticate(username, password)
|
129
|
-
-
|
132
|
+
- .with_password_reset_token(token)
|
130
133
|
- #password and #password=(value)
|
131
|
-
- #
|
134
|
+
- #remember_me! and #forget_me! (unless remember_attr is set to nil)
|
135
|
+
- #request_password_reset! (unless reset_attr is set to nil)
|
132
136
|
|
133
137
|
Examples
|
134
138
|
========
|
135
139
|
|
136
|
-
Thought you might ask. There's a full example Rails app in the examples folder. Check
|
140
|
+
Thought you might ask. There's a full example Rails app and a Sinatra app in the examples folder. Check them out.
|
137
141
|
|
138
142
|
Author
|
139
143
|
======
|
@@ -0,0 +1,12 @@
|
|
1
|
+
## Example Sinatra App with Entrance
|
2
|
+
|
3
|
+
A quick example that shows how to use Entrance with a Sinatra modular app. Requires MongoDB.
|
4
|
+
|
5
|
+
To run:
|
6
|
+
|
7
|
+
git clone https://github.com/tomas/entrance
|
8
|
+
cd entrance/examples/sinatra-app
|
9
|
+
bundle install
|
10
|
+
bundle exec puma
|
11
|
+
|
12
|
+
And ready-o. Then point your browser to localhost:9292 and sign up, then sign in using your credentials.
|
@@ -14,6 +14,7 @@ end
|
|
14
14
|
|
15
15
|
class User
|
16
16
|
include MongoMapper::Document
|
17
|
+
include Entrance::Model
|
17
18
|
|
18
19
|
key :state, :default => 'active'
|
19
20
|
|
@@ -27,10 +28,10 @@ class User
|
|
27
28
|
key :reset_token
|
28
29
|
key :reset_token_expires_at, Time
|
29
30
|
|
30
|
-
|
31
|
+
validate_entrance! # ensures everything is in order, and sets up password validations
|
31
32
|
|
32
33
|
def active?
|
33
34
|
state.to_sym == :active
|
34
35
|
end
|
35
36
|
|
36
|
-
end
|
37
|
+
end
|
@@ -15,7 +15,7 @@
|
|
15
15
|
<input checked="checked" id="remember_me" name="remember_me" type="checkbox" />
|
16
16
|
</p>
|
17
17
|
|
18
|
-
<input class="right btn btn-primary"
|
18
|
+
<input class="right btn btn-primary" tabindex="3" type="submit" value="Log in" />
|
19
19
|
|
20
20
|
</form>
|
21
21
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
<h2>Sign up</h2>
|
2
2
|
|
3
|
-
<form accept-charset="UTF-8" action="/signup"
|
3
|
+
<form accept-charset="UTF-8" action="/signup" method="post">
|
4
4
|
|
5
5
|
<p>
|
6
6
|
<input id="user_name" name="user[name]" placeholder="Your name" tabindex="1" type="text" />
|
@@ -18,8 +18,8 @@
|
|
18
18
|
<input id="user_password_confirmation" name="user[password_confirmation]" placeholder="Retype password" tabindex="4" type="password" />
|
19
19
|
</p>
|
20
20
|
|
21
|
-
<input class="right btn btn-primary"
|
21
|
+
<input class="right btn btn-primary" tabindex="5" type="submit" value="Sign up" />
|
22
22
|
|
23
23
|
</form>
|
24
24
|
|
25
|
-
<p>Have an account? <a href="<%= url('/login') %>">Log in</a
|
25
|
+
<p>Have an account? <a href="<%= url('/login') %>">Log in</a>.</p>
|
data/lib/entrance/model.rb
CHANGED
@@ -3,61 +3,63 @@ module Entrance
|
|
3
3
|
|
4
4
|
def self.included(base)
|
5
5
|
|
6
|
-
# if the target model class does not have a Model.where() method,
|
6
|
+
# if the target model class does not have a Model.where() method,
|
7
7
|
# then login_by_session wont work, nor the ClassMethods below.
|
8
8
|
# won't work so we cannot continue.
|
9
9
|
unless base.respond_to?(:where)
|
10
|
-
raise "#{base.name} does not have a .where() class method. Cannot continue."
|
10
|
+
raise "#{base.name} does not have a .where() finder class method. Cannot continue."
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
elsif base.respond_to?(:keys) # MongoMapper::Document
|
16
|
-
base.keys.keys
|
17
|
-
else # just get setters in the class
|
18
|
-
base.instance_methods(false).select { |m| m[/\=$/] }.map { |s| s.to_s.sub('=', '') }
|
19
|
-
end.map { |el| el.to_sym }
|
20
|
-
|
21
|
-
%w(username_attr password_attr).each do |key|
|
22
|
-
field = Entrance.config.send(key)
|
23
|
-
unless fields.include?(field.to_sym)
|
24
|
-
raise "Couldn't find '#{field}' in #{base.name} model."
|
25
|
-
end
|
26
|
-
end
|
13
|
+
base.extend(ClassMethods)
|
14
|
+
end
|
27
15
|
|
28
|
-
|
29
|
-
if field = Entrance.config.send("#{what}_token_attr")
|
30
|
-
until_field = Entrance.config.send("#{what}_until_attr")
|
16
|
+
module ClassMethods
|
31
17
|
|
18
|
+
def validate_entrance!
|
19
|
+
fields = if self.respond_to?(:columns) # ActiveRecord::Base
|
20
|
+
columns.collect(&:name)
|
21
|
+
elsif self.respond_to?(:keys) # MongoMapper::Document
|
22
|
+
keys.keys
|
23
|
+
else # just get setters in the class
|
24
|
+
instance_methods(false).select { |m| m[/\=$/] }.map { |s| s.to_s.sub('=', '') }
|
25
|
+
end.map { |el| el.to_sym }
|
26
|
+
|
27
|
+
%w(username_attr password_attr).each do |key|
|
28
|
+
field = Entrance.config.send(key)
|
32
29
|
unless fields.include?(field.to_sym)
|
33
|
-
raise "
|
34
|
-
Set the config.#{what}_token_attr option to nil to disable the #{what} option."
|
30
|
+
raise "Couldn't find '#{field}' in #{base.name} model."
|
35
31
|
end
|
32
|
+
end
|
33
|
+
|
34
|
+
%w(remember reset).each do |what|
|
35
|
+
if field = Entrance.config.send("#{what}_token_attr")
|
36
|
+
until_field = Entrance.config.send("#{what}_until_attr")
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
unless fields.include?(field.to_sym)
|
39
|
+
raise "No #{Entrance.config.send("#{what}_token_attr")} field found. \
|
40
|
+
Set the config.#{what}_token_attr option to nil to disable the #{what} option."
|
41
|
+
end
|
42
|
+
|
43
|
+
if until_field
|
44
|
+
unless fields.include?(until_field.to_sym)
|
45
|
+
raise "Couldn't find a #{Entrance.config.send("#{what}_until_attr")} field. Cannot continue."
|
46
|
+
end
|
47
|
+
else
|
48
|
+
puts "Disabling expiration timestamp for the #{what} option. This is a VERY bad idea."
|
40
49
|
end
|
41
|
-
else
|
42
|
-
puts "Disabling expiration timestamp for the #{what} option. This is a VERY bad idea."
|
43
|
-
end
|
44
50
|
|
45
|
-
|
46
|
-
|
51
|
+
Entrance.config.can?(what, true)
|
52
|
+
self.send(:include, what.to_sym == :remember ? RememberMethods : ResetMethods)
|
53
|
+
end
|
47
54
|
end
|
48
|
-
end
|
49
55
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
56
|
+
if self.respond_to?(:validates)
|
57
|
+
validates :password, :presence => true, :length => 6..32, :if => :password_required?
|
58
|
+
validates :password, :confirmation => true, :if => :password_required?
|
59
|
+
validates :password_confirmation, :presence => true, :if => :password_required?
|
60
|
+
end
|
54
61
|
end
|
55
62
|
|
56
|
-
base.extend(ClassMethods)
|
57
|
-
end
|
58
|
-
|
59
|
-
module ClassMethods
|
60
|
-
|
61
63
|
def authenticate(username, password)
|
62
64
|
return if username.blank? or password.blank?
|
63
65
|
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'entrance'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
|
5
|
+
module Entrance
|
6
|
+
|
7
|
+
def self.registered(app)
|
8
|
+
|
9
|
+
app.include ::Entrance::Controller
|
10
|
+
|
11
|
+
app.helpers do
|
12
|
+
def redirect_with(url, type, message)
|
13
|
+
flash[type] = message if respond_to?(:flash)
|
14
|
+
redirect(to(url))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
app.get '/login' do
|
19
|
+
if logged_in?
|
20
|
+
redirect(to('/'))
|
21
|
+
else
|
22
|
+
erb :'public/login'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
app.post '/login' do
|
27
|
+
remember = ['on', 'true', '1'].include?(params[:remember])
|
28
|
+
if user = authenticate_and_login(params[:username], params[:password], remember)
|
29
|
+
flash[:success] = 'Welcome back!'
|
30
|
+
redirect_to_stored_or(to('/'))
|
31
|
+
else
|
32
|
+
redirect_with('/login', :error, "Couldn't log you in. Please try again.")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
app.get '/logout' do
|
37
|
+
kill_session!
|
38
|
+
redirect_with('/login', :notice, 'Logged out! See you soon.')
|
39
|
+
end
|
40
|
+
|
41
|
+
app.get '/signup' do
|
42
|
+
return redirect(to('/')) if logged_in?
|
43
|
+
@user = ::Entrance.model.new
|
44
|
+
erb :'public/signup'
|
45
|
+
end
|
46
|
+
|
47
|
+
app.post '/signup' do
|
48
|
+
@user = ::Entrance.model.new(params[:user])
|
49
|
+
if @user.valid? && @user.save
|
50
|
+
redirect_with('/login', :success, "Account created! Please sign in to continue.")
|
51
|
+
else
|
52
|
+
flash[:error] = "Couldn't sign you up. Please try again."
|
53
|
+
erb :'public/signup'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/lib/entrance/version.rb
CHANGED
metadata
CHANGED
@@ -1,46 +1,37 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: entrance
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 2
|
8
|
-
- 5
|
9
|
-
version: 0.2.5
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
12
|
-
-
|
6
|
+
authors:
|
7
|
+
- Tomás Pollak
|
13
8
|
autorequire:
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2015-03-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: bcrypt
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
segments:
|
28
|
-
- 3
|
29
|
-
- 0
|
30
|
-
version: "3.0"
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
31
20
|
type: :runtime
|
32
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
33
27
|
description: Doesn't fiddle with your controllers and routes.
|
34
|
-
email:
|
28
|
+
email:
|
35
29
|
- tomas@forkhq.com
|
36
30
|
executables: []
|
37
|
-
|
38
31
|
extensions: []
|
39
|
-
|
40
32
|
extra_rdoc_files: []
|
41
|
-
|
42
|
-
|
43
|
-
- .gitignore
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
44
35
|
- README.md
|
45
36
|
- Rakefile
|
46
37
|
- entrance.gemspec
|
@@ -109,6 +100,7 @@ files:
|
|
109
100
|
- examples/rails-app/vendor/assets/javascripts/.keep
|
110
101
|
- examples/rails-app/vendor/assets/stylesheets/.keep
|
111
102
|
- examples/sinatra-app/Gemfile
|
103
|
+
- examples/sinatra-app/README.md
|
112
104
|
- examples/sinatra-app/app/models.rb
|
113
105
|
- examples/sinatra-app/app/routes.rb
|
114
106
|
- examples/sinatra-app/app/views/layout.erb
|
@@ -121,38 +113,30 @@ files:
|
|
121
113
|
- lib/entrance/config.rb
|
122
114
|
- lib/entrance/controller.rb
|
123
115
|
- lib/entrance/model.rb
|
116
|
+
- lib/entrance/sinatra.rb
|
124
117
|
- lib/entrance/version.rb
|
125
|
-
has_rdoc: true
|
126
118
|
homepage: https://github.com/tomas/entrance
|
127
119
|
licenses: []
|
128
|
-
|
120
|
+
metadata: {}
|
129
121
|
post_install_message:
|
130
122
|
rdoc_options: []
|
131
|
-
|
132
|
-
require_paths:
|
123
|
+
require_paths:
|
133
124
|
- lib
|
134
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
136
127
|
- - ">="
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
-
requirements:
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
143
132
|
- - ">="
|
144
|
-
- !ruby/object:Gem::Version
|
145
|
-
segments:
|
146
|
-
- 1
|
147
|
-
- 3
|
148
|
-
- 6
|
133
|
+
- !ruby/object:Gem::Version
|
149
134
|
version: 1.3.6
|
150
135
|
requirements: []
|
151
|
-
|
152
136
|
rubyforge_project: entrance
|
153
|
-
rubygems_version:
|
137
|
+
rubygems_version: 2.2.0
|
154
138
|
signing_key:
|
155
|
-
specification_version:
|
139
|
+
specification_version: 4
|
156
140
|
summary: Lean authentication alternative for Rails and Sinatra.
|
157
141
|
test_files: []
|
158
|
-
|
142
|
+
has_rdoc:
|