zilch-authorisation 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 433b19fae52cfe196363bea6f17d45e6cb1dbc9b
4
+ data.tar.gz: 33b502c5c925507d44a3269be82c82f8fdddd513
5
+ SHA512:
6
+ metadata.gz: 53c9fc71053e3f69fc905f525274af252855ad1d984b0c7abf092375e7e73d218c871d217fd0c6e844c962c72433122eac8a53f792bd53029d1d1014ba8e74c1
7
+ data.tar.gz: 095881123a96e2a3e6ba177fa396e2b2f2b81e2d512c2f85f70188b106aa81776f78f0e11cb1d262d97558f9410abf21528c8dd2bd4b80585e2b478d9055eec0
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+
16
+ vendor/gems
17
+ .agignore
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zilch.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Rob Yurkowski and Philip Arndt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ # Zilch-Authorisation
2
+
3
+ Zilch-Authorisation is a zero-dependency authorisation interface.
4
+
5
+ The idea is that Zilch stands in for any authentication or authorisation calls
6
+ that your application makes and fulfills them by passing them to a registered
7
+ adapter that implements its interface with common libraries.
8
+
9
+ Once you are ready to implement a different authentication and/or authorisation
10
+ mechanism, you can write an adapter that implements the interfaces that Zilch
11
+ provides. In this way, you can easily swap out implementation details and not
12
+ affect your code.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'zilch', '~> 1.0'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it ( https://github.com/robyurkowski/zilch/fork )
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,2 @@
1
+ require "zilch/authorisation"
2
+ require "zilch/authorisation/version"
@@ -0,0 +1,4 @@
1
+ module Zilch
2
+ module Authorisation
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+ require 'zilch/authorisation/nil_user'
2
+
3
+ module Zilch
4
+ module Authorisation
5
+ module Adapters
6
+ class Default
7
+
8
+ def allow?(operation, resource)
9
+ true
10
+ end
11
+
12
+ def authenticate!
13
+ true
14
+ end
15
+
16
+ def current_user
17
+ Zilch::Authorisation::NilUser.new
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,54 @@
1
+ require 'forwardable'
2
+ require 'zilch/authorisation/adapters/default'
3
+
4
+ module Zilch
5
+ module Authorisation
6
+ # Interface for interacting with authentication and authorisation.
7
+ #
8
+ # @since x.x.x
9
+ class AuthorisationManager
10
+ extend Forwardable
11
+
12
+
13
+ def_delegators :adapter, :authenticate, :authenticated?, :current_user, :allow?
14
+
15
+
16
+ # Exception-raising version of `#authenticate`. Raise if not
17
+ # authenticated.
18
+ #
19
+ # @since x.x.x
20
+ def authenticate!
21
+ raise Zilch::Authorisation::NotAuthorisedException unless adapter.authenticate!
22
+ adapter.current_user
23
+ end
24
+
25
+
26
+ # Reader for the adapter which implements this interface.
27
+ #
28
+ # @since x.x.x
29
+ def adapter
30
+ @adapter ||= default_adapter
31
+ end
32
+
33
+
34
+ # Setter for the adapter which implements the interface. The given
35
+ # adapter must respond to
36
+ #
37
+ # @since x.x.x
38
+ attr_writer :adapter
39
+
40
+
41
+ # Specifies the default adapter to use.
42
+ #
43
+ # @since x.x.x
44
+ def default_adapter
45
+ Zilch::Authorisation::Adapters::Default.new
46
+ end
47
+
48
+
49
+ end
50
+
51
+ class NotAuthorisedException < Exception
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,9 @@
1
+ module Zilch
2
+ module Authorisation
3
+ class NilUser
4
+ def to_s
5
+ self.class.to_s
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ require 'forwardable'
2
+
3
+ module Zilch
4
+ module Authorisation
5
+ # Interface for interacting with users.
6
+ #
7
+ # @since x.x.x
8
+ class UsersManager
9
+ extend Forwardable
10
+
11
+
12
+ def_delegators :adapter, :primary_user
13
+
14
+
15
+ # Reader for the adapter which implements this interface.
16
+ #
17
+ # @since x.x.x
18
+ def adapter
19
+ @adapter ||= default_adapter
20
+ end
21
+
22
+
23
+ # Setter for the adapter which implements the interface.
24
+ #
25
+ # @since x.x.x
26
+ attr_writer :adapter
27
+
28
+
29
+ # Specifies the default adapter to use.
30
+ #
31
+ # @since x.x.x
32
+ def default_adapter
33
+ Zilch::Authorisation::Adapters::Default.new
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Zilch
2
+ module Authorisation
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,71 @@
1
+ require "spec_helper"
2
+ require "zilch/authorisation/authorisation_manager"
3
+
4
+ RSpec.describe Zilch::Authorisation::AuthorisationManager do
5
+ describe "authentication" do
6
+ let(:adapter) { double("NewAdapter") }
7
+
8
+ before do
9
+ subject.adapter = adapter
10
+ end
11
+
12
+ describe "#authenticate" do
13
+ it "asks the adapter" do
14
+ expect(adapter).to receive(:authenticate)
15
+ subject.authenticate
16
+ end
17
+ end
18
+
19
+
20
+ describe "#authenticated?" do
21
+ it "asks the adapter" do
22
+ expect(adapter).to receive(:authenticated?)
23
+ subject.authenticated?
24
+ end
25
+ end
26
+
27
+
28
+ describe "#current_user" do
29
+ it "asks the adapter" do
30
+ expect(adapter).to receive(:current_user)
31
+ subject.current_user
32
+ end
33
+ end
34
+
35
+
36
+ describe "#allow?" do
37
+ it "asks the adapter" do
38
+ expect(adapter).to receive(:allow?)
39
+ subject.allow?
40
+ end
41
+ end
42
+
43
+
44
+ describe "#authenticate!" do
45
+ it "asks the adapter" do
46
+ allow(adapter).to receive(:current_user)
47
+ expect(adapter).to receive(:authenticate!).and_return true
48
+ subject.authenticate!
49
+ end
50
+
51
+ context "when not authenticated" do
52
+ it "throws an exception" do
53
+ allow(adapter).to receive(:authenticate!).and_return false
54
+ expect { subject.authenticate! }.to raise_exception Zilch::Authorisation::NotAuthorisedException
55
+ end
56
+ end
57
+
58
+ context "when authenticated" do
59
+ it "returns the current user" do
60
+ allow(adapter).to receive(:authenticate!).and_return true
61
+ expect(adapter).to receive(:current_user)
62
+ subject.authenticate!
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "adapter" do
69
+ it_behaves_like "zilch adapter"
70
+ end
71
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'zilch/authorisation/nil_user'
3
+
4
+ RSpec.describe Zilch::Authorisation::NilUser do
5
+ it "has an approximate string representation" do
6
+ expect(subject.to_s).to eq("Zilch::Authorisation::NilUser")
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+ require "zilch/authorisation/users_manager"
3
+
4
+ RSpec.describe Zilch::Authorisation::UsersManager do
5
+ describe "users" do
6
+ let(:adapter) { double("NewAdapter") }
7
+
8
+ before do
9
+ subject.adapter = adapter
10
+ end
11
+
12
+ describe "primary user" do
13
+ it "asks the adapter" do
14
+ expect(adapter).to receive(:primary_user)
15
+ subject.primary_user
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "adapter" do
21
+ it_behaves_like "zilch adapter"
22
+ end
23
+ end
@@ -0,0 +1,87 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |f| require f }
18
+
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # These two settings work together to allow you to limit a spec run
44
+ # to individual examples or groups you care about by tagging them with
45
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
46
+ # get run.
47
+ config.filter_run :focus
48
+ config.run_all_when_everything_filtered = true
49
+
50
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
51
+ # For more details, see:
52
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
53
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
54
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
55
+ config.disable_monkey_patching!
56
+
57
+ # This setting enables warnings. It's recommended, but in some cases may
58
+ # be too noisy due to issues in dependencies.
59
+ config.warnings = true
60
+
61
+ # Many RSpec users commonly either run the entire suite or an individual
62
+ # file, and it's useful to allow more verbose output when running an
63
+ # individual spec file.
64
+ if config.files_to_run.one?
65
+ # Use the documentation formatter for detailed output,
66
+ # unless a formatter has already been configured
67
+ # (e.g. via a command-line flag).
68
+ config.default_formatter = 'doc'
69
+ end
70
+
71
+ # Print the 10 slowest examples and example groups at the
72
+ # end of the spec run, to help surface which specs are running
73
+ # particularly slow.
74
+ config.profile_examples = 10
75
+
76
+ # Run specs in random order to surface order dependencies. If you find an
77
+ # order dependency and want to debug it, you can fix the order by providing
78
+ # the seed, which is printed after each run.
79
+ # --seed 1234
80
+ config.order = :random
81
+
82
+ # Seed global randomization in this process using the `--seed` CLI option.
83
+ # Setting this allows you to use `--seed` to deterministically reproduce
84
+ # test failures related to randomization by passing the same `--seed` value
85
+ # as the one that triggered the failure.
86
+ Kernel.srand config.seed
87
+ end
@@ -0,0 +1,23 @@
1
+ shared_examples_for "zilch adapter" do
2
+ let(:adapter) { double("NewAdapter") }
3
+
4
+ describe "#adapter" do
5
+ context "when given an adapter" do
6
+ before do
7
+ subject.adapter = adapter
8
+ end
9
+
10
+ it "uses that adapter" do
11
+ expect(subject.adapter).to eq(adapter)
12
+ end
13
+ end
14
+
15
+ context "when not given an adapter" do
16
+ it "uses the default adapter" do
17
+ default_adapter = double("DefaultAdapter")
18
+ allow(subject).to receive(:default_adapter).and_return(default_adapter)
19
+ expect(subject.adapter).to eq(default_adapter)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zilch/authorisation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zilch-authorisation"
8
+ spec.version = Zilch::Authorisation::VERSION
9
+ spec.authors = ["Rob Yurkowski", "Philip Arndt"]
10
+ spec.email = ["rob@yurkowski.net", "p@arndt.io"]
11
+ spec.summary = %q{A zero-dependency authorisation stub}
12
+ spec.description = %q{A zero-dependency authorisation stub}
13
+ spec.homepage = "https://github.com/robyurkowski/zilch-authorisation"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zilch-authorisation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rob Yurkowski
8
+ - Philip Arndt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '10.0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '10.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.0'
42
+ description: A zero-dependency authorisation stub
43
+ email:
44
+ - rob@yurkowski.net
45
+ - p@arndt.io
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - lib/zilch-authorisation.rb
57
+ - lib/zilch/authorisation.rb
58
+ - lib/zilch/authorisation/adapters/default.rb
59
+ - lib/zilch/authorisation/authorisation_manager.rb
60
+ - lib/zilch/authorisation/nil_user.rb
61
+ - lib/zilch/authorisation/users_manager.rb
62
+ - lib/zilch/authorisation/version.rb
63
+ - spec/lib/zilch/authorisation/authorisation_manager_spec.rb
64
+ - spec/lib/zilch/authorisation/nil_user_spec.rb
65
+ - spec/lib/zilch/authorisation/users_manager_spec.rb
66
+ - spec/spec_helper.rb
67
+ - spec/support/shared_examples.rb
68
+ - zilch-authorisation.gemspec
69
+ homepage: https://github.com/robyurkowski/zilch-authorisation
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.4.6
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A zero-dependency authorisation stub
93
+ test_files:
94
+ - spec/lib/zilch/authorisation/authorisation_manager_spec.rb
95
+ - spec/lib/zilch/authorisation/nil_user_spec.rb
96
+ - spec/lib/zilch/authorisation/users_manager_spec.rb
97
+ - spec/spec_helper.rb
98
+ - spec/support/shared_examples.rb
99
+ has_rdoc: