Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

configuration options for overriding flash messages, template renderi…

…ng, and template name/path
  • Loading branch information...
commit 977cfd44a1f741576c44d9d08353f90eef559eb3 1 parent 286bc4b
@jsmestad jsmestad authored
View
20 Gemfile
@@ -1,16 +1,16 @@
source "http://gemcutter.org"
-gem 'sinatra', '>= 0.9.4'
-gem 'warden', '~> 0.5.0'
+gem 'sinatra', '>= 0.9.4'
+gem 'warden', '~> 0.5.0'
-gem 'rake', :only => [:test]
-gem 'rspec', '~> 1.2.9', :only => [:test], :require_as => 'spec'
-gem 'yard', :only => [:test]
-gem 'rack-test', '~> 0.5.0', :only => [:test], :require_as => 'rack/test'
-gem 'rcov', :only => [:test]
+gem 'rake', :only => :testing
+gem 'rspec', '~> 1.2.9', :only => :testing, :require_as => 'spec'
+gem 'yard', :only => :testing
+gem 'rack-test', '~> 0.5.0', :only => :testing, :require_as => 'rack/test'
+gem 'rcov', :only => :testing
-gem 'do_sqlite3', '~> 0.10.0', :only => [:test]
-gem 'dm-core', '~> 0.10.1', :only => [:test]
-gem 'bcrypt-ruby', :only => [:test], :require_as => 'bcrypt'
+gem 'do_sqlite3', '~> 0.10.0', :only => :testing
+gem 'dm-core', '~> 0.10.1', :only => :testing
+gem 'bcrypt-ruby', :only => :testing, :require_as => 'bcrypt'
disable_system_gems
View
2  LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2009 jsmestad
+Copyright (c) 2009 Justin Smestad
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
19 README.rdoc
@@ -1,6 +1,6 @@
-= sinatra_warden
+= Sinatra::Warden
-Provides a module for basic helpers and actions needed for user authentication using Warden with Sinatra.
+A Sinatra (http://github.com/sinatra/sinatra) module that provides authentication for your Sinatra application through Warden (http://github.com/hassox/warden).
== Usage
@@ -20,20 +20,31 @@ Provides a module for basic helpers and actions needed for user authentication u
haml :dashboard
end
end
+
+== More Information
+
+Please read the wiki (http://wiki.github.com/jsmestad/sinatra_warden) for more information on more advanced configurations.
== Note on Patches/Pull Requests
+We use bundler on this project and disable system gems. Contributers should do the following:
+
+ $ git clone git://github.com/jsmestad/sinatra_warden.git
+ $ cd sinatra_warden
+ $ gem bundle
+ $ rake
+
* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Commit, do not mess with rakefile, 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)
* Send me a pull request. Bonus points for topic branches.
-== Contributers
+== Contributors
* Justin Smestad (http://github.com/jsmestad)
* Daniel Neighman (http://github.com/hassox)
+* Shane Hanna (http://github.com/shanna)
== Copyright
View
2  lib/sinatra_warden.rb
@@ -1,4 +1,4 @@
-require 'warden'
+Bundler.require_env
require File.join(File.dirname(__FILE__), 'sinatra_warden', 'sinatra')
Warden::Manager.before_failure do |env, opts|
View
26 lib/sinatra_warden/sinatra.rb
@@ -1,9 +1,7 @@
-require 'sinatra/base'
-require 'warden'
-
module Sinatra
module Warden
module Helpers
+
# The main accessor for the warden proxy instance
def warden
request.env['warden']
@@ -22,41 +20,49 @@ def user(*args)
alias_method :current_user, :user
# Set the currently logged in user
+ # @params [User] the user you want to log in
def user=(user)
warden.set_user user
end
alias_method :current_user=, :user=
# Require authorization for an action
- def authorize!(failure_path = nil)
+ # @params [String] path to redirect to if user is unauthenticated
+ def authorize!(failure_path=nil)
redirect(failure_path ? failure_path : options.auth_failure_path) unless authenticated?
end
end
def self.registered(app)
app.helpers Warden::Helpers
+
app.set :auth_failure_path, '/'
- app.set :auth_success_path, lambda{ back}
+ app.set :auth_success_path, lambda{ back }
+
+ app.set :auth_error_message, "Could not log you in."
+ app.set :auth_success_message, "You have logged in successfully."
+ app.set :auth_use_erb, false
+ app.set :auth_login_template, :login
app.post '/unauthenticated/?' do
status 401
- flash[:error] = "Could not log you in" if defined?(Rack::Flash)
- haml :login
+ flash[:error] = "Could not log you in." if defined?(Rack::Flash)
+ options.auth_use_erb ? erb(options.auth_login_template) : haml(options.auth_login_template)
end
app.get '/login/?' do
- haml :login
+ options.auth_use_erb ? erb(options.auth_login_template) : haml(options.auth_login_template)
end
app.post '/login/?' do
env['warden'].authenticate!
- flash[:success] = "You have logged in successfully." if defined?(Rack::Flash)
+ flash[:success] = options.auth_success_message if defined?(Rack::Flash)
redirect options.auth_success_path
end
app.get '/logout/?' do
env['warden'].logout
- flash[:success] = "You are now logged out." if defined?(Rack::Flash)
+ flash[:success] = options.auth_error_message if defined?(Rack::Flash)
redirect options.auth_success_path
end
end
View
2  spec/fixtures/testing_login.rb
@@ -1,5 +1,5 @@
class TestingLogin < Sinatra::Base
- register SinatraWarden
+ register Sinatra::Warden
get '/dashboard' do
authorize!('/login')
View
4 spec/spec_helper.rb
@@ -4,7 +4,7 @@
ENV['RACK_ENV'] ||= 'test'
project_root = File.expand_path(File.dirname(__FILE__))
require File.join(project_root, '..', 'vendor', 'gems', 'environment')
-Bundler.require_env(:test)
+Bundler.require_env(:testing)
require 'sinatra_warden'
require 'spec'
@@ -13,7 +13,7 @@
DataMapper.setup(:default, 'sqlite3::memory:')
%w(fixtures support).each do |path|
- Dir[ File.join( project_root, path, '/**/*.rb') ].each do |m|
+ Dir[ File.join(project_root, path, '/**/*.rb') ].each do |m|
require m
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.