sorcery 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sorcery might be problematic. Click here for more details.

data/README.rdoc CHANGED
@@ -37,16 +37,20 @@ Brute Force Protection (see lib/sorcery/controller/submodules/brute_force_protec
37
37
  * Brute force login hammering protection.
38
38
  * configurable logins before ban, logins within time period before ban, ban time and ban action.
39
39
 
40
+ Basic HTTP Authentication (see lib/sorcery/controller/submodules/http_basic_auth.rb):
41
+ * A before filter for requesting authentication with HTTP Basic.
42
+ * automatic login from HTTP Basic.
43
+ * automatic login is disabled if session key changed.
44
+
40
45
  Other:
41
46
  * Modular design, load only the modules you need.
42
47
  * 100% TDD'd code, 100% test coverage.
43
48
 
44
- == Planned Features:
49
+ == Next Planned Features:
45
50
 
46
51
  I've got many plans which include:
47
- * Basic HTTP Authentication
48
- * Auto login
49
52
  * Hammering reset password protection
53
+ * Configurable Auto login on registration/activation
50
54
  * Other reset password strategies (security questions?)
51
55
  * Sinatra support
52
56
  * Mongoid support
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -0,0 +1,57 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ module HttpBasicAuth
5
+ def self.included(base)
6
+ base.send(:include, InstanceMethods)
7
+ Config.module_eval do
8
+ class << self
9
+ attr_accessor :controller_to_realm_map # how many failed logins allowed.
10
+
11
+ def merge_http_basic_auth_defaults!
12
+ @defaults.merge!(:@controller_to_realm_map => {"application" => "Application"})
13
+ end
14
+ end
15
+ merge_http_basic_auth_defaults!
16
+ end
17
+ Config.login_sources << :login_from_basic_auth
18
+ end
19
+
20
+ module InstanceMethods
21
+
22
+ protected
23
+
24
+ # to be used as a before_filter.
25
+ # The method sets a session when requesting the user's credentials.
26
+ # This is a trick to overcome the way HTTP authentication work (explained below):
27
+ #
28
+ # Once the user fills the credentials once, the browser will always send it to the server when visiting the website, until the browser is closed.
29
+ # This causes wierd behaviour if the user logs out. The session is reset, yet the user is re-logged in by the before_filter calling 'login_from_basic_auth'.
30
+ # To overcome this, we set a session when requesting the password, which logout will reset, and that's how we know if we need to request for HTTP auth again.
31
+ def require_user_login_from_http
32
+ request_http_basic_authentication(realm_name_by_controller) and (session[:http_authentication_used] = true) and return if request.authorization.nil? || session[:http_authentication_used].nil?
33
+ require_user_login
34
+ end
35
+
36
+ def login_from_basic_auth
37
+ authenticate_with_http_basic do |username, password|
38
+ @logged_in_user = (Config.user_class.authenticate(username, password) if session[:http_authentication_used]) || false
39
+ end
40
+ end
41
+
42
+ def realm_name_by_controller
43
+ current_controller = self.class
44
+ while current_controller != ActionController::Base
45
+ result = Config.controller_to_realm_map[current_controller.controller_name]
46
+ return result if result
47
+ current_controller = self.class.superclass
48
+ end
49
+ nil
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
@@ -25,7 +25,7 @@ module Sorcery
25
25
  session[:login_time] = session[:last_action_time] = Time.now.utc
26
26
  end
27
27
 
28
- # To be used as a before_filter, before authenticate
28
+ # To be used as a before_filter, before require_user_login
29
29
  def validate_session
30
30
  session_to_use = Config.session_timeout_from_last_action ? session[:last_action_time] : session[:login_time]
31
31
  if session_to_use && (Time.now.utc - session_to_use > Config.session_timeout)
data/lib/sorcery.rb CHANGED
@@ -13,6 +13,7 @@ module Sorcery
13
13
  autoload :RememberMe, 'sorcery/controller/submodules/remember_me'
14
14
  autoload :SessionTimeout, 'sorcery/controller/submodules/session_timeout'
15
15
  autoload :BruteForceProtection, 'sorcery/controller/submodules/brute_force_protection'
16
+ autoload :HttpBasicAuth, 'sorcery/controller/submodules/http_basic_auth'
16
17
  end
17
18
  end
18
19
  module CryptoProviders
data/sorcery.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sorcery}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Noam Ben Ari"]
12
- s.date = %q{2011-02-04}
12
+ s.date = %q{2011-02-05}
13
13
  s.description = %q{Provides common authentication needs such as signing in/out, activating by email and resetting password.}
