sinatra-authentication 0.2.4 → 0.3.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/Rakefile +2 -1
- data/example/dm_extend_app.rb +25 -0
- data/example/dm_sinbook.rb +55 -0
- data/example/extend_views/edit.haml +42 -0
- data/example/extend_views/index.haml +31 -0
- data/example/extend_views/login.haml +21 -0
- data/example/extend_views/show.haml +9 -0
- data/example/extend_views/signup.haml +30 -0
- data/example/mm_app.rb +22 -0
- data/example/tc_app.rb +16 -0
- data/example/tc_sinbook.rb +62 -0
- data/lib/models/dm_adapter.rb +2 -3
- data/lib/sinatra-authentication.rb +33 -10
- data/lib/views/edit.haml +2 -0
- data/lib/views/login.haml +2 -0
- data/lib/views/signup.haml +2 -0
- data/readme.markdown +73 -1
- data/sinatra-authentication.gemspec +22 -2
- data/test/lib/dm_app.rb +2 -0
- data/test/lib/dm_extend_app.rb +26 -0
- data/test/lib/extend_views/edit.haml +42 -0
- data/test/lib/extend_views/index.haml +31 -0
- data/test/lib/extend_views/login.haml +21 -0
- data/test/lib/extend_views/show.haml +9 -0
- data/test/lib/extend_views/signup.haml +29 -0
- metadata +29 -2
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ begin
|
|
6
6
|
|
7
7
|
Jeweler::Tasks.new do |gemspec|
|
8
8
|
gemspec.name = 'sinatra-authentication'
|
9
|
-
gemspec.version = '0.
|
9
|
+
gemspec.version = '0.3.0'
|
10
10
|
gemspec.description = "Simple authentication plugin for sinatra."
|
11
11
|
gemspec.summary = "Simple authentication plugin for sinatra."
|
12
12
|
gemspec.homepage = "http://github.com/maxjustus/sinatra-authentication"
|
@@ -18,6 +18,7 @@ begin
|
|
18
18
|
gemspec.add_dependency "dm-timestamps"
|
19
19
|
gemspec.add_dependency "rufus-tokyo"
|
20
20
|
gemspec.add_dependency "sinbook"
|
21
|
+
gemspec.add_dependency "rack-flash"
|
21
22
|
end
|
22
23
|
Jeweler::GemcutterTasks.new
|
23
24
|
rescue LoadError
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sinatra'
|
3
|
+
require 'haml'
|
4
|
+
require 'dm-core'
|
5
|
+
require 'rack-flash'
|
6
|
+
require 'sinatra-authentication'
|
7
|
+
|
8
|
+
class DmUser
|
9
|
+
property :name, String
|
10
|
+
end
|
11
|
+
|
12
|
+
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/test.db")
|
13
|
+
DataMapper.auto_migrate!
|
14
|
+
|
15
|
+
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "extend_views/"
|
16
|
+
use Rack::Session::Cookie, :secret => "heyhihello"
|
17
|
+
use Rack::Flash
|
18
|
+
|
19
|
+
set :environment, 'development'
|
20
|
+
set :public, 'public'
|
21
|
+
set :views, 'views'
|
22
|
+
|
23
|
+
get '/' do
|
24
|
+
haml "= render_login_logout", :layout => :layout
|
25
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sinatra'
|
3
|
+
require 'haml'
|
4
|
+
require 'sinbook'
|
5
|
+
require 'dm-core'
|
6
|
+
require 'sinatra-authentication'
|
7
|
+
|
8
|
+
facebook do
|
9
|
+
api_key 'aa2db1b96cb7b57f0c5b1d4d3d8f0a22'
|
10
|
+
secret '21d94ee63969ae3b3f833689838ca00f'
|
11
|
+
app_id 48652736613
|
12
|
+
url 'peoplewithjetpacks.com:4568/'
|
13
|
+
callback 'peoplewithjetpacks.com:4568/'
|
14
|
+
end
|
15
|
+
|
16
|
+
set :port, 4568
|
17
|
+
|
18
|
+
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/test.db")
|
19
|
+
DataMapper.auto_migrate!
|
20
|
+
|
21
|
+
use Rack::Session::Cookie, :secret => "heyhihello"
|
22
|
+
|
23
|
+
set :environment, 'development'
|
24
|
+
set :public, 'public'
|
25
|
+
set :views, 'views'
|
26
|
+
|
27
|
+
get '/' do
|
28
|
+
haml :main
|
29
|
+
end
|
30
|
+
|
31
|
+
get '/test' do
|
32
|
+
login_required
|
33
|
+
'hihihi'
|
34
|
+
end
|
35
|
+
|
36
|
+
__END__
|
37
|
+
|
38
|
+
@@ layout
|
39
|
+
%html{:xmlns=>"http://www.w3.org/1999/xhtml", :'xmlns:fb'=>"http://www.facebook.com/2008/fbml"}
|
40
|
+
%head
|
41
|
+
%title Welcome to my Facebook Connect website!
|
42
|
+
%script{:type => 'text/javascript', :src => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US'}
|
43
|
+
%body
|
44
|
+
= render_login_logout
|
45
|
+
= yield
|
46
|
+
:javascript
|
47
|
+
FB.init("#{fb.api_key}", "/receiver")
|
48
|
+
|
49
|
+
@@ main
|
50
|
+
- if fb[:user]
|
51
|
+
Hi,
|
52
|
+
%fb:profile-pic{:uid => fb[:user]}
|
53
|
+
%fb:name{:uid => fb[:user], :useyou => 'false', :firstnameonly => 'true'}
|
54
|
+
!
|
55
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#sinatra_authentication
|
2
|
+
#sinatra_authentication_flash= flash[:notice]
|
3
|
+
%h1
|
4
|
+
Edit
|
5
|
+
- if @user.id == current_user.id
|
6
|
+
account
|
7
|
+
- else
|
8
|
+
- if @user.email
|
9
|
+
= @user.email
|
10
|
+
- elsif @user.fb_uid
|
11
|
+
<fb:name uid=#{@user.fb_uid} linked='false' />
|
12
|
+
- else
|
13
|
+
account
|
14
|
+
%form{:action => "/users/#{@user.id}/edit", :method => "post"}
|
15
|
+
.field
|
16
|
+
.label
|
17
|
+
%label{:for => "user_email"} Email
|
18
|
+
%input{ :id => "user_email", :name => "user[email]", :size => 30, :type => "text", :value => @user.email }
|
19
|
+
.field
|
20
|
+
.label
|
21
|
+
%label{:for => "user_password"} New password
|
22
|
+
%input{ :id => "user_password", :name => "user[password]", :size => 30, :type => "password" }
|
23
|
+
.field
|
24
|
+
.label
|
25
|
+
%label{:for => "user_password_confirmation"} Confirm
|
26
|
+
%input{ :id => "user_password_confirmation", :name => "user[password_confirmation]", :size => 30, :type => "password" }
|
27
|
+
-# don't render permission field if admin and editing yourself so you don't shoot yourself in the foot
|
28
|
+
- if current_user.admin? && current_user.id != @user.id
|
29
|
+
.field
|
30
|
+
.label
|
31
|
+
%label{:for => 'permission_level'} Permission level
|
32
|
+
%select{ :id => "permission_level", :name => "user[permission_level]" }
|
33
|
+
%option{:value => -1, :selected => @user.admin?}
|
34
|
+
Admin
|
35
|
+
%option{:value => 1, :selected => @user.permission_level == 1}
|
36
|
+
Authenticated user
|
37
|
+
.buttons
|
38
|
+
%input{ :value => "Update", :type => "submit" }
|
39
|
+
- if Sinatra.const_defined?('FacebookObject')
|
40
|
+
- unless @user.fb_uid
|
41
|
+
|
|
42
|
+
= render_facebook_connect_link('Link account with Facebook')
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#sinatra_authentication
|
2
|
+
%h1.page_title Users
|
3
|
+
%table
|
4
|
+
%tr
|
5
|
+
%th
|
6
|
+
- if current_user.admin?
|
7
|
+
%th permission level
|
8
|
+
- @users.each do |user|
|
9
|
+
%tr
|
10
|
+
%td
|
11
|
+
- if user.email
|
12
|
+
= user.email
|
13
|
+
- elsif user.fb_uid
|
14
|
+
<fb:name uid=#{user.fb_uid} />
|
15
|
+
- else
|
16
|
+
"user #{user.id}"
|
17
|
+
- if current_user.admin?
|
18
|
+
%td= user.permission_level
|
19
|
+
%td
|
20
|
+
= user.name
|
21
|
+
%td
|
22
|
+
%a{:href => "/users/#{user.id}"} show
|
23
|
+
- if current_user.admin?
|
24
|
+
%td
|
25
|
+
%a{:href => "/users/#{user.id}/edit"} edit
|
26
|
+
%td
|
27
|
+
-# this doesn't work for tk
|
28
|
+
- if !user.site_admin?
|
29
|
+
%a{:href => "/users/#{user.id}/delete", :onclick => "return confirm('you sure?')"} delete
|
30
|
+
- else
|
31
|
+
site admin
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#sinatra_authentication
|
2
|
+
#sinatra_authentication_flash= flash[:notice]
|
3
|
+
%h1.page_title Login
|
4
|
+
%form{:action => "/login", :method => "post"}
|
5
|
+
.field
|
6
|
+
.label
|
7
|
+
%label{:for => "user_email'"} Email
|
8
|
+
%input{:id => "user_email", :name => "email", :size => 30, :type => "text"}
|
9
|
+
.field
|
10
|
+
.label
|
11
|
+
%label{:for => "user_password"} Password
|
12
|
+
%input{:id => "user_password", :name => "password", :size => 30, :type => "password"}
|
13
|
+
.buttons
|
14
|
+
%input{:value => "login", :type => "submit"}
|
15
|
+
%a{:href => "/signup", :class => 'sinatra_authentication_link'}
|
16
|
+
Signup
|
17
|
+
- if Sinatra.const_defined?('FacebookObject')
|
18
|
+
.third_party_signup
|
19
|
+
%h3.section_title One click login:
|
20
|
+
.login_link.facebook_login
|
21
|
+
= render_facebook_connect_link('Login using facebook', :size => 'large')
|
@@ -0,0 +1,30 @@
|
|
1
|
+
%h1 This view is overridden
|
2
|
+
#sinatra_authentication
|
3
|
+
#sinatra_authentication_flash= flash[:notice]
|
4
|
+
%h1.page_title Signup
|
5
|
+
%form{:action => "/signup", :method => "post"}
|
6
|
+
.field
|
7
|
+
.label
|
8
|
+
%label{:for => "user_email"} Email
|
9
|
+
%input{ :id => "user_email", :name => "user[email]", :size => 30, :type => "text" }
|
10
|
+
.field
|
11
|
+
.label
|
12
|
+
%label{:for => "user_password"} Password
|
13
|
+
%input{ :id => "user_password", :name => "user[password]", :size => 30, :type => "password" }
|
14
|
+
.field
|
15
|
+
.label
|
16
|
+
%label{:for => "user_name"} Name
|
17
|
+
%input{ :id => "user_name", :name => "user[name]", :size => 30, :type => "text" }
|
18
|
+
.field
|
19
|
+
.label
|
20
|
+
%label{:for => "user_password_confirmation"} Confirm Password
|
21
|
+
%input{ :id => "user_password_confirmation", :name => "user[password_confirmation]", :size => 30, :type => "password" }
|
22
|
+
.buttons
|
23
|
+
%input{ :value => "Create account", :type => "submit" }
|
24
|
+
%a{:href => "/login", :class => 'sinatra_authentication_link'}
|
25
|
+
Login
|
26
|
+
- if Sinatra.const_defined?('FacebookObject')
|
27
|
+
.third_party_signup
|
28
|
+
%h3.section_title One click signup:
|
29
|
+
.login_link.facebook_login
|
30
|
+
= render_facebook_connect_link('Signup using facebook', :size => 'large')
|
data/example/mm_app.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'haml'
|
4
|
+
require 'mongo_mapper'
|
5
|
+
require 'sinatra-authentication'
|
6
|
+
|
7
|
+
logger = Logger.new($stdout)
|
8
|
+
MongoMapper.connection = Mongo::Connection.new('db.mongohq.com', 27017, :logger => logger)
|
9
|
+
MongoMapper.database = "fdbk"
|
10
|
+
MongoMapper.database.authenticate(ENV['mongohq_user'], ENV['mongohq_pass'])
|
11
|
+
|
12
|
+
class TestApp < Sinatra::Base
|
13
|
+
use Rack::Session::Cookie, :secret => "heyhihello"
|
14
|
+
|
15
|
+
set :environment, 'development'
|
16
|
+
set :public, 'public'
|
17
|
+
set :views, 'views'
|
18
|
+
|
19
|
+
get '/' do
|
20
|
+
haml "= render_login_logout", :layout => :layout
|
21
|
+
end
|
22
|
+
end
|
data/example/tc_app.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sinatra'
|
3
|
+
require 'haml'
|
4
|
+
require 'rufus/tokyo'
|
5
|
+
require 'sinatra-authentication'
|
6
|
+
|
7
|
+
use Rack::Session::Cookie, :secret => "heyhihello"
|
8
|
+
TcUserTable.cabinet_path = File.dirname(__FILE__)
|
9
|
+
|
10
|
+
set :environment, 'development'
|
11
|
+
set :public, 'public'
|
12
|
+
set :views, 'views'
|
13
|
+
|
14
|
+
get '/' do
|
15
|
+
haml "= render_login_logout", :layout => :layout
|
16
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'haml'
|
3
|
+
require 'sinbook'
|
4
|
+
require 'rufus/tokyo'
|
5
|
+
require 'sinatra'
|
6
|
+
require 'sinatra-authentication'
|
7
|
+
|
8
|
+
use Rack::Session::Cookie, :secret => "heyhihello"
|
9
|
+
TcUserTable.cabinet_path = File.dirname(__FILE__)
|
10
|
+
|
11
|
+
facebook do
|
12
|
+
api_key 'aa2db1b96cb7b57f0c5b1d4d3d8f0a22'
|
13
|
+
secret '21d94ee63969ae3b3f833689838ca00f'
|
14
|
+
app_id 48652736613
|
15
|
+
url 'peoplewithjetpacks.com:4568/'
|
16
|
+
callback 'peoplewithjetpacks.com:4568/'
|
17
|
+
end
|
18
|
+
|
19
|
+
set :port, 4568
|
20
|
+
|
21
|
+
get '/' do
|
22
|
+
haml :main
|
23
|
+
end
|
24
|
+
|
25
|
+
get '/test' do
|
26
|
+
login_required
|
27
|
+
'hihihi'
|
28
|
+
end
|
29
|
+
|
30
|
+
__END__
|
31
|
+
|
32
|
+
@@ layout
|
33
|
+
%html{:xmlns=>"http://www.w3.org/1999/xhtml", :'xmlns:fb'=>"http://www.facebook.com/2008/fbml"}
|
34
|
+
%head
|
35
|
+
%title Welcome to my Facebook Connect website!
|
36
|
+
%script{:type => 'text/javascript', :src => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US'}
|
37
|
+
%script{:type => 'text/javascript', :src => 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'}
|
38
|
+
:javascript
|
39
|
+
$(document).ready(function(){
|
40
|
+
/* test facebook crap works with ajax */
|
41
|
+
$('.sinatra-authentication-login').click(function(){
|
42
|
+
$.get($(this).attr('href'), {}, function(data){
|
43
|
+
$('#test_box').html(data);
|
44
|
+
});
|
45
|
+
return false;
|
46
|
+
});
|
47
|
+
});
|
48
|
+
%body
|
49
|
+
= render_login_logout
|
50
|
+
= yield
|
51
|
+
:javascript
|
52
|
+
FB.init("#{fb.api_key}", "/receiver")
|
53
|
+
#test_box
|
54
|
+
|
55
|
+
@@ main
|
56
|
+
- if fb[:user]
|
57
|
+
Hi,
|
58
|
+
%fb:profile-pic{:uid => fb[:user]}
|
59
|
+
%fb:name{:uid => fb[:user], :useyou => 'false', :firstnameonly => 'true'}
|
60
|
+
!
|
61
|
+
%br/
|
62
|
+
|
data/lib/models/dm_adapter.rb
CHANGED
@@ -31,15 +31,14 @@ module DmAdapter
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def delete(pk)
|
34
|
-
user =
|
34
|
+
user = DmUser.first(:id => pk)
|
35
35
|
user.destroy
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
module InstanceMethods
|
40
40
|
def update(attributes)
|
41
|
-
@instance.
|
42
|
-
@instance.save
|
41
|
+
@instance.update attributes
|
43
42
|
end
|
44
43
|
|
45
44
|
def method_missing(meth, *args, &block)
|
@@ -11,7 +11,7 @@ module Sinatra
|
|
11
11
|
#sinatra 9.1.1 doesn't have multiple view capability anywhere
|
12
12
|
#so to get around I have to do it totally manually by
|
13
13
|
#loading the view from this path into a string and rendering it
|
14
|
-
set :
|
14
|
+
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
|
15
15
|
|
16
16
|
get '/users' do
|
17
17
|
login_required
|
@@ -48,6 +48,11 @@ module Sinatra
|
|
48
48
|
post '/login' do
|
49
49
|
if user = User.authenticate(params[:email], params[:password])
|
50
50
|
session[:user] = user.id
|
51
|
+
|
52
|
+
if Rack.const_defined?('Flash')
|
53
|
+
flash[:notice] = "Login successful."
|
54
|
+
end
|
55
|
+
|
51
56
|
if session[:return_to]
|
52
57
|
redirect_url = session[:return_to]
|
53
58
|
session[:return_to] = false
|
@@ -56,13 +61,18 @@ module Sinatra
|
|
56
61
|
redirect '/'
|
57
62
|
end
|
58
63
|
else
|
64
|
+
if Rack.const_defined?('Flash')
|
65
|
+
flash[:notice] = "The email or password you entered is incorrect."
|
66
|
+
end
|
59
67
|
redirect '/login'
|
60
68
|
end
|
61
69
|
end
|
62
70
|
|
63
71
|
get '/logout' do
|
64
72
|
session[:user] = nil
|
65
|
-
|
73
|
+
if Rack.const_defined?('Flash')
|
74
|
+
flash[:notice] = "Logout successful."
|
75
|
+
end
|
66
76
|
redirect '/'
|
67
77
|
end
|
68
78
|
|
@@ -72,19 +82,23 @@ module Sinatra
|
|
72
82
|
|
73
83
|
post '/signup' do
|
74
84
|
@user = User.set(params[:user])
|
75
|
-
if @user
|
85
|
+
if @user && @user.id
|
76
86
|
session[:user] = @user.id
|
87
|
+
if Rack.const_defined?('Flash')
|
88
|
+
flash[:notice] = "Account created."
|
89
|
+
end
|
77
90
|
redirect '/'
|
78
91
|
else
|
79
|
-
|
80
|
-
|
92
|
+
if Rack.const_defined?('Flash')
|
93
|
+
flash[:notice] = 'There were some problems creating your account. Please be sure you\'ve entered all your information correctly.'
|
94
|
+
end
|
95
|
+
redirect '/signup'
|
81
96
|
end
|
82
97
|
end
|
83
98
|
|
84
99
|
get '/users/:id/edit' do
|
85
100
|
login_required
|
86
101
|
redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]
|
87
|
-
|
88
102
|
@user = User.get(:id => params[:id])
|
89
103
|
haml get_view_as_string("edit.haml"), :layout => use_layout?
|
90
104
|
end
|
@@ -101,9 +115,14 @@ module Sinatra
|
|
101
115
|
end
|
102
116
|
|
103
117
|
if user.update(user_attributes)
|
118
|
+
if Rack.const_defined?('Flash')
|
119
|
+
flash[:notice] = 'Account updated.'
|
120
|
+
end
|
104
121
|
redirect '/'
|
105
122
|
else
|
106
|
-
|
123
|
+
if Rack.const_defined?('Flash')
|
124
|
+
flash[:notice] = 'Whoops, looks like there were some problems with your updates.'
|
125
|
+
end
|
107
126
|
redirect "/users/#{user.id}/edit"
|
108
127
|
end
|
109
128
|
end
|
@@ -113,9 +132,13 @@ module Sinatra
|
|
113
132
|
redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]
|
114
133
|
|
115
134
|
if User.delete(params[:id])
|
116
|
-
|
135
|
+
if Rack.const_defined?('Flash')
|
136
|
+
flash[:notice] = "User deleted."
|
137
|
+
end
|
117
138
|
else
|
118
|
-
|
139
|
+
if Rack.const_defined?('Flash')
|
140
|
+
flash[:notice] = "Deletion failed."
|
141
|
+
end
|
119
142
|
end
|
120
143
|
redirect '/'
|
121
144
|
end
|
@@ -185,7 +208,7 @@ module Sinatra
|
|
185
208
|
|
186
209
|
#BECAUSE sinatra 9.1.1 can't load views from different paths properly
|
187
210
|
def get_view_as_string(filename)
|
188
|
-
view = options.
|
211
|
+
view = options.sinatra_authentication_view_path + filename
|
189
212
|
data = ""
|
190
213
|
f = File.open(view, "r")
|
191
214
|
f.each_line do |line|
|
data/lib/views/edit.haml
CHANGED
data/lib/views/login.haml
CHANGED
data/lib/views/signup.haml
CHANGED
data/readme.markdown
CHANGED
@@ -2,23 +2,31 @@
|
|
2
2
|
|
3
3
|
## INSTALLATION:
|
4
4
|
|
5
|
-
in your sinatra app simply require either "dm-core", "rufus-tokyo" or "mongo_mapper", "digest/sha1" and then "sinatra-authentication" and turn on session storage
|
5
|
+
in your sinatra app simply require either "dm-core", "rufus-tokyo" or "mongo_mapper", "digest/sha1", 'rack-flash' (if you want flash messages) and then "sinatra-authentication" and turn on session storage
|
6
6
|
with a super secret key, like so:
|
7
7
|
|
8
8
|
require "dm-core"
|
9
9
|
require "digest/sha1"
|
10
|
+
require 'rack-flash'
|
10
11
|
require "sinatra-authentication"
|
11
12
|
|
12
13
|
use Rack::Session::Cookie, :secret => 'A1 sauce 1s so good you should use 1t on a11 yr st34ksssss'
|
14
|
+
#if you want flash messages
|
15
|
+
use Rack::Flash
|
13
16
|
|
14
17
|
If you're using rufus-tokyo, you also need to set the database path for Users. like so:
|
15
18
|
|
16
19
|
require "rufus_tokyo"
|
17
20
|
require "digest/sha1"
|
21
|
+
require 'rack-flash'
|
18
22
|
require "sinatra-authentication"
|
23
|
+
|
24
|
+
#Setting the database path for Users
|
19
25
|
TcUserTable.cabinet_path = File.dirname(__FILE__) + 'folder/where/you/wanna/store/your/database'
|
20
26
|
|
21
27
|
use Rack::Session::Cookie, :secret => 'A1 sauce 1s so good you should use 1t on a11 yr st34ksssss'
|
28
|
+
#if you want flash messages
|
29
|
+
use Rack::Flash
|
22
30
|
|
23
31
|
## DEFAULT ROUTES:
|
24
32
|
|
@@ -37,6 +45,20 @@ If you fetch any of the user pages using ajax, they will automatically render wi
|
|
37
45
|
* get '/reciever'
|
38
46
|
* get '/connect'
|
39
47
|
|
48
|
+
## FLASH MESSAGES
|
49
|
+
|
50
|
+
Flash messages are implemented using rack-flash. To set them up add this to your code:
|
51
|
+
|
52
|
+
require 'rack-flash'
|
53
|
+
|
54
|
+
#be sure and do this after after 'use Rack:Session:Cookie...'
|
55
|
+
use Rack::Flash
|
56
|
+
|
57
|
+
And then sinatra-authentication related flash messages will be made available through flash[:notice]
|
58
|
+
|
59
|
+
-# somewhere in a haml view:
|
60
|
+
= flash[:notice]
|
61
|
+
|
40
62
|
## HELPER METHODS:
|
41
63
|
|
42
64
|
This plugin provides the following helper methods for your sinatra app:
|
@@ -141,3 +163,53 @@ If they aren't already logged in to the app through the normal login form,
|
|
141
163
|
it creates a new user in the database without an email address or password.
|
142
164
|
They can later add this data by going to "/users/#{current_user.id}/edit",
|
143
165
|
which will allow them to log in using their email address and password, OR their facebook account.
|
166
|
+
|
167
|
+
## OVERRIDING DEFAULT VIEWS
|
168
|
+
|
169
|
+
Right now if you're going to override sinatra-authentication's views, you have to override all of them.
|
170
|
+
This is something I hope to change in a future release.
|
171
|
+
|
172
|
+
To override the default view path do something like this:
|
173
|
+
|
174
|
+
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "my_views/"
|
175
|
+
|
176
|
+
And then the views you'll need to define are:
|
177
|
+
|
178
|
+
* show.haml
|
179
|
+
* index.haml
|
180
|
+
* signup.haml
|
181
|
+
* login.haml
|
182
|
+
* edit.haml
|
183
|
+
|
184
|
+
The signup and edit form fields are named so they pass a hash called 'user' to the server:
|
185
|
+
|
186
|
+
%input{:name => "user[email]", :size => 30, :type => "text", :value => @user.email}
|
187
|
+
%input{:name => "user[password]", :size => 30, :type => "password"}
|
188
|
+
%input{:name => "user[password_confirmation]", :size => 30, :type => "password"}
|
189
|
+
|
190
|
+
%select{:name => "user[permission_level]"}
|
191
|
+
%option{:value => -1, :selected => @user.admin?}
|
192
|
+
Admin
|
193
|
+
%option{:value => 1, :selected => @user.permission_level == 1}
|
194
|
+
Authenticated user
|
195
|
+
|
196
|
+
The login form fields just pass a field called email and a field called password:
|
197
|
+
|
198
|
+
%input{:name => "email", :size => 30, :type => "text"}
|
199
|
+
%input{:name => "password", :size => 30, :type => "password"}
|
200
|
+
|
201
|
+
To add methods or properties to the User class, you have to access the underlying database user class, like so:
|
202
|
+
|
203
|
+
class DmUser
|
204
|
+
property :name, String
|
205
|
+
property :has_dog, Boolean, :default => false
|
206
|
+
end
|
207
|
+
|
208
|
+
The database user classes are named as follows:
|
209
|
+
|
210
|
+
* for Datamapper:
|
211
|
+
> DmUser
|
212
|
+
* for Rufus Tokyo:
|
213
|
+
> TcUser
|
214
|
+
* for Mongomapper:
|
215
|
+
> MmUser
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sinatra-authentication}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Max Justus Spransy"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-04-17}
|
13
13
|
s.description = %q{Simple authentication plugin for sinatra.}
|
14
14
|
s.email = %q{maxjustus@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -21,6 +21,16 @@ Gem::Specification.new do |s|
|
|
21
21
|
"Manifest",
|
22
22
|
"Rakefile",
|
23
23
|
"TODO",
|
24
|
+
"example/dm_extend_app.rb",
|
25
|
+
"example/dm_sinbook.rb",
|
26
|
+
"example/extend_views/edit.haml",
|
27
|
+
"example/extend_views/index.haml",
|
28
|
+
"example/extend_views/login.haml",
|
29
|
+
"example/extend_views/show.haml",
|
30
|
+
"example/extend_views/signup.haml",
|
31
|
+
"example/mm_app.rb",
|
32
|
+
"example/tc_app.rb",
|
33
|
+
"example/tc_sinbook.rb",
|
24
34
|
"lib/models/abstract_user.rb",
|
25
35
|
"lib/models/datamapper_user.rb",
|
26
36
|
"lib/models/dm_adapter.rb",
|
@@ -38,7 +48,13 @@ Gem::Specification.new do |s|
|
|
38
48
|
"sinatra-authentication.gemspec",
|
39
49
|
"test/datamapper_test.rb",
|
40
50
|
"test/lib/dm_app.rb",
|
51
|
+
"test/lib/dm_extend_app.rb",
|
41
52
|
"test/lib/dm_sinbook.rb",
|
53
|
+
"test/lib/extend_views/edit.haml",
|
54
|
+
"test/lib/extend_views/index.haml",
|
55
|
+
"test/lib/extend_views/login.haml",
|
56
|
+
"test/lib/extend_views/show.haml",
|
57
|
+
"test/lib/extend_views/signup.haml",
|
42
58
|
"test/lib/helper.rb",
|
43
59
|
"test/lib/mm_app.rb",
|
44
60
|
"test/lib/tc_app.rb",
|
@@ -58,6 +74,7 @@ Gem::Specification.new do |s|
|
|
58
74
|
"test/lib/mm_app.rb",
|
59
75
|
"test/lib/tc_sinbook.rb",
|
60
76
|
"test/lib/helper.rb",
|
77
|
+
"test/lib/dm_extend_app.rb",
|
61
78
|
"test/lib/dm_app.rb",
|
62
79
|
"test/datamapper_test.rb",
|
63
80
|
"test/mongomapper_test.rb",
|
@@ -76,6 +93,7 @@ Gem::Specification.new do |s|
|
|
76
93
|
s.add_runtime_dependency(%q<dm-timestamps>, [">= 0"])
|
77
94
|
s.add_runtime_dependency(%q<rufus-tokyo>, [">= 0"])
|
78
95
|
s.add_runtime_dependency(%q<sinbook>, [">= 0"])
|
96
|
+
s.add_runtime_dependency(%q<rack-flash>, [">= 0"])
|
79
97
|
else
|
80
98
|
s.add_dependency(%q<sinatra>, [">= 0"])
|
81
99
|
s.add_dependency(%q<dm-core>, [">= 0"])
|
@@ -83,6 +101,7 @@ Gem::Specification.new do |s|
|
|
83
101
|
s.add_dependency(%q<dm-timestamps>, [">= 0"])
|
84
102
|
s.add_dependency(%q<rufus-tokyo>, [">= 0"])
|
85
103
|
s.add_dependency(%q<sinbook>, [">= 0"])
|
104
|
+
s.add_dependency(%q<rack-flash>, [">= 0"])
|
86
105
|
end
|
87
106
|
else
|
88
107
|
s.add_dependency(%q<sinatra>, [">= 0"])
|
@@ -91,6 +110,7 @@ Gem::Specification.new do |s|
|
|
91
110
|
s.add_dependency(%q<dm-timestamps>, [">= 0"])
|
92
111
|
s.add_dependency(%q<rufus-tokyo>, [">= 0"])
|
93
112
|
s.add_dependency(%q<sinbook>, [">= 0"])
|
113
|
+
s.add_dependency(%q<rack-flash>, [">= 0"])
|
94
114
|
end
|
95
115
|
end
|
96
116
|
|
data/test/lib/dm_app.rb
CHANGED
@@ -2,12 +2,14 @@ require 'rubygems'
|
|
2
2
|
require 'sinatra'
|
3
3
|
require 'haml'
|
4
4
|
require 'dm-core'
|
5
|
+
require 'rack-flash'
|
5
6
|
require File.join(File.dirname(__FILE__), '../../lib/sinatra-authentication')
|
6
7
|
|
7
8
|
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/test.db")
|
8
9
|
DataMapper.auto_migrate!
|
9
10
|
|
10
11
|
use Rack::Session::Cookie, :secret => "heyhihello"
|
12
|
+
use Rack::Flash
|
11
13
|
|
12
14
|
set :environment, 'development'
|
13
15
|
set :public, 'public'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sinatra'
|
3
|
+
require 'haml'
|
4
|
+
require 'dm-core'
|
5
|
+
require 'rack-flash'
|
6
|
+
require File.join(File.dirname(__FILE__), '../../lib/sinatra-authentication')
|
7
|
+
|
8
|
+
|
9
|
+
class DmUser
|
10
|
+
property :name, String
|
11
|
+
end
|
12
|
+
|
13
|
+
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/test.db")
|
14
|
+
DataMapper.auto_migrate!
|
15
|
+
|
16
|
+
set :lil_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "extend_views/"
|
17
|
+
use Rack::Session::Cookie, :secret => "heyhihello"
|
18
|
+
use Rack::Flash
|
19
|
+
|
20
|
+
set :environment, 'development'
|
21
|
+
set :public, 'public'
|
22
|
+
set :views, 'views'
|
23
|
+
|
24
|
+
get '/' do
|
25
|
+
haml "= render_login_logout", :layout => :layout
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#sinatra_authentication
|
2
|
+
#sinatra_authentication_flash= session[:flash]
|
3
|
+
%h1
|
4
|
+
Edit
|
5
|
+
- if @user.id == current_user.id
|
6
|
+
account
|
7
|
+
- else
|
8
|
+
- if @user.email
|
9
|
+
= @user.email
|
10
|
+
- elsif @user.fb_uid
|
11
|
+
<fb:name uid=#{@user.fb_uid} linked='false' />
|
12
|
+
- else
|
13
|
+
account
|
14
|
+
%form{:action => "/users/#{@user.id}/edit", :method => "post"}
|
15
|
+
.field
|
16
|
+
.label
|
17
|
+
%label{:for => "user_email"} Email
|
18
|
+
%input{ :id => "user_email", :name => "user[email]", :size => 30, :type => "text", :value => @user.email }
|
19
|
+
.field
|
20
|
+
.label
|
21
|
+
%label{:for => "user_password"} New password
|
22
|
+
%input{ :id => "user_password", :name => "user[password]", :size => 30, :type => "password" }
|
23
|
+
.field
|
24
|
+
.label
|
25
|
+
%label{:for => "user_password_confirmation"} Confirm
|
26
|
+
%input{ :id => "user_password_confirmation", :name => "user[password_confirmation]", :size => 30, :type => "password" }
|
27
|
+
-# don't render permission field if admin and editing yourself so you don't shoot yourself in the foot
|
28
|
+
- if current_user.admin? && current_user.id != @user.id
|
29
|
+
.field
|
30
|
+
.label
|
31
|
+
%label{:for => 'permission_level'} Permission level
|
32
|
+
%select{ :id => "permission_level", :name => "user[permission_level]" }
|
33
|
+
%option{:value => -1, :selected => @user.admin?}
|
34
|
+
Admin
|
35
|
+
%option{:value => 1, :selected => @user.permission_level == 1}
|
36
|
+
Authenticated user
|
37
|
+
.buttons
|
38
|
+
%input{ :value => "Update", :type => "submit" }
|
39
|
+
- if Sinatra.const_defined?('FacebookObject')
|
40
|
+
- unless @user.fb_uid
|
41
|
+
|
|
42
|
+
= render_facebook_connect_link('Link account with Facebook')
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#sinatra_authentication
|
2
|
+
%h1.page_title Users
|
3
|
+
%table
|
4
|
+
%tr
|
5
|
+
%th
|
6
|
+
- if current_user.admin?
|
7
|
+
%th permission level
|
8
|
+
- @users.each do |user|
|
9
|
+
%tr
|
10
|
+
%td
|
11
|
+
- if user.email
|
12
|
+
= user.email
|
13
|
+
- elsif user.fb_uid
|
14
|
+
<fb:name uid=#{user.fb_uid} />
|
15
|
+
- else
|
16
|
+
"user #{user.id}"
|
17
|
+
- if current_user.admin?
|
18
|
+
%td= user.permission_level
|
19
|
+
%td
|
20
|
+
= user.name
|
21
|
+
%td
|
22
|
+
%a{:href => "/users/#{user.id}"} show
|
23
|
+
- if current_user.admin?
|
24
|
+
%td
|
25
|
+
%a{:href => "/users/#{user.id}/edit"} edit
|
26
|
+
%td
|
27
|
+
-# this doesn't work for tk
|
28
|
+
- if !user.site_admin?
|
29
|
+
%a{:href => "/users/#{user.id}/delete", :onclick => "return confirm('you sure?')"} delete
|
30
|
+
- else
|
31
|
+
site admin
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#sinatra_authentication
|
2
|
+
#sinatra_authentication_flash= session[:flash]
|
3
|
+
%h1.page_title Login
|
4
|
+
%form{:action => "/login", :method => "post"}
|
5
|
+
.field
|
6
|
+
.label
|
7
|
+
%label{:for => "user_email'"} Email
|
8
|
+
%input{:id => "user_email", :name => "email", :size => 30, :type => "text"}
|
9
|
+
.field
|
10
|
+
.label
|
11
|
+
%label{:for => "user_password"} Password
|
12
|
+
%input{:id => "user_password", :name => "password", :size => 30, :type => "password"}
|
13
|
+
.buttons
|
14
|
+
%input{:value => "login", :type => "submit"}
|
15
|
+
%a{:href => "/signup", :class => 'sinatra_authentication_link'}
|
16
|
+
Signup
|
17
|
+
- if Sinatra.const_defined?('FacebookObject')
|
18
|
+
.third_party_signup
|
19
|
+
%h3.section_title One click login:
|
20
|
+
.login_link.facebook_login
|
21
|
+
= render_facebook_connect_link('Login using facebook', :size => 'large')
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#sinatra_authentication
|
2
|
+
#sinatra_authentication_flash= session[:flash]
|
3
|
+
%h1.page_title Signup
|
4
|
+
%form{:action => "/signup", :method => "post"}
|
5
|
+
.field
|
6
|
+
.label
|
7
|
+
%label{:for => "user_email"} Email
|
8
|
+
%input{ :id => "user_email", :name => "user[email]", :size => 30, :type => "text" }
|
9
|
+
.field
|
10
|
+
.label
|
11
|
+
%label{:for => "user_password"} Password
|
12
|
+
%input{ :id => "user_password", :name => "user[password]", :size => 30, :type => "password" }
|
13
|
+
.field
|
14
|
+
.label
|
15
|
+
%label{:for => "user_name"} Name
|
16
|
+
%input{ :id => "user_name", :name => "user[name]", :size => 30, :type => "text" }
|
17
|
+
.field
|
18
|
+
.label
|
19
|
+
%label{:for => "user_password_confirmation"} Confirm Password
|
20
|
+
%input{ :id => "user_password_confirmation", :name => "user[password_confirmation]", :size => 30, :type => "password" }
|
21
|
+
.buttons
|
22
|
+
%input{ :value => "Create account", :type => "submit" }
|
23
|
+
%a{:href => "/login", :class => 'sinatra_authentication_link'}
|
24
|
+
Login
|
25
|
+
- if Sinatra.const_defined?('FacebookObject')
|
26
|
+
.third_party_signup
|
27
|
+
%h3.section_title One click signup:
|
28
|
+
.login_link.facebook_login
|
29
|
+
= render_facebook_connect_link('Signup using facebook', :size => 'large')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-authentication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Justus Spransy
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-04-17 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -72,6 +72,16 @@ dependencies:
|
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: "0"
|
74
74
|
version:
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rack-flash
|
77
|
+
type: :runtime
|
78
|
+
version_requirement:
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
version:
|
75
85
|
description: Simple authentication plugin for sinatra.
|
76
86
|
email: maxjustus@gmail.com
|
77
87
|
executables: []
|
@@ -86,6 +96,16 @@ files:
|
|
86
96
|
- Manifest
|
87
97
|
- Rakefile
|
88
98
|
- TODO
|
99
|
+
- example/dm_extend_app.rb
|
100
|
+
- example/dm_sinbook.rb
|
101
|
+
- example/extend_views/edit.haml
|
102
|
+
- example/extend_views/index.haml
|
103
|
+
- example/extend_views/login.haml
|
104
|
+
- example/extend_views/show.haml
|
105
|
+
- example/extend_views/signup.haml
|
106
|
+
- example/mm_app.rb
|
107
|
+
- example/tc_app.rb
|
108
|
+
- example/tc_sinbook.rb
|
89
109
|
- lib/models/abstract_user.rb
|
90
110
|
- lib/models/datamapper_user.rb
|
91
111
|
- lib/models/dm_adapter.rb
|
@@ -103,7 +123,13 @@ files:
|
|
103
123
|
- sinatra-authentication.gemspec
|
104
124
|
- test/datamapper_test.rb
|
105
125
|
- test/lib/dm_app.rb
|
126
|
+
- test/lib/dm_extend_app.rb
|
106
127
|
- test/lib/dm_sinbook.rb
|
128
|
+
- test/lib/extend_views/edit.haml
|
129
|
+
- test/lib/extend_views/index.haml
|
130
|
+
- test/lib/extend_views/login.haml
|
131
|
+
- test/lib/extend_views/show.haml
|
132
|
+
- test/lib/extend_views/signup.haml
|
107
133
|
- test/lib/helper.rb
|
108
134
|
- test/lib/mm_app.rb
|
109
135
|
- test/lib/tc_app.rb
|
@@ -145,6 +171,7 @@ test_files:
|
|
145
171
|
- test/lib/mm_app.rb
|
146
172
|
- test/lib/tc_sinbook.rb
|
147
173
|
- test/lib/helper.rb
|
174
|
+
- test/lib/dm_extend_app.rb
|
148
175
|
- test/lib/dm_app.rb
|
149
176
|
- test/datamapper_test.rb
|
150
177
|
- test/mongomapper_test.rb
|