lamassu 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
+ SHA256:
3
+ metadata.gz: 2682d297a316a2f736fe00895cffe5c0a55f0deb285a3784c194297d00db233a
4
+ data.tar.gz: d26e36045baef5c8396bc60367e66fc7ec552fa01a3b40c549a6f1022d7b537e
5
+ SHA512:
6
+ metadata.gz: eec290deb6a77c651c427b5a71e40aaa88fb62f86ea4461fafd79f5117c154a3a0fc2ba75921a75c7d2c0eb95186c5af9d1b5147a5e1c9b9d5abb82dba8bef9e
7
+ data.tar.gz: 80ae9cb31cfc62c97d46bc80c08cec911db3807582c7128b5fa6f40f15937cd1ffc0ba51e1eef7dd8f22b690ac87a9ca83e13864d95e2e355add50d1a39442b1
data/.editorconfig ADDED
@@ -0,0 +1,17 @@
1
+ # EditorConfig is awesome: http://EditorConfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ # Unix-style newlines with a newline ending every file
7
+ [*]
8
+ charset = utf-8
9
+ end_of_line = lf
10
+ indent_size = 2
11
+ indent_style = space
12
+ insert_final_newline = true
13
+ trim_trailing_whitespace = true
14
+
15
+ # Markdown uses trailing whitespace for linebreaks.
16
+ [*.md]
17
+ trim_trailing_whitespace = false
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ ### JetBrains
14
+ .idea
15
+
16
+ ### Ruby
17
+ *.gem
18
+ *.rbc
19
+ /.config
20
+ /InstalledFiles
21
+ /spec/examples.txt
22
+ /test/tmp/
23
+ /test/version_tmp/
24
+
25
+ # Used by dotenv library to load environment variables.
26
+ .env
27
+
28
+ ## Specific to RubyMotion (use of CocoaPods):
29
+ #
30
+ # We recommend against adding the Pods directory to your .gitignore. However
31
+ # you should judge for yourself, the pros and cons are mentioned at:
32
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
33
+ #
34
+ # vendor/Pods/
35
+
36
+ ## Documentation cache and generated files:
37
+ /.yardoc/
38
+ /rdoc/
39
+
40
+ ## Environment normalization:
41
+ /vendor/bundle
42
+ /lib/bundler/man/
43
+
44
+ # for a library or gem, you might want to ignore these files since the code is
45
+ # intended to run in multiple environments; otherwise, check them in:
46
+ Gemfile.lock
47
+ .ruby-version
48
+ .ruby-gemset
49
+
50
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
51
+ .rvmrc
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,80 @@
1
+ # This file is a template, and might need editing before it works on your project.
2
+ # Official language image. Look for the different tagged releases at:
3
+ # https://hub.docker.com/r/library/ruby/tags/
4
+ image: "ruby:2.5"
5
+
6
+ # Cache gems in between builds
7
+ cache:
8
+ paths:
9
+ - vendor/ruby
10
+
11
+ variables:
12
+ JRUBY_OPTS: "--debug"
13
+
14
+ before_script:
15
+ - ruby -v # Print out ruby version for debugging
16
+ - apt update
17
+ - apt install git build-essential -y
18
+ - gem install bundler --no-ri --no-rdoc # Bundler is not installed with the image
19
+ - bundle install -j $(nproc) --path vendor # Install dependencies into ./vendor/ruby
20
+
21
+ # Optional - Delete if not using `rubocop`
22
+ rubocop:
23
+ script:
24
+ - bundle exec rubocop --version
25
+ - bundle exec rubocop --extra-details --display-style-guide --format clang
26
+ allow_failure: true
27
+
28
+ reek:
29
+ script:
30
+ - bundle exec reek --version
31
+ - bundle exec reek
32
+ allow_failure: true
33
+
34
+ rspec:2.3:
35
+ image: "ruby:2.3"
36
+ script:
37
+ - bundle exec rspec spec
38
+
39
+ rspec:2.4:
40
+ image: "ruby:2.4"
41
+ script:
42
+ - bundle exec rspec spec
43
+
44
+ rspec:
45
+ script:
46
+ - bundle exec rspec spec
47
+ artifacts:
48
+ paths:
49
+ - coverage/
50
+ expire_in: 30 days
51
+
52
+ rspec:2.6-rc:
53
+ image: "ruby:2.6-rc"
54
+ script:
55
+ - bundle exec rspec spec
56
+ allow_failure: true
57
+
58
+ rspec:jruby:9.1:
59
+ image: "jruby:9.1"
60
+ script:
61
+ - bundle exec rspec spec
62
+
63
+ rspec:jruby:9.2:
64
+ image: "jruby:9.2"
65
+ script:
66
+ - bundle exec rspec spec
67
+
68
+ pages:
69
+ stage: deploy
70
+ dependencies:
71
+ - rspec
72
+ script:
73
+ - mkdir -p public/
74
+ - mv coverage/ public/
75
+ artifacts:
76
+ paths:
77
+ - public
78
+ expire_in: 30 days
79
+ only:
80
+ - master
data/.mdlrc ADDED
@@ -0,0 +1 @@
1
+ rules "~MD024"
data/.reek ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ UtilityFunction:
3
+ public_methods_only: true
4
+ DataClump:
5
+ min_clump_size: 3
data/.reek.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ detectors:
3
+ UtilityFunction:
4
+ public_methods_only: true
5
+ DataClump:
6
+ min_clump_size: 3
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ DisplayCopNames: true
4
+ DisplayStyleGuide: true
5
+ Style/Alias:
6
+ # Prefer `alias_method :foo, :bar` over `alias foo bar`
7
+ EnforcedStyle: prefer_alias_method
8
+ Naming/ConstantName:
9
+ Enabled: false
10
+ Metrics/BlockLength:
11
+ Exclude:
12
+ - lamassu.gemspec
13
+ - spec/**/*_spec.rb
data/.travis.yml ADDED
@@ -0,0 +1,21 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3
5
+ - 2.4
6
+ - 2.5
7
+ before_script:
8
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
9
+ - chmod +x ./cc-test-reporter
10
+ - ./cc-test-reporter before-build
11
+ script:
12
+ - bundle exec rspec
13
+ after_script:
14
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
15
+ notifications:
16
+ webhooks:
17
+ urls:
18
+ - https://webhooks.gitter.im/e/e598d063293fab2e0914
19
+ on_success: change # options: [always|never|change] default: always
20
+ on_failure: always # options: [always|never|change] default: always
21
+ on_start: never # options: [always|never|change] default: always
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [v0.1.0] - 2018-09-14
11
+
12
+ ### Added
13
+
14
+ - Initial implementation of Lamassu
15
+
16
+ [Unreleased]: https://gitlab.com/huyderman/lamassu/compare/v0.1.0...HEAD
17
+ [v0.1.0]: https://gitlab.com/huyderman/lamassu/compare/v0.0.0...v0.1.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at johannes@huyderman.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in lamassu.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jo-Herman Haugholt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # ![Lamassu](http://res.cloudinary.com/huyderman/image/upload/c_lpad,dpr_2.0,f_auto,g_center,h_150,w_888/v1523625102/lamassu)
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/lamassu.svg)](https://badge.fury.io/rb/querylicious)
4
+ [![Build Status](https://gitlab.com/huyderman/lamassu/badges/master/build.svg)](https://gitlab.com/huyderman/lamassu/pipelines)
5
+ [![Coverage](https://gitlab.com/huyderman/lamassu/badges/master/coverage.svg?job=rspec)](http://huyderman.gitlab.io/lamassu/coverage)
6
+ [![Join the chat at https://gitter.im/lamassu-rb/Lobby](https://badges.gitter.im/lamassu-rb/Lobby.svg)](https://gitter.im/lamassu-rb/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7
+
8
+ > Lamassu is an functional authorization framework for Ruby, based on
9
+ > callable policy objects and policy containers
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'lamassu'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install lamassu
26
+
27
+ ## Usage
28
+
29
+ To use Lamassu, you need an Guardian for your project:
30
+
31
+ ```rb
32
+ MyGuardian = Lamassu::Guardian.new
33
+ ```
34
+
35
+ You can then register policies for various Modules with the guardian, and if authorized:
36
+
37
+ ```rb
38
+ include Dry::Monads::Result::Mixin
39
+
40
+ Article = Struct.new(:title, :owner, :published)
41
+
42
+ MyGuardian.policies.for Article do
43
+ policy :read, proc do |user, article|
44
+ if article.published || user == article.owner
45
+ Success(:allowed)
46
+ else
47
+ Failure(:no_access)
48
+ end
49
+ end
50
+ end
51
+
52
+ article = Article.new('My Article', :bob, true)
53
+ MyGuardian.authorize(:bob, article, :read)
54
+ # => Success(:allowed)
55
+ ```
56
+
57
+ A policy can be any callable object returning a `dry-monads` `Result` object, or an
58
+ object responding to `to_result`.
59
+
60
+ When building an application with many policies, it's recommended to create
61
+ proper policy objects by including `Lamassu::Policy`:
62
+
63
+ ```rb
64
+ # app/policies/article/read_policy.rb
65
+
66
+ class ReadPolicy
67
+ include Lamassu::Policy
68
+
69
+ def call(user, article)
70
+ if article.published || user == article.owner
71
+ Success(:allowed)
72
+ else
73
+ Failure(:disallowed)
74
+ end
75
+ end
76
+ end
77
+
78
+ MyGuardian.policies.for Article do
79
+ policy :read, ReadPolicy.new
80
+ end
81
+ ```
82
+
83
+ ### Subsets
84
+
85
+ Since the result is wrapped in a `Result`, the policy object can also be used
86
+ to return a subset or selection of data:
87
+
88
+ ```rb
89
+ class ReadPolicy
90
+ include Lamassu::Policy
91
+
92
+ def call(user, _)
93
+ articles = ArticleRepository.select do |article|
94
+ article.owner == user || article.published
95
+ end
96
+
97
+ if articles.empty?
98
+ Failure(nil)
99
+ else
100
+ Success(articles)
101
+ end
102
+ end
103
+ end
104
+
105
+ result = guardian.authorize(:bob, article, :read)
106
+ # => Success([#<Article>, ...])
107
+
108
+ result.value! if result.success?
109
+ # => [#<Article>, ...]
110
+ ```
111
+
112
+ Example usages for this include to return only the articles a user have
113
+ authorization to view, or transform an object to remove any fields the user
114
+ is authorized to read.
115
+
116
+ ### Policy adapters
117
+
118
+ Lamassu comes with several "policy adapters" for convenience when creating policies:
119
+
120
+ ```rb
121
+ MyGuardian.policies.for Article do
122
+ check :write, (proc { true })
123
+ map :published, (proc { ArticleRepository.select(&:published) })
124
+ end
125
+ ```
126
+
127
+ Both adapters wraps a callable object, and returns a valid policy object.
128
+ The `check` adapter is used with an callable object that returns a truish or
129
+ falsish value, and wraps the result in `Success` or `Failure` respectively.
130
+ The `map` adapter wraps a callable object, and wraps any output in a `Success`.
131
+
132
+ These adapters are meant for simple applications or to ease porting existing
133
+ ones. It's recommended to create proper policy objects when possible, as you
134
+ have better control over the return value.
135
+
136
+ ### Web frameworks
137
+
138
+ Lamassu don't currently include any integrations with any web frameworks.
139
+ It's meant to be flexible and usable with any framework, but the details will
140
+ depend on the chosen framework.
141
+
142
+ To simplify calling lamassu, it could be useful to create a helper to
143
+ include in your controllers:
144
+
145
+ ```rb
146
+ module AuthorizationHelper
147
+ # Convenience method for checking authorization where the user object is
148
+ # available through `current_user`, and the scope is the current controller.
149
+ def authorize(policy)
150
+ MyGuardian.authorize(current_user, self, policy)
151
+ end
152
+ end
153
+ ```
154
+
155
+ ## Development
156
+
157
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
158
+ run `rake spec` to run the tests. You can also run `bin/console` for an
159
+ interactive prompt that will allow you to experiment.
160
+
161
+ To install this gem onto your local machine, run `bundle exec rake install`.
162
+
163
+ ## Contributing
164
+
165
+ Bug reports and pull requests are welcome on GitHub at
166
+ https://gitlab.com/huyderman/lamassu/issues. This project is intended to be a safe,
167
+ welcoming space for collaboration, and contributors are expected to adhere
168
+ to the [code of conduct](CODE_OF_CONDUCT.md).
169
+
170
+ ## License
171
+
172
+ The gem is available as open source under the terms of the
173
+ [MIT License](https://opensource.org/licenses/MIT).
174
+
175
+ ## Code of Conduct
176
+
177
+ Everyone interacting in the Lamassu project’s codebases, issue trackers,
178
+ chat rooms and mailing lists is expected to follow the
179
+ [code of conduct](CODE_OF_CONDUCT.md).
180
+
181
+ ## Acknowledgments
182
+
183
+ * [dry-rb](https://dry-rb.org/) collection of gems which have both a great inspiration, and without this library would not be possible
184
+ * [Pundit](https://github.com/varvet/pundit) which is an great authorization library and have been an influence of some of the design of Lamassu
185
+ * Alex Daily ([@Alexis@beepboop.one](https://beepboop.one/@Alexis) or [@heyalexdaily@twitter.com](https://twitter.com/heyalexdaily)) who created the awesome logo for Lamassu.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
9
+
10
+ desc 'Run Mutation testing'
11
+ task :mutant do
12
+ sh 'mutant --use rspec Lamassu*'
13
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'pry'
6
+ require 'lamassu'
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ Guardian = Lamassu::Guardian.new
12
+
13
+ puts '`Guardian` initialized'
14
+
15
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lamassu.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ version_file = File.expand_path('VERSION', __dir__)
5
+ version = File.read(version_file).lines.first.chomp
6
+
7
+ spec.name = 'lamassu'
8
+ spec.version = version
9
+ spec.authors = ['Jo-Herman Haugholt']
10
+ spec.email = ['johannes@huyderman.com']
11
+
12
+ spec.summary = 'Autorization gem based on policy objects and dry-container'
13
+ spec.homepage = 'https://github.com/huyderman/lamassu'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'dry-configurable', '~> 0.7.0'
22
+ spec.add_runtime_dependency 'dry-container', '~> 0.6.0'
23
+ spec.add_runtime_dependency 'dry-inflector', '~> 0.1.1'
24
+ spec.add_runtime_dependency 'dry-monads', '~> 1.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'mutant', '~> 0.8.0'
28
+ spec.add_development_dependency 'mutant-rspec', '~> 0.8.0'
29
+ spec.add_development_dependency 'pry', '~> 0.11.0'
30
+ spec.add_development_dependency 'rake', '~> 12.0'
31
+ spec.add_development_dependency 'reek', '~> 4.7'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ spec.add_development_dependency 'rubocop', '~> 0.52'
34
+ spec.add_development_dependency 'simplecov', '~> 0.16'
35
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads/result'
4
+ require 'dry/monads/do'
5
+ require 'lamassu/policy_container'
6
+
7
+ module Lamassu
8
+ # Guardian object for authorizing a subject
9
+ class Guardian
10
+ include Dry::Monads::Result::Mixin
11
+ include Dry::Monads::List::Mixin
12
+ include Dry::Monads::Do.for(:authorize_many)
13
+
14
+ attr_reader :container
15
+
16
+ def initialize(container: PolicyContainer.new)
17
+ @container = container
18
+ @namespace_resolver = Lamassu.namespace_resolver
19
+ end
20
+
21
+ alias_method :policies, :container
22
+
23
+ # Check authorization for subject on target for one or more policies
24
+ #
25
+ # If more than one policy is specified, it will return the last Success if
26
+ # all policies are successful. Otherwise, it will return the first Failure
27
+ #
28
+ # :reek:LongParameterList
29
+ # @param [Object] subject Subject for authorization check
30
+ # @param [Object,Module] target Target for authorization check
31
+ # @param [Symbol,String] policies Policy or policies to check
32
+ # @return [Dry::Result]
33
+ def authorize(subject, target, *policies)
34
+ case policies.length
35
+ when 0
36
+ raise ArgumentError, 'No policy given'
37
+ when 1
38
+ authorize_one(subject, target, *policies)
39
+ else
40
+ authorize_many(subject, target, *policies)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def authorize_one(subject, target, policy)
47
+ namespace = @namespace_resolver.call(target)
48
+ policy = container.resolve("#{namespace}.#{policy}")
49
+
50
+ policy.call(subject, target).to_result
51
+ end
52
+
53
+ def authorize_many(subject, target, *policies)
54
+ namespace = @namespace_resolver.call(target)
55
+
56
+ Success(
57
+ List.new(policies)
58
+ .fmap { |policy| "#{namespace}.#{policy}" }
59
+ .fmap(container.method(:resolve))
60
+ .fmap { |policy| yield policy.call(subject, target) }
61
+ )
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/inflector'
4
+
5
+ module Lamassu
6
+ # Default namespace resolver used for scopes in Lamassu
7
+ #
8
+ # If target is a symbol or string, it is used as is. If passed a Module,
9
+ # it is translated underscorized string. For any other object, it resolves
10
+ # the targets class and it's translated as above.
11
+ class NamespaceResolver
12
+ def initialize(inflector: Dry::Inflector.new)
13
+ @inflector = inflector
14
+ end
15
+
16
+ def call(target)
17
+ case target
18
+ when Module
19
+ coerce_module(target)
20
+ when String, Symbol
21
+ coerce_string(target)
22
+ else
23
+ coerce_object(target)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def coerce_string(target)
30
+ target.to_s
31
+ end
32
+
33
+ def coerce_object(target)
34
+ @inflector.underscore(target.class)
35
+ end
36
+
37
+ def coerce_module(target)
38
+ @inflector.underscore(target)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads/result'
4
+
5
+ module Lamassu
6
+ # Base module to be included in policy objects
7
+ module Policy
8
+ def self.included(klass)
9
+ klass.class_eval do
10
+ include Dry::Monads::Result::Mixin
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads/result'
4
+
5
+ module Lamassu
6
+ module PolicyAdapters
7
+ # Policy Adapter for a callable wrapping returned value to Success on true
8
+ # and Failure on false
9
+ class Check
10
+ include Dry::Monads::Result::Mixin
11
+
12
+ attr_reader :policy
13
+
14
+ def initialize(policy)
15
+ @policy = policy
16
+ end
17
+
18
+ def call(*args)
19
+ value = policy.call(*args)
20
+
21
+ value ? Success(value) : Failure(value)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads/result'
4
+
5
+ module Lamassu
6
+ module PolicyAdapters
7
+ # Policy Adapter for a callable wrapping returned value to Success
8
+ class Map
9
+ include Dry::Monads::Result::Mixin
10
+
11
+ attr_reader :policy
12
+
13
+ def initialize(policy)
14
+ @policy = policy
15
+ end
16
+
17
+ def call(*args)
18
+ value = policy.call(*args)
19
+
20
+ Success(value)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-container'
4
+ require 'dry/monads/result'
5
+ require 'lamassu/policy_adapters/check'
6
+ require 'lamassu/policy_adapters/map'
7
+
8
+ module Lamassu
9
+ # Dry::Container with convenience methods when registering policy objects
10
+ class PolicyContainer
11
+ include Dry::Container::Mixin
12
+ include Dry::Monads::Result::Mixin
13
+
14
+ # @param [Module] scope
15
+ # @param [Proc] block
16
+ # @return [PolicyContainer]
17
+ def for(scope, &block)
18
+ container = PolicyContainer.new
19
+ container.instance_eval(&block)
20
+
21
+ merge(container, namespace: Lamassu.namespace_resolver.call(scope))
22
+ end
23
+
24
+ # @param [String,Symbol] key
25
+ # @param [#call] policy_object
26
+ def policy(key, policy_object)
27
+ register(key, policy_object, call: false)
28
+ end
29
+
30
+ # @param [String,Symbol] key
31
+ # @param [#call] policy_object
32
+ def check(key, policy_object)
33
+ policy(key, PolicyAdapters::Check.new(policy_object))
34
+ end
35
+
36
+ # @param [String,Symbol] key
37
+ # @param [#call] policy_object
38
+ def map(key, policy_object)
39
+ policy(key, PolicyAdapters::Map.new(policy_object))
40
+ end
41
+ end
42
+ end
data/lib/lamassu.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/configurable'
4
+ require 'lamassu/namespace_resolver'
5
+
6
+ # Lamassu is container and policy based authorization framework
7
+ module Lamassu
8
+ extend Dry::Configurable
9
+
10
+ setting :namespace_resolver, Lamassu::NamespaceResolver.new, reader: true
11
+ end
12
+
13
+ require 'lamassu/guardian'
14
+ require 'lamassu/policy'
metadata ADDED
@@ -0,0 +1,252 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lamassu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jo-Herman Haugholt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-container
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-inflector
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.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.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-monads
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.16'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mutant
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: mutant-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.8.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.8.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.11.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.11.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '12.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '12.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: reek
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '4.7'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '4.7'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.52'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.52'
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '0.16'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '0.16'
195
+ description:
196
+ email:
197
+ - johannes@huyderman.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".editorconfig"
203
+ - ".gitignore"
204
+ - ".gitlab-ci.yml"
205
+ - ".mdlrc"
206
+ - ".reek"
207
+ - ".reek.yml"
208
+ - ".rspec"
209
+ - ".rubocop.yml"
210
+ - ".travis.yml"
211
+ - CHANGELOG.md
212
+ - CODE_OF_CONDUCT.md
213
+ - Gemfile
214
+ - LICENSE.txt
215
+ - README.md
216
+ - Rakefile
217
+ - VERSION
218
+ - bin/console
219
+ - bin/setup
220
+ - lamassu.gemspec
221
+ - lib/lamassu.rb
222
+ - lib/lamassu/guardian.rb
223
+ - lib/lamassu/namespace_resolver.rb
224
+ - lib/lamassu/policy.rb
225
+ - lib/lamassu/policy_adapters/check.rb
226
+ - lib/lamassu/policy_adapters/map.rb
227
+ - lib/lamassu/policy_container.rb
228
+ homepage: https://github.com/huyderman/lamassu
229
+ licenses:
230
+ - MIT
231
+ metadata: {}
232
+ post_install_message:
233
+ rdoc_options: []
234
+ require_paths:
235
+ - lib
236
+ required_ruby_version: !ruby/object:Gem::Requirement
237
+ requirements:
238
+ - - ">="
239
+ - !ruby/object:Gem::Version
240
+ version: '0'
241
+ required_rubygems_version: !ruby/object:Gem::Requirement
242
+ requirements:
243
+ - - ">="
244
+ - !ruby/object:Gem::Version
245
+ version: '0'
246
+ requirements: []
247
+ rubyforge_project:
248
+ rubygems_version: 2.7.7
249
+ signing_key:
250
+ specification_version: 4
251
+ summary: Autorization gem based on policy objects and dry-container
252
+ test_files: []