14
14
  s.email = %q{nbenari@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "lib/sorcery.rb",
30
30
  "lib/sorcery/controller.rb",
31
31
  "lib/sorcery/controller/submodules/brute_force_protection.rb",
32
+ "lib/sorcery/controller/submodules/http_basic_auth.rb",
32
33
  "lib/sorcery/controller/submodules/remember_me.rb",
33
34
  "lib/sorcery/controller/submodules/session_timeout.rb",
34
35
  "lib/sorcery/crypto_providers/aes256.rb",
@@ -108,6 +109,7 @@ Gem::Specification.new do |s|
108
109
  "spec/rails3/app_root/test/unit/user_test.rb",
109
110
  "spec/rails3/app_root/vendor/plugins/.gitkeep",
110
111
  "spec/rails3/controller_brute_force_protection_spec.rb",
112
+ "spec/rails3/controller_http_basic_auth_spec.rb",
111
113
  "spec/rails3/controller_remember_me_spec.rb",
112
114
  "spec/rails3/controller_session_timeout_spec.rb",
113
115
  "spec/rails3/controller_spec.rb",
@@ -152,6 +154,7 @@ Gem::Specification.new do |s|
152
154
  "spec/rails3/app_root/test/test_helper.rb",
153
155
  "spec/rails3/app_root/test/unit/user_test.rb",
154
156
  "spec/rails3/controller_brute_force_protection_spec.rb",
157
+ "spec/rails3/controller_http_basic_auth_spec.rb",
155
158
  "spec/rails3/controller_remember_me_spec.rb",
156
159
  "spec/rails3/controller_session_timeout_spec.rb",
157
160
  "spec/rails3/controller_spec.rb",
data/spec/Gemfile CHANGED
@@ -2,7 +2,7 @@ source 'http://rubygems.org'
2
2
 
3
3
  gem "rails", '3.0.3'
4
4
  gem 'bcrypt-ruby', :require => 'bcrypt'
5
- gem "sorcery", '0.1.1', :path => '../../../'
5
+ gem "sorcery", '0.1.3', :path => '../../../'
6
6
 
7
7
  group :development do
8
8
  gem 'rspec'
data/spec/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../../
3
3
  specs:
4
- sorcery (0.1.1)
4
+ sorcery (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -105,4 +105,4 @@ DEPENDENCIES
105
105
  rspec
106
106
  ruby-debug19
107
107
  simplecov (>= 0.3.8)
108
- sorcery (= 0.1.1)!
108
+ sorcery (= 0.1.3)!
data/spec/rails3/Gemfile CHANGED
@@ -2,7 +2,7 @@ source 'http://rubygems.org'
2
2
 
3
3
  gem 'rails', '3.0.3'
4
4
  gem 'sqlite3-ruby', :require => 'sqlite3'
5
- gem "sorcery", '0.1.1', :path => '../../../'
5
+ gem "sorcery", '0.1.3', :path => '../../../'
6
6
  gem 'bcrypt-ruby', '~> 2.1.4', :require => 'bcrypt'
7
7
 
8
8
  group :development do
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../../
3
3
  specs:
4
- sorcery (0.1.1)
4
+ sorcery (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -112,5 +112,5 @@ DEPENDENCIES
112
112
  rspec-rails
113
113
  ruby-debug19
114
114
  simplecov (>= 0.3.8)
115
- sorcery (= 0.1.1)!
115
+ sorcery (= 0.1.3)!
116
116
  sqlite3-ruby
@@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
2
2
  protect_from_forgery
3
3
 
4
4
  #before_filter :validate_session, :only => [:test_should_be_logged_in] if defined?(:validate_session)
5
+ before_filter :require_user_login_from_http, :only => [:test_http_basic_auth]
5
6
  before_filter :require_user_login, :only => [:test_logout, :test_should_be_logged_in, :some_action]
6
7
 
7
8
  def index
@@ -54,6 +55,10 @@ class ApplicationController < ActionController::Base
54
55
  render :text => ""
55
56
  end
56
57
 
58
+ def test_http_basic_auth
59
+ render :text => "HTTP Basic Auth"
60
+ end
61
+
57
62
  protected
58
63
 
59
64
 
@@ -8,6 +8,7 @@ AppRoot::Application.routes.draw do
8
8
  match '/test_login_with_remember_in_login', :to => 'application#test_login_with_remember_in_login'
9
9
  match '/test_login_from_cookie', :to => 'application#test_login_from_cookie'
10
10
  match '/test_should_be_logged_in', :to => 'application#test_should_be_logged_in'
11
+ match '/test_http_basic_auth', :to => 'application#test_http_basic_auth'
11
12
  # The priority is based upon order of creation:
12
13
  # first created -> highest priority.
13
14
 
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe ApplicationController do
4
+
5
+ # ----------------- HTTP BASIC AUTH -----------------------
6
+ describe ApplicationController, "with http basic auth features" do
7
+ before(:all) do
8
+ plugin_model_configure([:http_basic_auth])
9
+ create_new_user
10
+ end
11
+
12
+ it "requests basic authentication when before_filter is used" do
13
+ get :test_http_basic_auth
14
+ response.code.should == "401"
15
+ end
16
+
17
+ it "authenticates from http basic if credentials are sent" do
18
+ @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("#{@user.username}:secret")
19
+ get :test_http_basic_auth, nil, :http_authentication_used => true
20
+ response.should be_a_success
21
+ end
22
+
23
+ it "fails authentication if credentials are wrong" do
24
+ @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("#{@user.username}:wrong!")
25
+ get :test_http_basic_auth, nil, :http_authentication_used => true
26
+ response.code.should redirect_to root_url
27
+ end
28
+
29
+ it "should allow configuration option 'controller_to_realm_map'" do
30
+ plugin_set_controller_config_property(:controller_to_realm_map, {"1" => "2"})
31
+ Sorcery::Controller::Config.controller_to_realm_map.should == {"1" => "2"}
32
+ end
33
+
34
+ it "should display the correct realm name configured for the controller" do
35
+ plugin_set_controller_config_property(:controller_to_realm_map, {"application" => "Salad"})
36
+ get :test_http_basic_auth
37
+ response.headers["WWW-Authenticate"].should == "Basic realm=\"Salad\""
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: sorcery
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Noam Ben Ari
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-04 00:00:00 +02:00
13
+ date: 2011-02-05 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -145,6 +145,7 @@ files:
145
145
  - lib/sorcery.rb
146
146
  - lib/sorcery/controller.rb
147
147
  - lib/sorcery/controller/submodules/brute_force_protection.rb
148
+ - lib/sorcery/controller/submodules/http_basic_auth.rb
148
149
  - lib/sorcery/controller/submodules/remember_me.rb
149
150
  - lib/sorcery/controller/submodules/session_timeout.rb
150
151
  - lib/sorcery/crypto_providers/aes256.rb
@@ -224,6 +225,7 @@ files:
224
225
  - spec/rails3/app_root/test/unit/user_test.rb
225
226
  - spec/rails3/app_root/vendor/plugins/.gitkeep
226
227
  - spec/rails3/controller_brute_force_protection_spec.rb
228
+ - spec/rails3/controller_http_basic_auth_spec.rb
227
229
  - spec/rails3/controller_remember_me_spec.rb
228
230
  - spec/rails3/controller_session_timeout_spec.rb
229
231
  - spec/rails3/controller_spec.rb
@@ -290,6 +292,7 @@ test_files:
290
292
  - spec/rails3/app_root/test/test_helper.rb
291
293
  - spec/rails3/app_root/test/unit/user_test.rb
292
294
  - spec/rails3/controller_brute_force_protection_spec.rb
295
+ - spec/rails3/controller_http_basic_auth_spec.rb
293
296
  - spec/rails3/controller_remember_me_spec.rb
294
297
  - spec/rails3/controller_session_timeout_spec.rb
295
298
  - spec/rails3/controller_spec.rb