sorcery 0.2.0 → 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/Gemfile.lock +1 -1
- data/README.rdoc +91 -59
- data/VERSION +1 -1
- data/lib/generators/sorcery_migration/sorcery_migration_generator.rb +24 -0
- data/lib/generators/sorcery_migration/templates/activity_logging.rb +17 -0
- data/lib/generators/sorcery_migration/templates/brute_force_protection.rb +11 -0
- data/lib/generators/sorcery_migration/templates/core.rb +16 -0
- data/lib/generators/sorcery_migration/templates/oauth.rb +14 -0
- data/lib/generators/sorcery_migration/templates/remember_me.rb +15 -0
- data/lib/generators/sorcery_migration/templates/reset_password.rb +13 -0
- data/lib/generators/sorcery_migration/templates/user_activation.rb +17 -0
- data/lib/sorcery/controller/adapters/sinatra.rb +97 -0
- data/lib/sorcery/controller/submodules/http_basic_auth.rb +10 -6
- data/lib/sorcery/controller/submodules/oauth/oauth1.rb +12 -4
- data/lib/sorcery/controller/submodules/oauth/oauth2.rb +2 -1
- data/lib/sorcery/controller/submodules/oauth.rb +9 -9
- data/lib/sorcery/model/submodules/activity_logging.rb +1 -1
- data/lib/sorcery/model/submodules/brute_force_protection.rb +0 -4
- data/lib/sorcery/model/submodules/oauth.rb +1 -1
- data/lib/sorcery/sinatra.rb +14 -0
- data/lib/sorcery/test_helpers/rails.rb +57 -0
- data/lib/sorcery/test_helpers/sinatra.rb +131 -0
- data/lib/sorcery/test_helpers.rb +8 -52
- data/lib/sorcery.rb +8 -0
- data/sorcery.gemspec +77 -3
- data/spec/Gemfile +1 -1
- data/spec/Gemfile.lock +2 -2
- data/spec/rails3/app_root/Gemfile +2 -4
- data/spec/rails3/app_root/Gemfile.lock +2 -5
- data/spec/rails3/app_root/spec/controller_oauth2_spec.rb +4 -2
- data/spec/rails3/app_root/spec/controller_oauth_spec.rb +8 -3
- data/spec/rails3/app_root/spec/controller_session_timeout_spec.rb +2 -2
- data/spec/rails3/app_root/spec/spec_helper.rb +1 -0
- data/spec/sinatra/Gemfile +12 -0
- data/spec/sinatra/Gemfile.lock +134 -0
- data/spec/sinatra/Rakefile +10 -0
- data/spec/sinatra/authentication.rb +3 -0
- data/spec/sinatra/db/migrate/activation/20101224223622_add_activation_to_users.rb +17 -0
- data/spec/sinatra/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
- data/spec/sinatra/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
- data/spec/sinatra/db/migrate/core/20101224223620_create_users.rb +16 -0
- data/spec/sinatra/db/migrate/oauth/20101224223628_create_authentications.rb +14 -0
- data/spec/sinatra/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
- data/spec/sinatra/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
- data/spec/sinatra/filters.rb +21 -0
- data/spec/sinatra/myapp.rb +133 -0
- data/spec/sinatra/sorcery_mailer.rb +25 -0
- data/spec/sinatra/spec/controller_activity_logging_spec.rb +85 -0
- data/spec/sinatra/spec/controller_brute_force_protection_spec.rb +69 -0
- data/spec/sinatra/spec/controller_http_basic_auth_spec.rb +53 -0
- data/spec/sinatra/spec/controller_oauth2_spec.rb +119 -0
- data/spec/sinatra/spec/controller_oauth_spec.rb +121 -0
- data/spec/sinatra/spec/controller_remember_me_spec.rb +64 -0
- data/spec/sinatra/spec/controller_session_timeout_spec.rb +52 -0
- data/spec/sinatra/spec/controller_spec.rb +120 -0
- data/spec/sinatra/spec/spec.opts +4 -0
- data/spec/sinatra/spec/spec_helper.rb +44 -0
- data/spec/sinatra/spec/user_activation_spec.rb +188 -0
- data/spec/sinatra/spec/user_activity_logging_spec.rb +36 -0
- data/spec/sinatra/spec/user_brute_force_protection_spec.rb +76 -0
- data/spec/sinatra/spec/user_oauth_spec.rb +39 -0
- data/spec/sinatra/spec/user_remember_me_spec.rb +66 -0
- data/spec/sinatra/spec/user_reset_password_spec.rb +178 -0
- data/spec/sinatra/spec/user_spec.rb +317 -0
- data/spec/sinatra/user.rb +6 -0
- data/spec/sinatra/views/test_login.erb +4 -0
- data/spec/untitled folder +18 -0
- metadata +76 -2
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
|
@@ -1,32 +1,103 @@
|
|
|
1
1
|
= sorcery
|
|
2
|
-
Magical Authentication for Rails 3.
|
|
2
|
+
Magical Authentication for Rails 3 and Sinatra.
|
|
3
3
|
|
|
4
4
|
Inspired by restful_authentication, Authlogic and Devise.
|
|
5
5
|
Crypto code taken almost unchanged from Authlogic.
|
|
6
|
-
OAuth code inspired by OmniAuth.
|
|
6
|
+
OAuth code inspired by OmniAuth and Ryan Bates's railscasts about it.
|
|
7
|
+
|
|
7
8
|
|
|
8
9
|
== Summary
|
|
9
10
|
|
|
10
|
-
Sorcery aims to make your life easier by giving you an easy API to write your own user authentication flow with.
|
|
11
|
-
It does this with a few goals in mind:
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
Sorcery is a stripped-down, bare-bones authentication library, with which you can write your own authentication flow.
|
|
13
|
+
It was built with a few goals in mind:
|
|
14
|
+
|
|
15
|
+
* Less is more - less than 20 public methods to remember for the entire feature-set make the lib easy to 'get'.
|
|
16
|
+
* No built-in or generated code - use the library's methods inside *your own* MVC structures, and don't fight to fix someone else's.
|
|
15
17
|
* Magic yes, Voodoo no - the lib should be easy to hack for most developers.
|
|
16
18
|
* Configuration over Confusion - Simple & short configuration as possible, not drowning in syntactic sugar.
|
|
17
19
|
* Keep MVC cleanly separated - DB is for models, sessions are for controllers. Models stay unaware of sessions.
|
|
18
20
|
|
|
19
21
|
Hopefully, I've achieved this. If not, let me know.
|
|
20
22
|
|
|
23
|
+
|
|
21
24
|
== Useful Links:
|
|
22
25
|
|
|
23
|
-
Example app using sorcery: https://github.com/NoamB/sorcery-example-app
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
Example Rails 3 app using sorcery: https://github.com/NoamB/sorcery-example-app
|
|
28
|
+
|
|
29
|
+
Example Sinatra app using sorcery: https://github.com/NoamB/sorcery-example-app-sinatra
|
|
30
|
+
|
|
31
|
+
Documentation: http://rubydoc.info/gems/sorcery/0.3.0/frames
|
|
32
|
+
|
|
33
|
+
Check out the tutorials in the github wiki!
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
== Installation:
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
If using bundler, first add 'sorcery' to your Gemfile:
|
|
40
|
+
|
|
41
|
+
gem "sorcery"
|
|
42
|
+
|
|
43
|
+
And run
|
|
44
|
+
|
|
45
|
+
bundle install
|
|
46
|
+
|
|
47
|
+
Otherwise simply
|
|
48
|
+
|
|
49
|
+
gem install sorcery
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
== Configuration:
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
1. config/application.rb
|
|
56
|
+
|
|
57
|
+
config.sorcery.submodules = [:user_activation, :remember_me] # add the submodules you want to use
|
|
58
|
+
# You can also configure here any controller and any controller-submodule option here. For example:
|
|
59
|
+
config.sorcery.session_timeout = 10.minutes
|
|
60
|
+
|
|
61
|
+
2. app/models/user.rb (or another model of your choice, but a User class is assumed by default)
|
|
62
|
+
|
|
63
|
+
activate_sorcery! do |config|
|
|
64
|
+
config.user_activation_mailer = MyMailer
|
|
65
|
+
config.username_attribute_name = :email
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
3. app/controllers/application_controller.rb (OPTIONAL: this is actually needed only in some cases)
|
|
69
|
+
|
|
70
|
+
activate_sorcery! do |config|
|
|
71
|
+
config.session_timeout = 10.minutes
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
The configuration options vary with the submodules you've chosen to use, so check the documentation or the wiki tutorials regarding the specific submodule.
|
|
75
|
+
|
|
76
|
+
For your convenience, Sorcery includes a migrations generator for Rails, which can be used like so:
|
|
77
|
+
|
|
78
|
+
rails g sorcery_migration [list of submodules]
|
|
79
|
+
|
|
80
|
+
For example, for only the core functionality use:
|
|
81
|
+
|
|
82
|
+
rails g sorcery_migration core
|
|
83
|
+
|
|
84
|
+
To generate migrations for both the core AND 'remember_me' submodule:
|
|
85
|
+
|
|
86
|
+
rails g sorcery_migration core remember_me
|
|
87
|
+
|
|
88
|
+
These migrations use the default fields. You can choose to use these migrations or make your own tables and fields. Sorcery tries not to impose a database structure and naming scheme on your application.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
== Usage
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
Please see the tutorials in the github wiki.
|
|
95
|
+
|
|
26
96
|
|
|
27
97
|
== Full Features List by module:
|
|
28
98
|
|
|
29
|
-
|
|
99
|
+
|
|
100
|
+
Core (see lib/sorcery/model.rb and lib/sorcery/controller.rb):
|
|
30
101
|
* login/logout, optional return user to requested url on login, configurable redirect for non-logged-in users.
|
|
31
102
|
* password encryption, algorithms: bcrypt(default), md5, sha1, sha256, sha512, aes256, custom(yours!), none. Configurable stretches and salt.
|
|
32
103
|
* configurable attribute names for username, password and email.
|
|
@@ -70,62 +141,20 @@ Oauth (see lib/sorcery/controller/submodules/oauth.rb):
|
|
|
70
141
|
* OAuth1 and OAuth2 support (currently twitter & facebook)
|
|
71
142
|
* configurable db field names and authentications table.
|
|
72
143
|
|
|
144
|
+
|
|
73
145
|
== Next Planned Features:
|
|
74
146
|
|
|
147
|
+
|
|
75
148
|
I've got many plans which include (by priority):
|
|
76
|
-
*
|
|
149
|
+
* Scoping logins (to a subdomain or another arbitrary field)
|
|
150
|
+
* Simple auth (no user)
|
|
151
|
+
* Switching authentication mode at runtime (Maintenance mode)
|
|
77
152
|
* Mongoid support
|
|
78
153
|
* Configurable Auto login on registration/activation
|
|
79
154
|
* Other reset password strategies (security questions?)
|
|
80
155
|
* Other brute force protection strategies (captcha)
|
|
81
156
|
* Have an idea? Let me know, and it might get into the gem!
|
|
82
157
|
|
|
83
|
-
== Installation:
|
|
84
|
-
|
|
85
|
-
You can either git clone and then 'rake install' to live on the edge (unstable),
|
|
86
|
-
|
|
87
|
-
Or simply (stable):
|
|
88
|
-
|
|
89
|
-
gem install sorcery
|
|
90
|
-
|
|
91
|
-
== Configuration:
|
|
92
|
-
|
|
93
|
-
First add 'sorcery' to your Gemfile:
|
|
94
|
-
|
|
95
|
-
gem "sorcery"
|
|
96
|
-
|
|
97
|
-
And run
|
|
98
|
-
|
|
99
|
-
bundle install
|
|
100
|
-
|
|
101
|
-
There are 2 required places to configure the plugin, and an optional one:
|
|
102
|
-
1. config/application.rb
|
|
103
|
-
|
|
104
|
-
config.sorcery.submodules = [:user_activation, :remember_me] # add the modules you want to use
|
|
105
|
-
|
|
106
|
-
You can also configure here any controller and any controller-submodule option here.
|
|
107
|
-
For example:
|
|
108
|
-
|
|
109
|
-
config.sorcery.session_timeout = 10.minutes
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
2. app/models/user.rb (or another model of your choice)
|
|
113
|
-
|
|
114
|
-
activate_sorcery! do |config|
|
|
115
|
-
config.user_activation_mailer = MyMailer
|
|
116
|
-
config.username_attribute_name = :email
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
3. app/controllers/application_controller.rb (OPTIONAL: this is actually needed only in some cases)
|
|
120
|
-
|
|
121
|
-
activate_sorcery! do |config|
|
|
122
|
-
config.session_timeout = 10.minutes
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
Also check the migrations in the example app to see what database fields are expected.
|
|
126
|
-
|
|
127
|
-
The configuration options vary with the modules you've chosen to use.
|
|
128
|
-
|
|
129
158
|
|
|
130
159
|
== Contributing to sorcery
|
|
131
160
|
|
|
@@ -135,19 +164,22 @@ For this:
|
|
|
135
164
|
|
|
136
165
|
* Fork the project.
|
|
137
166
|
* Make your feature addition or bug fix.
|
|
138
|
-
* Add tests for it.
|
|
167
|
+
* Add tests for it. I've used RSpec so far, please remain consistent with it.
|
|
139
168
|
* Commit, do not mess with Rakefiles, version, or history. (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)
|
|
140
169
|
* Send me a pull request. Bonus points for topic branches.
|
|
141
170
|
|
|
142
171
|
If you feel my work has made your life easier, and you would like to thank me through a donation, my paypal email is in the contact details.
|
|
143
172
|
|
|
173
|
+
|
|
144
174
|
== Contact
|
|
145
175
|
|
|
176
|
+
Feel free to ask questions using these contact details:
|
|
177
|
+
|
|
146
178
|
email: nbenari@gmail.com ( also for paypal )
|
|
147
179
|
twitter: @nbenari
|
|
148
180
|
|
|
181
|
+
|
|
149
182
|
== Copyright
|
|
150
183
|
|
|
151
|
-
Copyright (c) 2010 Noam Ben Ari (nbenari@gmail.com). See LICENSE.txt for further details.
|
|
152
|
-
Released with permission from Kontera (http://www.kontera.com), where I work.
|
|
153
184
|
|
|
185
|
+
Copyright (c) 2010 Noam Ben Ari (nbenari@gmail.com). See LICENSE.txt for further details.
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.3.0
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'rails/generators'
|
|
2
|
+
require 'rails/generators/migration'
|
|
3
|
+
|
|
4
|
+
class SorceryMigrationGenerator < Rails::Generators::Base
|
|
5
|
+
include Rails::Generators::Migration
|
|
6
|
+
|
|
7
|
+
source_root File.join(File.dirname(__FILE__), 'templates')
|
|
8
|
+
argument :submodules, :type => :array, :required => true
|
|
9
|
+
|
|
10
|
+
def self.next_migration_number(dirname)
|
|
11
|
+
if ActiveRecord::Base.timestamped_migrations
|
|
12
|
+
Time.new.utc.strftime("%Y%m%d%H%M%S")
|
|
13
|
+
else
|
|
14
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def create_migration_file
|
|
19
|
+
self.submodules.each do |submodule|
|
|
20
|
+
migration_template "#{submodule}.rb", "db/migrate/sorcery_#{submodule}.rb"
|
|
21
|
+
sleep 1 # for the timestamp to change
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class SorceryActivityLogging < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
add_column :users, :last_login_at, :datetime, :default => nil
|
|
4
|
+
add_column :users, :last_logout_at, :datetime, :default => nil
|
|
5
|
+
add_column :users, :last_activity_at, :datetime, :default => nil
|
|
6
|
+
|
|
7
|
+
add_index :users, [:last_logout_at, :last_activity_at]
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.down
|
|
11
|
+
remove_index :users, [:last_logout_at, :last_activity_at]
|
|
12
|
+
|
|
13
|
+
remove_column :users, :last_activity_at
|
|
14
|
+
remove_column :users, :last_logout_at
|
|
15
|
+
remove_column :users, :last_login_at
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
class SorceryBruteForceProtection < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
add_column :users, :failed_logins_count, :integer, :default => 0
|
|
4
|
+
add_column :users, :lock_expires_at, :datetime, :default => nil
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def self.down
|
|
8
|
+
remove_column :users, :lock_expires_at
|
|
9
|
+
remove_column :users, :failed_logins_count
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class SorceryCore < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
create_table :users do |t|
|
|
4
|
+
t.string :username, :null => false
|
|
5
|
+
t.string :email, :default => nil
|
|
6
|
+
t.string :crypted_password, :default => nil
|
|
7
|
+
t.string :salt, :default => nil
|
|
8
|
+
|
|
9
|
+
t.timestamps
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.down
|
|
14
|
+
drop_table :users
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class SorceryOauth < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
create_table :authentications do |t|
|
|
4
|
+
t.integer :user_id, :null => false
|
|
5
|
+
t.string :provider, :uid, :null => false
|
|
6
|
+
|
|
7
|
+
t.timestamps
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.down
|
|
12
|
+
drop_table :authentications
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class SorceryRememberMe < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
add_column :users, :remember_me_token, :string, :default => nil
|
|
4
|
+
add_column :users, :remember_me_token_expires_at, :datetime, :default => nil
|
|
5
|
+
|
|
6
|
+
add_index :users, :remember_me_token
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def self.down
|
|
10
|
+
remove_index :users, :remember_me_token
|
|
11
|
+
|
|
12
|
+
remove_column :users, :remember_me_token_expires_at
|
|
13
|
+
remove_column :users, :remember_me_token
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class SorceryResetPassword < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
add_column :users, :reset_password_token, :string, :default => nil
|
|
4
|
+
add_column :users, :reset_password_token_expires_at, :datetime, :default => nil
|
|
5
|
+
add_column :users, :reset_password_email_sent_at, :datetime, :default => nil
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def self.down
|
|
9
|
+
remove_column :users, :reset_password_email_sent_at
|
|
10
|
+
remove_column :users, :reset_password_token_expires_at
|
|
11
|
+
remove_column :users, :reset_password_token
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class SorceryUserActivation < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
add_column :users, :activation_state, :string, :default => nil
|
|
4
|
+
add_column :users, :activation_token, :string, :default => nil
|
|
5
|
+
add_column :users, :activation_token_expires_at, :datetime, :default => nil
|
|
6
|
+
|
|
7
|
+
add_index :users, :activation_token
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.down
|
|
11
|
+
remove_index :users, :activation_token
|
|
12
|
+
|
|
13
|
+
remove_column :users, :activation_token_expires_at
|
|
14
|
+
remove_column :users, :activation_token
|
|
15
|
+
remove_column :users, :activation_state
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
module Sorcery
|
|
2
|
+
module Controller
|
|
3
|
+
module Adapters
|
|
4
|
+
# This module does the magic of translating Rails commands to Sinatra.
|
|
5
|
+
# This way the Rails code doesn't change, but it actually now calls Sinatra calls.
|
|
6
|
+
module Sinatra
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.class_eval do
|
|
9
|
+
class << self
|
|
10
|
+
# prepend a filter
|
|
11
|
+
def prepend_filter(type, path = nil, options = {}, &block)
|
|
12
|
+
return filters[type].unshift block unless path
|
|
13
|
+
path, options = //, path if path.respond_to?(:each_pair)
|
|
14
|
+
block, *arguments = compile!(type, path, block, options)
|
|
15
|
+
prepend_filter(type) do
|
|
16
|
+
process_route(*arguments) { instance_eval(&block) }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def after_filter(filter)
|
|
21
|
+
after do
|
|
22
|
+
send(filter)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
::Sinatra::Request.class_eval do
|
|
29
|
+
def authorization
|
|
30
|
+
env['HTTP_AUTHORIZATION'] ||
|
|
31
|
+
env['X-HTTP_AUTHORIZATION'] ||
|
|
32
|
+
env['X_HTTP_AUTHORIZATION'] ||
|
|
33
|
+
env['REDIRECT_X_HTTP_AUTHORIZATION'] || nil
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
base.send(:include, InstanceMethods)
|
|
38
|
+
base.extend(ClassMethods)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
module InstanceMethods
|
|
42
|
+
def reset_session
|
|
43
|
+
session.clear
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def redirect_to(*args)
|
|
47
|
+
args.pop if args.last.is_a?(Hash)
|
|
48
|
+
redirect(*args)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def root_path
|
|
52
|
+
'/'
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
helpers do
|
|
56
|
+
def request_http_basic_authentication(realm)
|
|
57
|
+
response.header['WWW-Authenticate'] = %(Basic realm="#{realm}")
|
|
58
|
+
response.status = 401
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def authenticate_with_http_basic(&blk)
|
|
62
|
+
@auth ||= Rack::Auth::Basic::Request.new(request.env)
|
|
63
|
+
yield @auth.credentials if ( @auth.provided? && @auth.basic? && @auth.credentials )
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def cookies
|
|
68
|
+
@cookie_proxy ||= CookieProxy.new(request,response)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
class CookieProxy
|
|
73
|
+
def initialize(request,response)
|
|
74
|
+
@request = request
|
|
75
|
+
@response = response
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def [](key)
|
|
79
|
+
@request.cookies[key.to_s]
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def []=(key, value)
|
|
83
|
+
@response.set_cookie(key, value)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
module ClassMethods
|
|
88
|
+
def prepend_before_filter(filter)
|
|
89
|
+
prepend_filter(:before) do
|
|
90
|
+
send(filter)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
@@ -48,13 +48,17 @@ module Sorcery
|
|
|
48
48
|
|
|
49
49
|
# Sets the realm name by searching the controller name in the hash given at configuration time.
|
|
50
50
|
def realm_name_by_controller
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
if defined?(ActionController::Base)
|
|
52
|
+
current_controller = self.class
|
|
53
|
+
while current_controller != ActionController::Base
|
|
54
|
+
result = Config.controller_to_realm_map[current_controller.controller_name]
|
|
55
|
+
return result if result
|
|
56
|
+
current_controller = self.class.superclass
|
|
57
|
+
end
|
|
58
|
+
nil
|
|
59
|
+
else
|
|
60
|
+
Config.controller_to_realm_map["application"]
|
|
56
61
|
end
|
|
57
|
-
nil
|
|
58
62
|
end
|
|
59
63
|
|
|
60
64
|
end
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require 'oauth'
|
|
1
2
|
module Sorcery
|
|
2
3
|
module Controller
|
|
3
4
|
module Submodules
|
|
@@ -7,16 +8,23 @@ module Sorcery
|
|
|
7
8
|
"1.0"
|
|
8
9
|
end
|
|
9
10
|
|
|
10
|
-
def get_request_token
|
|
11
|
-
::OAuth::
|
|
11
|
+
def get_request_token(token=nil,secret=nil)
|
|
12
|
+
return ::OAuth::RequestToken.new(get_consumer,token,secret) if token && secret
|
|
13
|
+
get_consumer.get_request_token(:oauth_callback => @callback_url)
|
|
12
14
|
end
|
|
13
15
|
|
|
14
16
|
def authorize_url(args)
|
|
15
|
-
args[:request_token].authorize_url(:oauth_callback => @callback_url)
|
|
17
|
+
get_request_token(args[:request_token],args[:request_token_secret]).authorize_url(:oauth_callback => @callback_url)
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
def get_access_token(args)
|
|
19
|
-
args[:request_token].get_access_token(:oauth_verifier => args[:oauth_verifier])
|
|
21
|
+
get_request_token(args[:request_token],args[:request_token_secret]).get_access_token(:oauth_verifier => args[:oauth_verifier])
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
protected
|
|
25
|
+
|
|
26
|
+
def get_consumer
|
|
27
|
+
::OAuth::Consumer.new(@key, @secret, :site => @site)
|
|
20
28
|
end
|
|
21
29
|
end
|
|
22
30
|
end
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require 'oauth2'
|
|
1
2
|
module Sorcery
|
|
2
3
|
module Controller
|
|
3
4
|
module Submodules
|
|
@@ -7,7 +8,7 @@ module Sorcery
|
|
|
7
8
|
"2.0"
|
|
8
9
|
end
|
|
9
10
|
|
|
10
|
-
def authorize_url(args)
|
|
11
|
+
def authorize_url(*args)
|
|
11
12
|
client = ::OAuth2::Client.new(@key, @secret, :site => @site)
|
|
12
13
|
client.web_server.authorize_url(:redirect_uri => @callback_url, :scope => @scope)
|
|
13
14
|
end
|
|
@@ -10,12 +10,9 @@ module Sorcery
|
|
|
10
10
|
Config.module_eval do
|
|
11
11
|
class << self
|
|
12
12
|
attr_reader :oauth_providers # oauth providers like twitter.
|
|
13
|
-
|
|
14
|
-
attr_accessor :authentications_class
|
|
15
|
-
|
|
13
|
+
|
|
16
14
|
def merge_oauth_defaults!
|
|
17
|
-
@defaults.merge!(:@oauth_providers => []
|
|
18
|
-
:@authentications_class => nil)
|
|
15
|
+
@defaults.merge!(:@oauth_providers => [])
|
|
19
16
|
end
|
|
20
17
|
|
|
21
18
|
def oauth_providers=(providers)
|
|
@@ -35,9 +32,12 @@ module Sorcery
|
|
|
35
32
|
# after authentication the user is redirected to the callback defined in the provider config
|
|
36
33
|
def auth_at_provider(provider)
|
|
37
34
|
@provider = Config.send(provider)
|
|
35
|
+
args = {}
|
|
38
36
|
if @provider.respond_to?(:get_request_token)
|
|
39
|
-
|
|
40
|
-
session[:request_token]
|
|
37
|
+
req_token = @provider.get_request_token
|
|
38
|
+
session[:request_token] = req_token.token
|
|
39
|
+
session[:request_token_secret] = req_token.secret
|
|
40
|
+
args.merge!({:request_token => req_token.token, :request_token_secret => req_token.secret})
|
|
41
41
|
end
|
|
42
42
|
redirect_to @provider.authorize_url(args)
|
|
43
43
|
end
|
|
@@ -46,7 +46,7 @@ module Sorcery
|
|
|
46
46
|
def login_from_access_token(provider)
|
|
47
47
|
@provider = Config.send(provider)
|
|
48
48
|
args = {}
|
|
49
|
-
args.merge!({:oauth_verifier => params[:oauth_verifier], :request_token => session[:request_token]}) if @provider.respond_to?(:get_request_token)
|
|
49
|
+
args.merge!({:oauth_verifier => params[:oauth_verifier], :request_token => session[:request_token], :request_token_secret => session[:request_token_secret]}) if @provider.respond_to?(:get_request_token)
|
|
50
50
|
args.merge!({:code => params[:code]}) if params[:code]
|
|
51
51
|
@access_token = @provider.get_access_token(args)
|
|
52
52
|
@user_hash = @provider.get_user_hash(@access_token)
|
|
@@ -84,7 +84,7 @@ module Sorcery
|
|
|
84
84
|
end
|
|
85
85
|
Config.user_class.transaction do
|
|
86
86
|
@user = Config.user_class.create!(attrs)
|
|
87
|
-
Config.authentications_class.create!({config.authentications_user_id_attribute_name => @user.id, config.provider_attribute_name => provider, config.provider_uid_attribute_name => @user_hash[:uid]})
|
|
87
|
+
Config.user_class.sorcery_config.authentications_class.create!({config.authentications_user_id_attribute_name => @user.id, config.provider_attribute_name => provider, config.provider_uid_attribute_name => @user_hash[:uid]})
|
|
88
88
|
end
|
|
89
89
|
@user
|
|
90
90
|
end
|
|
@@ -31,7 +31,7 @@ module Sorcery
|
|
|
31
31
|
config = sorcery_config
|
|
32
32
|
where("#{config.last_activity_at_attribute_name} IS NOT NULL") \
|
|
33
33
|
.where("#{config.last_logout_at_attribute_name} IS NULL OR #{config.last_activity_at_attribute_name} > #{config.last_logout_at_attribute_name}") \
|
|
34
|
-
.where("#{config.last_activity_at_attribute_name} > ? ", config.activity_timeout.seconds.ago)
|
|
34
|
+
.where("#{config.last_activity_at_attribute_name} > ? ", config.activity_timeout.seconds.ago.utc.to_s(:db))
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
end
|
|
@@ -22,7 +22,7 @@ module Sorcery
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
base.sorcery_config.instance_eval do
|
|
25
|
-
@defaults.merge!(:@authentications_class =>
|
|
25
|
+
@defaults.merge!(:@authentications_class => nil,
|
|
26
26
|
:@authentications_user_id_attribute_name => :user_id,
|
|
27
27
|
:@provider_attribute_name => :provider,
|
|
28
28
|
:@provider_uid_attribute_name => :uid)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
ActiveRecord::Base.send(:include, Sorcery::Model) if defined?(ActiveRecord)
|
|
2
|
+
if defined?(Sinatra::Base)
|
|
3
|
+
Sinatra::Base.send(:include, Sorcery::Controller::Adapters::Sinatra)
|
|
4
|
+
Sinatra::Base.send(:include, Sorcery::Controller)
|
|
5
|
+
# Sorcery::Controller::Config.class_eval do
|
|
6
|
+
# class << self
|
|
7
|
+
# def submodules=(mods)
|
|
8
|
+
# @submodules = mods
|
|
9
|
+
# Sinatra::Base.send(:include, Sorcery::Controller::Adapters::Sinatra)
|
|
10
|
+
# Sinatra::Base.send(:include, Sorcery::Controller)
|
|
11
|
+
# end
|
|
12
|
+
# end
|
|
13
|
+
# end
|
|
14
|
+
end
|