spine-authorisation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae25a7bd20391dbdb59d8a28b5833f9659c9c0bb
4
+ data.tar.gz: dad7806d35f9ed583ec583e089157cb3bc6d8b52
5
+ SHA512:
6
+ metadata.gz: 8663d77144f2ee9d7a8cf466a08d1fb0457ee24801d461d3d239c9233865ed094292641c21d51e3101f4a852563cb6158bf558469c0867c752b1c3a697ba20bc
7
+ data.tar.gz: af6df7e32d6b800b334c0ff8f49209aa2fc50ba393c4f9ae9dd5eeda14f9110877ef29be8003b870e2d4c43f17bd41532b2d2d3573c02296ee005d435fb19fa6
data/.gitignore ADDED
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /bin/
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require config/default
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ Changelog
2
+ =========
3
+
4
+ 0.1.0
5
+ -----
6
+ - First public release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spine-authorisation.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,51 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ spine-authorisation (0.1.0)
5
+ spine-hub (~> 0.1)
6
+ spine-permissions (~> 0.1)
7
+ spine-restrictions (~> 0.1)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ codeclimate-test-reporter (0.4.7)
13
+ simplecov (>= 0.7.1, < 1.0.0)
14
+ diff-lcs (1.2.5)
15
+ docile (1.1.5)
16
+ json (1.8.3)
17
+ rake (10.4.2)
18
+ rspec (3.2.0)
19
+ rspec-core (~> 3.2.0)
20
+ rspec-expectations (~> 3.2.0)
21
+ rspec-mocks (~> 3.2.0)
22
+ rspec-core (3.2.3)
23
+ rspec-support (~> 3.2.0)
24
+ rspec-expectations (3.2.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.2.0)
27
+ rspec-mocks (3.2.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.2.0)
30
+ rspec-support (3.2.2)
31
+ simplecov (0.10.0)
32
+ docile (~> 1.1.0)
33
+ json (~> 1.8)
34
+ simplecov-html (~> 0.10.0)
35
+ simplecov-html (0.10.0)
36
+ spine-hub (0.1.0)
37
+ spine-permissions (0.1.0)
38
+ spine-restrictions (0.1.0)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler (~> 1.7)
45
+ codeclimate-test-reporter (~> 0.4)
46
+ rake (~> 10.0)
47
+ rspec (~> 3.2)
48
+ spine-authorisation!
49
+
50
+ BUNDLED WITH
51
+ 1.10.2
data/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ Copyright (c) 2015, TOGGL LLC
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
+
10
+ 3. Neither the name of the TOGGL LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # Spine::Authorisation
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/spine-authorisation.svg)](http://badge.fury.io/rb/spine-authorisation)
4
+ [![Dependency Status](https://gemnasium.com/rspine/authorisation.svg)](https://gemnasium.com/rspine/authorisation)
5
+ [![Test Coverage](https://codeclimate.com/github/rspine/authorisation/badges/coverage.svg)](https://codeclimate.com/github/rspine/authorisation/coverage)
6
+ [![Code Climate](https://codeclimate.com/github/rspine/authorisation/badges/gpa.svg)](https://codeclimate.com/github/rspine/authorisation)
7
+ [![Codeship Status for rspine/authorisation](https://codeship.com/projects/f6de77f0-edaf-0132-b6b1-1efd3f886df2/status?branch=master)](https://codeship.com/projects/84059)
8
+
9
+ Authorisation context for Ruby applications.
10
+
11
+ ## Installation
12
+
13
+ To install it, add the gem to your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'spine-authorisation'
17
+ ```
18
+
19
+ Then run `bundle`. If you're not using Bundler, just `gem install spine-authorisation`.
20
+
21
+ ## Usage
22
+
23
+ Authorisation uses [Spine::Permissions](https://github.com/rspine/permissions)
24
+ and [Spine::Restrictions](https://github.com/rspine/restrictions) to define
25
+ rules.
26
+
27
+ ```ruby
28
+ Spine::Authorisation.permissions do
29
+ define(:user).grant(:read, :all)
30
+ end
31
+
32
+ Spine::Authorisation.restrictions do
33
+ register(MyRestriction).restrict(:write, :all)
34
+ end
35
+ ```
36
+
37
+ You can call `permissions` and `restrictions` directly or define yourself a
38
+ context. It requires you to override `role` and `subject` methods.
39
+
40
+ ```ruby
41
+ class UserContext
42
+ include Spine::Authorisation::Context
43
+
44
+ # Required to override
45
+ def role
46
+ user.role
47
+ end
48
+
49
+ # Required to override
50
+ def subject
51
+ user
52
+ end
53
+
54
+ def user
55
+ # find by identity
56
+ end
57
+ end
58
+
59
+ context = UserContext.new
60
+ context.authorize(:read, :tasks)
61
+ # => true
62
+ ```
63
+
64
+ Context authorize method also publishes events `:granted` and `:denied` with
65
+ `context, action, resource` arguments and `:restricted` with
66
+ `context, restriction, action, resource` arguments (see more
67
+ [Spine::Hub](https://github.com/rspine/hub) to see how to subscribe these).
68
+
69
+ ### Using with Spine::Engines
70
+
71
+ ```ruby
72
+ # application.rb
73
+
74
+ module MyApp
75
+ module Application
76
+ extension Spine::Authorisation::Engine
77
+ end
78
+ end
79
+ ```
80
+
81
+ Then you need to define your permissions and restrictions in
82
+ `config/authorisation.rb`.
83
+
84
+ ```ruby
85
+ module MyApp
86
+ module Application
87
+ permissions.define(:user).grant(:read, :all)
88
+ permissions.define(:admin).grant(:all, :all)
89
+
90
+ restrictions.register(MyRestriction).restrict(:write, :all)
91
+ end
92
+ end
93
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ module Spine
2
+ module Authorisation
3
+ autoload :Context, 'spine/authorisation/context'
4
+ autoload :Registrations, 'spine/authorisation/registrations'
5
+ autoload :Engine, 'spine/authorisation/engine'
6
+
7
+ extend Registrations
8
+ end
9
+ end
@@ -0,0 +1,43 @@
1
+ require 'spine/hub'
2
+
3
+ module Spine
4
+ module Authorisation
5
+ module Context
6
+ include Hub::Publisher
7
+
8
+ def subject
9
+ raise NotImplementedError, 'Context requires subject'
10
+ end
11
+
12
+ def role
13
+ raise NotImplementedError, 'Context requires role'
14
+ end
15
+
16
+ def authorize(action, resource)
17
+ return false unless subject
18
+
19
+ restriction = restricted?(action, resource)
20
+ if restriction
21
+ publish(:restricted, self, restriction, action, resource)
22
+ false
23
+ elsif !granted?(action, resource)
24
+ publish(:denied, self, action, resource)
25
+ false
26
+ else
27
+ publish(:granted, self, action, resource)
28
+ true
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def granted?(action, resource)
35
+ Authorisation.permissions.granted?(role, action, resource)
36
+ end
37
+
38
+ def restricted?(action, resource)
39
+ Authorisation.restrictions.restricted?(self, action, resource)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ module Spine
2
+ module Authorisation
3
+ module Engine
4
+ module Configuration
5
+ def authorisation
6
+ ::Spine::Authorisation
7
+ end
8
+ end
9
+
10
+ module Loader
11
+ extend self
12
+
13
+ def call(app)
14
+ require app.root.join('config', 'authorisation')
15
+ end
16
+ end
17
+
18
+ extend self
19
+
20
+ def configuration
21
+ Configuration
22
+ end
23
+
24
+ def loader
25
+ Loader
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,20 @@
1
+ require 'spine/permissions'
2
+ require 'spine/restrictions'
3
+
4
+ module Spine
5
+ module Authorisation
6
+ module Registrations
7
+ def permissions(&block)
8
+ @permissions ||= ::Spine::Permissions::Roles.new
9
+ @permissions.instance_eval(&block) if block_given?
10
+ @permissions
11
+ end
12
+
13
+ def restrictions(&block)
14
+ @restrictions ||= ::Spine::Restrictions::Collection.new
15
+ @restrictions.instance_eval(&block) if block_given?
16
+ @restrictions
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Spine
2
+ module Authorisation
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ require './spec/config/default'
2
+
3
+ require 'codeclimate-test-reporter'
4
+ CodeClimate::TestReporter.start
@@ -0,0 +1,20 @@
1
+ RSpec.configure do |config|
2
+ config.filter_run :focus
3
+ config.run_all_when_everything_filtered = true
4
+
5
+ if config.files_to_run.one?
6
+ config.default_formatter = 'doc'
7
+ end
8
+
9
+ config.order = :random
10
+
11
+ Kernel.srand config.seed
12
+
13
+ config.expect_with :rspec do |expectations|
14
+ expectations.syntax = :expect
15
+ end
16
+
17
+ config.mock_with :rspec do |mocks|
18
+ mocks.syntax = :expect
19
+ end
20
+ end
@@ -0,0 +1,48 @@
1
+ require 'spine/authorisation'
2
+
3
+ module Spine
4
+ module Authorisation
5
+ module Restriction
6
+ extend self
7
+
8
+ def restricted?(context)
9
+ true
10
+ end
11
+ end
12
+
13
+ permissions do
14
+ define(:user).grant(:read, :all).grant(:write, :data)
15
+ end
16
+
17
+ restrictions do
18
+ register(Restriction).restrict(:write, :all)
19
+ end
20
+
21
+ describe Context do
22
+ subject { double(subject: user, role: role).extend(Context) }
23
+ let(:user) { double }
24
+ let(:role) { :user }
25
+ let(:listener) { double }
26
+
27
+ before(:each) do
28
+ subject.subscribe(listener)
29
+ end
30
+
31
+ it 'grants authorization' do
32
+ expect(listener).to receive(:notify).with(:granted, subject, :read, :data)
33
+ expect(subject.authorize(:read, :data)).to be true
34
+ end
35
+
36
+ it 'denies authorisation' do
37
+ expect(listener).to receive(:notify).with(:denied, subject, :delete, :data)
38
+ expect(subject.authorize(:delete, :data)).to be false
39
+ end
40
+
41
+ it 'restricts authorization' do
42
+ expect(listener).to receive(:notify)
43
+ .with(:restricted, subject, Restriction, :write, :data)
44
+ expect(subject.authorize(:write, :data)).to be false
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'spine/authorisation/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "spine-authorisation"
7
+ spec.version = Spine::Authorisation::VERSION
8
+ spec.authors = ["TOGGL LLC"]
9
+ spec.email = ["support@toggl.com"]
10
+ spec.summary = 'Authorisation context for Ruby applications'
11
+ spec.description = ''
12
+ spec.homepage = 'https://github.com/rspine/authorisation'
13
+ spec.license = 'BSD-3-Clause'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'spine-hub', '~> 0.1'
21
+ spec.add_dependency 'spine-permissions', '~> 0.1'
22
+ spec.add_dependency 'spine-restrictions', '~> 0.1'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.2'
27
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
28
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spine-authorisation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TOGGL LLC
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: spine-hub
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: spine-permissions
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: spine-restrictions
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.4'
111
+ description: ''
112
+ email:
113
+ - support@toggl.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - CHANGELOG.md
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - lib/spine/authorisation.rb
127
+ - lib/spine/authorisation/context.rb
128
+ - lib/spine/authorisation/engine.rb
129
+ - lib/spine/authorisation/registrations.rb
130
+ - lib/spine/authorisation/version.rb
131
+ - spec/config/coverage.rb
132
+ - spec/config/default.rb
133
+ - spec/spine/authorisation/context_spec.rb
134
+ - spine-authorisation.gemspec
135
+ homepage: https://github.com/rspine/authorisation
136
+ licenses:
137
+ - BSD-3-Clause
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.4.5
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Authorisation context for Ruby applications
159
+ test_files:
160
+ - spec/config/coverage.rb
161
+ - spec/config/default.rb
162
+ - spec/spine/authorisation/context_spec.rb