padrino-warden 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :runtime do
4
+ gem 'sinatra', '>= 0.9.4'
5
+ gem 'warden', '>= 0.10.3'
6
+ end
7
+
8
+ group :test do
9
+ gem 'rake'
10
+ gem 'jeweler', '~> 1.3.0'
11
+ gem 'bundler', '~> 0.9.7'
12
+ gem 'rspec', '~> 1.2.9', :require => 'spec'
13
+ gem 'yard', '>= 0.5.4'
14
+ gem 'rack-test', '~> 0.5.0', :require => 'rack/test'
15
+ gem 'rcov'
16
+
17
+ gem 'do_sqlite3', '~> 0.10.0'
18
+ gem 'dm-core', '~> 0.10.1'
19
+ gem 'bcrypt-ruby', :require => 'bcrypt'
20
+ gem 'haml'
21
+ gem 'rack-flash', '~> 0.1.1', :require => 'rack-flash'
22
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dotan J. Nahum (jondot) <dotan@paracode.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,82 @@
1
+ = padrino-warden
2
+
3
+ A Padrino (http://github.com/padrino/padrino-framework) module that provides authentication for your Padrino application through Warden (http://github.com/hassox/warden).
4
+
5
+ Most of the code was adapted from sinatra_warden (http://github.com/jsmestad/sinatra_warden)
6
+
7
+ == Usage
8
+
9
+ Currently padrino-warden uses +password+ as default authentication strategy. If you wish to change that consult
10
+ Warden (http://github.com/hassox/warden).
11
+
12
+ class SampleApp < Padrino::Application
13
+ configure do
14
+ ##
15
+ # Application-specific configuration options
16
+ #
17
+ end
18
+
19
+ register Padrino::Warden
20
+
21
+
22
+ class User
23
+ attr_reader :name
24
+ def initialize(name)
25
+ @name=name
26
+ end
27
+
28
+ def self.authenticate(a, b)
29
+ return User.new('john')
30
+ end
31
+ end
32
+
33
+ Warden::Strategies.add(:password) do
34
+ def valid?
35
+ params["email"] || params["password"]
36
+ end
37
+
38
+ def authenticate!
39
+ u = User.authenticate(params["email"], params["password"])
40
+ u.nil? ? fail!("Could not log in") : success!(u)
41
+ end
42
+ end
43
+
44
+ Warden::Manager.serialize_into_session do |user|
45
+ user.id
46
+ end
47
+
48
+ Warden::Manager.serialize_from_session do |id|
49
+ User.get(id)
50
+ end
51
+
52
+ end
53
+
54
+ Run this to see your new routes:
55
+
56
+ $ padrino rake routes
57
+
58
+ You can now login at
59
+ http://localhost/sessions/login
60
+
61
+ After login you can fiddle with +current_user+ for anything you need.
62
+
63
+
64
+ == Note on Patches/Pull Requests
65
+
66
+ * Fork the project.
67
+ * Make your feature addition or bug fix.
68
+ * Add tests for it. This is important so I don't break it in a
69
+ future version unintentionally.
70
+ * Commit, do not mess with rakefile, version, or history.
71
+ (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)
72
+ * Send me a pull request. Bonus points for topic branches.
73
+
74
+ == Contributors
75
+
76
+ * Dotan Nahum (http://github.com/jondot)
77
+
78
+ For sinatra_warden, thanks to: Justin Smestad (http://github.com/jsmestad), Daniel Neighman (http://github.com/hassox), Shane Hanna (http://github.com/shanna)
79
+
80
+ == Copyright
81
+
82
+ Copyright (c) 2010 Dotan Nahum (jondot). See LICENSE for details.
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "padrino-warden"
7
+ gem.summary = %Q{authentication system for using warden with Padrino, adopted from sinatra_warden}
8
+ gem.description = %Q{basic helpers and authentication methods for using warden with padrino also providing some hooks into Rack::Flash}
9
+ gem.email = "dotan@paracode.com"
10
+ gem.homepage = "http://github.com/jondot/padrino-warden"
11
+ gem.authors = ["Dotan Nahum"]
12
+ gem.add_dependency('warden', '>= 0.10.3')
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+ task :spec => :check_dependencies
32
+
33
+ task :default => :spec
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
38
+
39
+ rdoc.rdoc_dir = 'rdoc'
40
+ rdoc.title = "padrino-warden #{version}"
41
+ rdoc.rdoc_files.include('README*')
42
+ rdoc.rdoc_files.include('lib/**/*.rb')
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,10 @@
1
+ require 'warden'
2
+ require File.join(File.dirname(__FILE__), 'padrino', 'warden')
3
+
4
+ Warden::Manager.before_failure do |env, opts|
5
+ # Sinatra is very sensitive to the request method
6
+ # since authentication could fail on any type of method, we need
7
+ # to set it for the failure app so it is routed to the correct block
8
+ env['REQUEST_METHOD'] = "POST"
9
+
10
+ end
@@ -0,0 +1,133 @@
1
+ module Padrino
2
+ module Warden
3
+ module Helpers
4
+
5
+ # The main accessor to the warden middleware
6
+ def warden
7
+ request.env['warden']
8
+ end
9
+
10
+ # Return session info
11
+ #
12
+ # @param [Symbol] the scope to retrieve session info for
13
+ def session_info(scope=nil)
14
+ scope ? warden.session(scope) : scope
15
+ end
16
+
17
+ # Check the current session is authenticated to a given scope
18
+ def authenticated?(scope=nil)
19
+ scope ? warden.authenticated?(scope) : warden.authenticated?
20
+ end
21
+ alias_method :logged_in?, :authenticated?
22
+
23
+ # Authenticate a user against defined strategies
24
+ def authenticate(*args)
25
+ warden.authenticate!(*args)
26
+ end
27
+ alias_method :login, :authenticate
28
+
29
+ # Terminate the current session
30
+ #
31
+ # @param [Symbol] the session scope to terminate
32
+ def logout(scopes=nil)
33
+ scopes ? warden.logout(scopes) : warden.logout
34
+ end
35
+
36
+ # Access the user from the current session
37
+ #
38
+ # @param [Symbol] the scope for the logged in user
39
+ def user(scope=nil)
40
+ scope ? warden.user(scope) : warden.user
41
+ end
42
+ alias_method :current_user, :user
43
+
44
+ # Store the logged in user in the session
45
+ #
46
+ # @param [Object] the user you want to store in the session
47
+ # @option opts [Symbol] :scope The scope to assign the user
48
+ # @example Set John as the current user
49
+ # user = User.find_by_name('John')
50
+ def user=(new_user, opts={})
51
+ warden.set_user(new_user, opts)
52
+ end
53
+ alias_method :current_user=, :user=
54
+
55
+ # Require authorization for an action
56
+ #
57
+ # @param [String] path to redirect to if user is unauthenticated
58
+ def authorize!(failure_path=nil)
59
+ unless authenticated?
60
+ session[:return_to] = request.path if options.auth_use_referrer
61
+ redirect(failure_path ? failure_path : options.auth_failure_path)
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ def self.registered(app)
68
+ app.helpers Helpers
69
+
70
+ # Enable Sessions
71
+ app.set :sessions, true
72
+ app.set :auth_failure_path, '/'
73
+ app.set :auth_success_path, '/'
74
+ # Setting this to true will store last request URL
75
+ # into a user's session so that to redirect back to it
76
+ # upon successful authentication
77
+ app.set :auth_use_referrer, false
78
+ app.set :auth_error_message, "Could not log you in."
79
+ app.set :auth_success_message, "You have logged in successfully."
80
+ app.set :auth_login_template, 'sessions/login'
81
+ # OAuth Specific Settings
82
+ app.set :auth_use_oauth, false
83
+
84
+ app.use ::Warden::Manager do |manager|
85
+ manager.default_strategies :password
86
+ manager.failure_app = app
87
+ end
88
+
89
+ app.controller :sessions do
90
+ post :unauthenticated do
91
+ status 401
92
+ warden.custom_failure! if warden.config.failure_app == self.class
93
+ env['x-rack.flash'][:error] = options.auth_error_message if defined?(Rack::Flash)
94
+ render options.auth_login_template
95
+ end
96
+
97
+ get :login do
98
+ if options.auth_use_oauth && !@auth_oauth_request_token.nil?
99
+ session[:request_token] = @auth_oauth_request_token.token
100
+ session[:request_token_secret] = @auth_oauth_request_token.secret
101
+ redirect @auth_oauth_request_token.authorize_url
102
+ else
103
+ render options.auth_login_template
104
+ end
105
+ end
106
+
107
+ get :oauth_callback do
108
+ if options.auth_use_oauth
109
+ authenticate
110
+ env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
111
+ redirect options.auth_success_path
112
+ else
113
+ redirect options.auth_failure_path
114
+ end
115
+ end
116
+
117
+ post :login do
118
+ authenticate
119
+ env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
120
+ redirect options.auth_use_referrer && session[:return_to] ? session.delete(:return_to) :
121
+ options.auth_success_path
122
+ end
123
+
124
+ get :logout do
125
+ authorize!
126
+ logout
127
+ env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
128
+ redirect options.auth_success_path
129
+ end
130
+ end
131
+ end
132
+ end # Warden
133
+ end # Padrino
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{padrino-warden}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Dotan Nahum"]
12
+ s.date = %q{2010-08-01}
13
+ s.description = %q{basic helpers and authentication methods for using warden with padrino also providing some hooks into Rack::Flash}
14
+ s.email = %q{dotan@paracode.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "Gemfile",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/padrino-warden.rb",
28
+ "lib/padrino/warden.rb",
29
+ "padrino-warden.gemspec",
30
+ "spec/padrino-warden_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/jondot/padrino-warden}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{authentication system for using warden with Padrino, adopted from sinatra_warden}
39
+ s.test_files = [
40
+ "spec/padrino-warden_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<warden>, [">= 0.10.3"])
50
+ else
51
+ s.add_dependency(%q<warden>, [">= 0.10.3"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<warden>, [">= 0.10.3"])
55
+ end
56
+ end
57
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "PadrinoWarden" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'padrino-warden'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: padrino-warden
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Dotan Nahum
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-01 00:00:00 +03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: warden
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 10
30
+ - 3
31
+ version: 0.10.3
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: basic helpers and authentication methods for using warden with padrino also providing some hooks into Rack::Flash
35
+ email: dotan@paracode.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - Gemfile
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/padrino-warden.rb
52
+ - lib/padrino/warden.rb
53
+ - padrino-warden.gemspec
54
+ - spec/padrino-warden_spec.rb
55
+ - spec/spec.opts
56
+ - spec/spec_helper.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/jondot/padrino-warden
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.6
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: authentication system for using warden with Padrino, adopted from sinatra_warden
87
+ test_files:
88
+ - spec/padrino-warden_spec.rb
89
+ - spec/spec_helper.rb