Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: vast/sinatra-simple-auth
base: master
...
head fork: uilgenstein/sinatra-simple-auth
compare: master
Checking mergeability… Don’t worry, you can still create the pull request.
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
85 README.md
@@ -1,34 +1,99 @@
##Sinatra SimpleAuth Extension
-Extends Sinatra with a extension methods and routes for dealing with a simple, single-password authorization
+Extends Sinatra with extension methods and routes for dealing with a simple authorization method.
+Both Sinatra application styles are supported: "Classic" and "Classy" (modular) style.
##Installation
- sudo gem install sinatra-simple-auth -s http://gemcutter.org/
+Since this is a fork of the original gem sources it is not released as a gem. You can build the gem yourself:
-##Usage
+ gem build sinatra-simple-auth.gemspec
+ gem install sinatra-simple-auth-0.1.1.gem
+
+or you can install it from a local path or even from github through Bundler by adding one of these two lines to your Gemfile:
+
+ gem 'sinatra-simple-auth', :path => /path/to/sources
+ or
+ gem 'sinatra-simple-auth'. :git => 'git://github.com/uilgenstein/sinatra-simple-auth.git'
+
+##Usage for "Classic" style applications
require 'rubygems'
require 'sinatra'
require 'sinatra/simple_auth'
enable :sessions
- set :password, 'my_cool_password' #define single password
- set :home, '/secure/' #where user should be redirected after successful authorization
+ set :home, '/secure/' #where user should be redirected after successful authorization
+
+ def authorize(login, password)
+ # return value will be saved in session[:user_id]
+ login if login == 'bob' && password == 'secret
+ end
- get '/a/?' do
- erb :auth #page with logon form
+ get '/login/?' do
+ erb :login #page with logon form
end
- get '/secure/' do
- protected! #protected route, requires auth
+ get '/secure/?' do
+ login_required #protected route, requires auth
erb :secure
end
get '/' do
if authorized? #helper method
- "Hello, %username%"
+ "Hello, your user id is: #{session[:user_id]}"
else
"Not authorized"
end
end
+
+##Usage for "Classy" (modular) style applications
+
+In your config.ru you can mount your proteceted app under a url prefix (e.g. /admin):
+
+ require 'rubygems'
+ require 'sinatra/base'
+ require 'admin_application
+ require 'public_application
+
+ map '/admin' do
+ run AdminApplication
+ end
+
+ map '/' do
+ run PublicApplication
+ end
+
+
+Your protected application could then look something like this:
+
+ require 'sinatra/simple_auth'
+
+ class AdminApplication < Sinatra::Base
+ register Sinatra::SimpleAuth
+
+ enable :sessions
+ set :home, '/' # relative to url prefix from config.ru.
+ # this is where user should be redirected after successful authorization
+
+ def authorize(login, password)
+ # return value will be saved in session[:user_id]
+ login if login == 'bob' && password == 'secret
+ end
+
+ before do
+ login_required unless request.path_info =~ /^\/login\/?$/
+ end
+
+ get '/login/?' do
+ erb :login #page with logon form
+ end
+
+ get '/' do
+ if authorized? #helper method
+ "Hello, your user id is: #{session[:user_id]}"
+ else
+ "Not authorized"
+ end
+ end
+ end
View
59 lib/sinatra/simple_auth.rb
@@ -3,42 +3,40 @@
module Sinatra
module SimpleAuth
module Helpers
- def authorized?
- session[:arni]
- end
- def auth!(password)
- if password == options.password
- session[:arni] = true
- redirect_back_or_default(options.home)
- end
- redirect '/a'
+ def authorize(login, password)
+ nil
end
- def logout!
- session.clear
- redirect '/'
+ def authorized?
+ session[:user_id]
end
-
- def protected!
+ alias :logged_in? :authorized?
+
+ def login_required
unless authorized?
store_location
- redirect '/a'
+ redirect full_app_path('/login')
end
end
def store_location
- session[:return_to] = request.fullpath if request.get?
+ session[:return_to] = request.path_info if request.get?
end
+
+ private
- protected
def redirect_back_or_default(default)
- if session[:return_to] && session[:return_to] !=~ /^\/a\/?$/
+ if session[:return_to] && session[:return_to] !=~ Regexp.new("^/login/?$")
back = session[:return_to].clone
session[:return_to] = nil
- redirect back
+ redirect full_app_path(back)
end
- redirect default
+ redirect full_app_path(default)
+ end
+
+ def full_app_path(path)
+ request.script_name + path
end
end
@@ -46,25 +44,24 @@ def redirect_back_or_default(default)
def self.registered(app)
app.helpers SimpleAuth::Helpers
- app.set :password, 'password'
app.set :home, '/'
- ['/a/?', '/login/?', '/signin/?'].each do |r|
- app.post r do
- auth!(params[:password])
+ app.post '/login/?' do
+ if user_id = authorize(params[:login], params[:password])
+ session[:user_id] = user_id
+ redirect_back_or_default(settings.home)
end
- end
-
- app.delete '/a/?' do
- logout!
+ redirect full_app_path('/login')
end
app.get '/logout/?' do
- logout!
+ session.clear
+ redirect full_app_path('/')
end
- end
+ end
end
register SimpleAuth
-end
+end
+
View
50 test/sinatra_app.rb
@@ -1,24 +1,38 @@
require 'rubygems'
-require 'sinatra'
-require 'lib/sinatra/simple_auth'
+require 'sinatra/base'
+require File.expand_path('../../lib/sinatra/simple_auth', __FILE__)
+class MockApplication < Sinatra::Base
+ register Sinatra::SimpleAuth
+
+ set :environment, :test
+ enable :sessions
+
+ set :home, '/secret'
+ set :login, 'admin'
+ set :password, 'abcxyz'
-set :password, 'hello'
-set :home, '/secret/'
+ def authorize(login, password)
+ login == settings.login && password == settings.password
+ end
+
+ get '/' do
+ "Root"
+ end
+
+ get '/foo' do
+ end
-get '/' do
- "hello, i'm root"
-end
+ get '/public' do
+ if authorized?
+ "hello %username%"
+ else
+ "Please login"
+ end
+ end
-get '/public' do
- if authorized?
- "hello, %username%"
- else
- "Please login"
- end
+ get '/protected' do
+ login_required
+ "private area"
+ end
end
-
-get '/pvt' do
- protected!
- "private area"
-end
View
53 test/sinatra_simple_auth_test.rb
@@ -3,54 +3,59 @@
require 'rack/test'
begin; require 'turn'; rescue LoadError; end
-set :environment, :test
-enable :sessions
-
class SinatraSimpleAuthTest < Test::Unit::TestCase
include Rack::Test::Methods
-
+
def app
- Sinatra::Application
+ MockApplication
end
-
+
def test_it_should_login_and_redirect
- post '/a', {:password => app.password}
+ login!
assert_redirect app.home
end
-
+
def test_it_should_fail_login_and_redirect
- post '/a', {:password => 'some fake data'}
- assert_redirect '/a'
+ post '/login', { :login => 'foo', :password => 'some fake data' }, env
+ assert_redirect '/login'
end
-
+
def test_it_should_login_and_redirect_back
- get '/pvt'
- assert_redirect '/a'
+ get '/protected', {}, env
+ assert_redirect '/login'
login!
- assert_redirect '/pvt'
+ assert_redirect '/protected'
end
-
+
def test_it_should_logout
login!
- delete '/a'
+ get '/logout', {}, env
assert_redirect '/'
+ get '/protected', {}, env
+ assert_redirect '/login'
end
-
+
def test_authorized_helper_should_work
- get '/public'
+ get '/public', {}, env
assert last_response.body.include?("Please login")
login!
- get '/public'
+ get '/public', {}, env
assert last_response.body.include?("%username%")
end
-
- protected
+
+ private
+
def login!
- post '/a', {:password => app.password}
+ post '/login', { :login => app.login, :password => app.password }, env
end
-
+
def assert_redirect(path)
assert last_response.redirect?
- assert_equal last_response.headers['Location'], path
+ assert_equal last_response.headers['Location'], env['SCRIPT_NAME'] + path
+ end
+
+ def env
+ { 'SCRIPT_NAME' => '/admin' }
end
+
end

No commit comments for this range

Something went wrong with that request. Please try again.