sinatra_warden 0.0.1

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,23 @@
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
+ bin
21
+ vendor
22
+
23
+ ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://gemcutter.org"
2
+
3
+ gem 'sinatra', '~> 0.9.4'
4
+ gem 'warden', '~> 0.5.0'
5
+
6
+ gem 'rake', :only => [:test]
7
+ gem 'rspec', '~> 1.2.9', :only => [:test], :require_as => 'spec'
8
+ gem 'yard', :only => [:test]
9
+ gem 'rack-test', '~> 0.5.0', :only => [:test], :require_as => 'rack/test'
10
+ gem 'rcov', :only => [:test]
11
+
12
+ gem 'do_sqlite3', '~> 0.10.0', :only => [:test]
13
+ gem 'dm-core', '~> 0.10.1', :only => [:test]
14
+ gem 'bcrypt-ruby', :only => [:test], :require_as => 'bcrypt'
15
+
16
+ disable_system_gems
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 jsmestad
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,43 @@
1
+ = sinatra_warden
2
+
3
+ Provides a module for basic helpers and actions needed for user
4
+ authentication using Warden with Sinatra. In addition to a
5
+ collection of authentication strategies.
6
+
7
+ == Usage
8
+
9
+ require 'sinatra'
10
+ require 'sinatra_warden'
11
+
12
+ class Application < Sinatra::Base
13
+ register SinatraWarden
14
+
15
+ get '/admin' do
16
+ authorize!('/login') # require session, redirect to '/login' instead of work
17
+ haml :admin
18
+ end
19
+
20
+ get '/dashboard' do
21
+ authorize! # require a session for this action
22
+ haml :dashboard
23
+ end
24
+ end
25
+
26
+ == Issues
27
+
28
+ * Currently assumes you have a model named 'User' and an email & password field.
29
+
30
+ == Note on Patches/Pull Requests
31
+
32
+ * Fork the project.
33
+ * Make your feature addition or bug fix.
34
+ * Add tests for it. This is important so I don't break it in a
35
+ future version unintentionally.
36
+ * Commit, do not mess with rakefile, version, or history.
37
+ (if you want to have your own version, that is fine but
38
+ bump version in a commit by itself I can ignore when I pull)
39
+ * Send me a pull request. Bonus points for topic branches.
40
+
41
+ == Copyright
42
+
43
+ Copyright (c) 2009 Justin Smestad. See LICENSE for details.
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'bundler'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "sinatra_warden"
9
+ gem.summary = %Q{authentication system for using warden with sinatra}
10
+ gem.description = %Q{basic helpers and authentication methods for using warden with sinatra also providing some hooks into Rack::Flash}
11
+ gem.email = "justin.smestad@gmail.com"
12
+ gem.homepage = "http://github.com/jsmestad/sinatra_warden"
13
+ gem.authors = ["Justin Smestad"]
14
+
15
+ manifest = Bundler::Environment.load(File.dirname(__FILE__) + '/Gemfile')
16
+ manifest.dependencies.each do |d|
17
+ next if d.only && d.only.include?('test')
18
+ gem.add_dependency(d.name, d.version)
19
+ end
20
+
21
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
22
+ end
23
+ Jeweler::GemcutterTasks.new
24
+ rescue LoadError
25
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
26
+ end
27
+
28
+ require 'spec/rake/spectask'
29
+ Spec::Rake::SpecTask.new(:spec) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.spec_files = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
35
+ spec.libs << 'lib' << 'spec'
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :spec => :check_dependencies
41
+
42
+ task :default => :spec
43
+
44
+ begin
45
+ require 'yard'
46
+ YARD::Rake::YardocTask.new
47
+ rescue LoadError
48
+ task :yardoc do
49
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
50
+ end
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,11 @@
1
+ require 'sinatra'
2
+ require 'warden'
3
+
4
+ require File.join(File.dirname(__FILE__) + '/sinatra_warden/sinatra')
5
+
6
+ Warden::Manager.before_failure do |env,opts|
7
+ # Sinatra is very sensitive to the request method
8
+ # since authentication could fail on any type of method, we need
9
+ # to set it for the failure app so it is routed to the correct block
10
+ env['REQUEST_METHOD'] = "POST"
11
+ end
@@ -0,0 +1,57 @@
1
+ module SinatraWarden
2
+ module Helpers
3
+ # The main accessor for the warden proxy instance
4
+ def warden
5
+ request.env['warden']
6
+ end
7
+
8
+ # Proxy to the authenticated? method on warden
9
+ def authenticated?(*args)
10
+ warden.authenticated?(*args)
11
+ end
12
+ alias_method :logged_in?, :authenticated?
13
+
14
+ # Access the currently logged in user
15
+ def user(*args)
16
+ warden.user(*args)
17
+ end
18
+ alias_method :current_user, :user
19
+
20
+ # Set the currently logged in user
21
+ def user=(user)
22
+ warden.set_user user
23
+ end
24
+ alias_method :current_user=, :user=
25
+
26
+ # Require authorization for an action
27
+ def authorize!(failure_path=nil)
28
+ redirect(failure_path ? failure_path : '/') unless authenticated?
29
+ end
30
+ end
31
+
32
+ def self.registered(app)
33
+ app.helpers SinatraWarden::Helpers
34
+
35
+ app.post '/unauthenticated/?' do
36
+ status 401
37
+ flash[:error] = "Could not log you in" if defined?(Rack::Flash)
38
+ haml :login
39
+ end
40
+
41
+ app.get '/login/?' do
42
+ haml :login
43
+ end
44
+
45
+ app.post '/login/?' do
46
+ env['warden'].authenticate!
47
+ flash[:success] = "You have logged in successfully." if defined?(Rack::Flash)
48
+ redirect back
49
+ end
50
+
51
+ app.get '/logout/?' do
52
+ env['warden'].logout
53
+ flash[:success] = "You are now logged out." if defined?(Rack::Flash)
54
+ redirect back
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ Warden::Manager.serialize_into_session{ |user| [user.class, user.id] }
2
+ Warden::Manager.serialize_from_session{ |klass, id| klass.find(id) }
3
+
4
+ Warden::Strategies.add(:bcrypt_activerecord) do
5
+
6
+ def valid?
7
+ params["login"] || params["password"]
8
+ end
9
+
10
+ def authenticate!
11
+ return fail! unless user = User.find(params["login"])
12
+
13
+ if user.password == params["password"]
14
+ success!(user)
15
+ else
16
+ errors.add(:login, "Login or Password incorrect")
17
+ fail!
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,16 @@
1
+ require 'dm-core'
2
+ require 'bcrypt'
3
+
4
+ Warden::Manager.serialize_into_session{|user| user.id }
5
+ Warden::Manager.serialize_from_session{|id| User.get(id) }
6
+
7
+ Warden::Strategies.add(:bcrypt_datamapper) do
8
+ def valid?
9
+ params["email"] || params["password"]
10
+ end
11
+
12
+ def authenticate!
13
+ return fail!("Could not log in") unless user = User.first(:email => params["email"])
14
+ user.password == params["password"] ? success!(user) : fail!("Could not log in")
15
+ end
16
+ end
File without changes
@@ -0,0 +1,12 @@
1
+ class TestingLogin < Sinatra::Base
2
+ register SinatraWarden
3
+
4
+ get '/dashboard' do
5
+ authorize!('/login')
6
+ end
7
+
8
+ get '/admin' do
9
+ authorize!
10
+ end
11
+
12
+ end
@@ -0,0 +1,20 @@
1
+ require 'dm-core'
2
+ require 'bcrypt'
3
+
4
+ class User
5
+ include DataMapper::Resource
6
+
7
+ property :id, Serial
8
+ property :email, String
9
+ property :encrypted_password, String
10
+
11
+ def password=(new_password)
12
+ @password = BCrypt::Password.create(new_password)
13
+ self.encrypted_password = @password
14
+ end
15
+
16
+ def password
17
+ @password ||= BCrypt::Password.new(encrypted_password)
18
+ end
19
+
20
+ end
@@ -0,0 +1,61 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "SinatraWarden" do
4
+ before(:each) do
5
+ @user = User.create(:email => 'justin.smestad@gmail.com', :password => 'thedude')
6
+ end
7
+
8
+ it "should be a valid user" do
9
+ @user.new?.should be_false
10
+ end
11
+
12
+ context "the authentication system" do
13
+
14
+ it "should allow us to login as that user"
15
+
16
+ it "should allow us to logout after logging in"
17
+
18
+ it "should redirect to root"
19
+
20
+ end
21
+
22
+ context "the helpers" do
23
+
24
+ context "the authorize! helper" do
25
+
26
+ it "should redirect to root if not logged in"
27
+
28
+ it "should redirect to the passed path if available"
29
+
30
+ it "should allow access if user is logged in"
31
+
32
+ end
33
+
34
+ context "the user helper" do
35
+
36
+ it "should be aliased to current_user"
37
+
38
+ it "should allow assignment of the user (user=)"
39
+
40
+ it "should return the current logged in user"
41
+
42
+ end
43
+
44
+ context "the authenticated? helper" do
45
+
46
+ it "should be aliased as logged_in?"
47
+
48
+ it "should return true when a user is authenticated"
49
+
50
+ it "should return false when a user is not authenticated"
51
+
52
+ end
53
+
54
+ context "the warden helper" do
55
+
56
+ it "returns the environment variables from warden"
57
+
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,35 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ ENV['RACK_ENV'] ||= 'test'
5
+ project_root = File.expand_path(File.dirname(__FILE__))
6
+ require File.join(project_root, '..', 'vendor', 'gems', 'environment')
7
+ Bundler.require_env(:test)
8
+
9
+ require 'sinatra_warden'
10
+ require 'spec'
11
+ require 'spec/autorun'
12
+
13
+ DataMapper.setup(:default, 'sqlite3::memory:')
14
+
15
+ %w(fixtures support).each do |path|
16
+ Dir[ File.join( project_root, path, '/**/*.rb') ].each do |m|
17
+ require m
18
+ end
19
+ end
20
+
21
+ Spec::Runner.configure do |config|
22
+ config.include(Rack::Test::Methods)
23
+
24
+ config.before(:each) do
25
+ DataMapper.auto_migrate!
26
+ end
27
+
28
+ def app
29
+ @app ||= Rack::Builder.app do
30
+ use Rack::Session::Cookie
31
+ run TestingLogin.app
32
+ end
33
+ end
34
+ end
35
+
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra_warden
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Justin Smestad
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-26 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.4
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: warden
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ version:
35
+ description: basic helpers and authentication methods for using warden with sinatra also providing some hooks into Rack::Flash
36
+ email: justin.smestad@gmail.com
37
+ executables:
38
+ - autospec
39
+ - rackup
40
+ - rake
41
+ - rcov
42
+ - spec
43
+ - yard-graph
44
+ - yardoc
45
+ - yri
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - LICENSE
50
+ - README.rdoc
51
+ files:
52
+ - .document
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.rdoc
57
+ - Rakefile
58
+ - VERSION
59
+ - lib/sinatra_warden.rb
60
+ - lib/sinatra_warden/sinatra.rb
61
+ - lib/sinatra_warden/strategies/bcrypt_activerecord.rb
62
+ - lib/sinatra_warden/strategies/bcrypt_datamapper.rb
63
+ - spec/fixtures/bcrypt_strategy.rb
64
+ - spec/fixtures/testing_login.rb
65
+ - spec/fixtures/user.rb
66
+ - spec/sinatra_warden_spec.rb
67
+ - spec/spec.opts
68
+ - spec/spec_helper.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/jsmestad/sinatra_warden
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.5
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: authentication system for using warden with sinatra
97
+ test_files:
98
+ - spec/fixtures/bcrypt_strategy.rb
99
+ - spec/fixtures/testing_login.rb
100
+ - spec/fixtures/user.rb
101
+ - spec/sinatra_warden_spec.rb
102
+ - spec/spec_helper.rb