simple-authorisation 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .bundle
2
+ .idea
3
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple-authorisation (0.0.2)
5
+ sinatra (~> 1.2.6)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.1.2)
11
+ rack (1.3.2)
12
+ rack-test (0.6.0)
13
+ rack (>= 1.0)
14
+ rake (0.9.2)
15
+ rspec (2.6.0)
16
+ rspec-core (~> 2.6.0)
17
+ rspec-expectations (~> 2.6.0)
18
+ rspec-mocks (~> 2.6.0)
19
+ rspec-core (2.6.4)
20
+ rspec-expectations (2.6.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.6.0)
23
+ sinatra (1.2.6)
24
+ rack (~> 1.1)
25
+ tilt (< 2.0, >= 1.2.2)
26
+ tilt (1.3.2)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ rack-test (>= 0.6.0)
33
+ rake (>= 0.9.2)
34
+ rspec (>= 2.6.0)
35
+ simple-authorisation!
36
+ sinatra (>= 1.2.6)
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ require 'rspec/core/rake_task'
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ $:.unshift(File.dirname(__FILE__) + '/lib')
8
+
9
+
10
+ desc "Run RSpec"
11
+ RSpec::Core::RakeTask.new do |t|
12
+ #t.rcov = ENV['RCOV']
13
+ #t.rcov_opts = %w{--exclude osx\/objc,gems\/,spec\/}
14
+ t.verbose = true
15
+ end
16
+
17
+
18
+ task :default => [:spec]
19
+
20
+ require 'rake/clean'
21
+ CLEAN.include %w(**/*.{log,pyc,rbc,tgz} doc)
@@ -0,0 +1,24 @@
1
+ module Simple
2
+ module Authorisation
3
+ def self.route(name, options)
4
+ @@routes ||= {}
5
+ @@routes[name] = options
6
+ end
7
+
8
+ def self.is_allowed?(route_name, options)
9
+ matching_route = (@@routes.keys.sort.reverse.select{|route | route_name.start_with?(route) }).first
10
+ route_rules = @@routes[matching_route]
11
+ raise "no rules found for #{route_name}" if route_rules.nil?
12
+ allow = route_rules.fetch(:allow, [])
13
+ deny = route_rules.fetch(:deny, [])
14
+ user = options.fetch(:user, nil)
15
+ anonymous_user_class = options.fetch(:anonymous_user_class, NilClass)
16
+
17
+ return true if allow.index('?')
18
+ return false if deny.index('?') and user.is_a? anonymous_user_class
19
+ return true if allow.index('*') and not user.is_a? anonymous_user_class
20
+
21
+ false
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module SinatraAuthorisation
5
+ def self.registered(app)
6
+ app.set :authorisation_login, '/login'
7
+ app.set :authorisation_current_user, :current_user
8
+ app.set :authorisation_anonymous_user_class, NilClass
9
+
10
+ app.before do
11
+ route_name = request.path
12
+ unless Simple::Authorisation.is_allowed?(route_name, :user => current_user, :anonymous_user_class => options.authorisation_anonymous_user_class)
13
+ session[:return_to] = request.fullpath unless request.fullpath.include?('favicon.ico')
14
+ redirect options.authorisation_login
15
+ return false
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ register SinatraAuthorisation
23
+ end
@@ -0,0 +1,2 @@
1
+ require 'simple-authorisation/authorisation'
2
+ require 'simple-authorisation/sinatra'
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'simple-authorisation'
6
+ s.version = '0.0.2'
7
+ s.authors = ["Derek Ekins"]
8
+ s.description = 'Handles authorisation only'
9
+ s.summary = "simple-authorisation-#{s.version}"
10
+ s.email = 'derek@spathi.com'
11
+ s.homepage = "http://github.com/dereke/simple-authorisation"
12
+
13
+ s.platform = Gem::Platform::RUBY
14
+ s.post_install_message = %{
15
+ (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
16
+
17
+ Thank you for installing simple-authorisation
18
+
19
+ (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
20
+
21
+ }
22
+
23
+ s.add_dependency 'sinatra', '~> 1.2.6'
24
+
25
+ s.add_development_dependency 'rake', '>= 0.9.2'
26
+ s.add_development_dependency 'rspec', '>= 2.6.0'
27
+ s.add_development_dependency 'sinatra', '>= 1.2.6'
28
+ s.add_development_dependency 'rack-test', '>= 0.6.0'
29
+
30
+ s.rubygems_version = ">= 1.6.1"
31
+ s.files = `git ls-files`.split("\n")
32
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
33
+ s.require_path = "lib"
34
+ end
@@ -0,0 +1,37 @@
1
+ require 'simple-authorisation/authorisation'
2
+
3
+ module Simple
4
+ describe Authorisation do
5
+ it "should allow requests to anonymous users" do
6
+ Simple::Authorisation.route '/test', :allow => ['?']
7
+ Simple::Authorisation.is_allowed?('/test', :user => nil).should be_true
8
+ end
9
+
10
+ it "should deny request to anonymous users" do
11
+ Simple::Authorisation.route '/test', :deny => ['?']
12
+ Simple::Authorisation.is_allowed?('/test', :user => nil).should be_false
13
+ end
14
+
15
+ it "should allow requests to any user" do
16
+ Simple::Authorisation.route '/test', :allow => ['*']
17
+ Simple::Authorisation.is_allowed?('/test', :user => Object.new).should be_true
18
+ end
19
+
20
+ it "should allow requests to any user but deny requests to anonymous users" do
21
+ Simple::Authorisation.route '/test', :allow => ['*'], :deny => ['?']
22
+ Simple::Authorisation.is_allowed?('/test', :user => Object.new).should be_true
23
+ Simple::Authorisation.is_allowed?('/test', :user => nil).should be_false
24
+ end
25
+
26
+ it "should find rules for a hierarchy" do
27
+ Simple::Authorisation.route '/test', :allow => ['?']
28
+ Simple::Authorisation.is_allowed?('/test/page', :user => nil).should be_true
29
+ end
30
+
31
+ it "should find rules for a hierarchy and apply most appropriate rule" do
32
+ Simple::Authorisation.route '/test/page', :allow => ['?']
33
+ Simple::Authorisation.route '/test', :deny=> ['?']
34
+ Simple::Authorisation.is_allowed?('/test/page/low', :user => nil).should be_true
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require "rack/test"
3
+ require 'simple-authorisation/sinatra'
4
+
5
+
6
+ module Sinatra
7
+ describe "Authorisation" do
8
+ before do
9
+ @session = Rack::Test::Session.new(TestApp)
10
+ end
11
+
12
+ it "asks Simple::Authorisation if access is allowed" do
13
+ Simple::Authorisation.route '/', :allow => ['?']
14
+ @session.get '/'
15
+
16
+ # this fails for some reason but I know that it does work - what is wrong???
17
+ Simple::Authorisation.should_receive(:is_allowed?).with("/", {:user=>nil})
18
+ end
19
+
20
+ end
21
+
22
+ class TestApp < Sinatra::Application
23
+ set :environment, :test
24
+
25
+
26
+ get "/" do
27
+ "Nothing to see here"
28
+ end
29
+
30
+ def current_user
31
+ nil
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ $:.unshift(File.dirname(__FILE__))
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ Bundler.setup
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-authorisation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Derek Ekins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-27 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: &10991520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *10991520
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &10991020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.2
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *10991020
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &10990560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 2.6.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *10990560
47
+ - !ruby/object:Gem::Dependency
48
+ name: sinatra
49
+ requirement: &10990100 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.6
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *10990100
58
+ - !ruby/object:Gem::Dependency
59
+ name: rack-test
60
+ requirement: &10989600 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: 0.6.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *10989600
69
+ description: Handles authorisation only
70
+ email: derek@spathi.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - .gitignore
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - Rakefile
79
+ - lib/simple-authorisation.rb
80
+ - lib/simple-authorisation/authorisation.rb
81
+ - lib/simple-authorisation/sinatra.rb
82
+ - simple-authorisation.gemspec
83
+ - spec/simple-authorisation/authorisation_spec.rb
84
+ - spec/simple-authorisation/sinatra_integration_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: http://github.com/dereke/simple-authorisation
87
+ licenses: []
88
+ post_install_message: ! '
89
+
90
+ (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
91
+
92
+
93
+ Thank you for installing simple-authorisation
94
+
95
+
96
+ (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
97
+
98
+
99
+ '
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.6
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: simple-authorisation-0.0.2
121
+ test_files:
122
+ - spec/simple-authorisation/authorisation_spec.rb
123
+ - spec/simple-authorisation/sinatra_integration_spec.rb
124
+ - spec/spec_helper.rb