nexus_cqrs_auth 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitlab-ci.yml +1 -1
- data/README.md +149 -1
- data/lib/nexus_cqrs_auth/helper.rb +7 -1
- data/lib/nexus_cqrs_auth/middleware.rb +3 -1
- data/lib/nexus_cqrs_auth/version.rb +1 -1
- data/nexus_cqrs_auth.gemspec +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e062f14cca2a6a41d143bf648edbe21a6e661056fa5f8a408aeaea5c75d9b69
|
4
|
+
data.tar.gz: 686b79c46f969565dc23ba210bc48ff4a42f9c2f309b2592f82e0174e9e652b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3168a7a3ae6bb2070a2b3790043c5630560502515d7305d87a64d36cdfd0ed88f969c036d292452a6be6758f5f045f147a0a6c25ff46463ad3b697b2d418b935
|
7
|
+
data.tar.gz: 7f64fc4f86f877b19621728360d1f9cbd1ed1afedbff3c8808dc250e9b729af99477c0148af58b64042fa75c88d785296c71d7c55119285b06b05b0bec3db63b
|
data/.gitlab-ci.yml
CHANGED
@@ -22,7 +22,7 @@ release:
|
|
22
22
|
- if: '$CI_COMMIT_TAG'
|
23
23
|
script:
|
24
24
|
- mkdir -p ~/.gem
|
25
|
-
- cp
|
25
|
+
- cp $RUBYGEMS_CREDENTIALS ~/.gem/credentials
|
26
26
|
- chmod 0600 ~/.gem/credentials
|
27
27
|
- gem update --system
|
28
28
|
- ruby --version
|
data/README.md
CHANGED
@@ -1 +1,149 @@
|
|
1
|
-
nexus_cqrs_auth
|
1
|
+
# nexus_cqrs_auth
|
2
|
+
|
3
|
+
Authorisation for the Nexus CQRS pattern.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'nexus_cqrs_auth'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nexus_cqrs_auth
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
When setting up the message bus, attach the authorisation middleware to it:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
middleware_stack = Middleware::Builder.new do |b|
|
27
|
+
b.use NexusCqrsAuth::AuthMiddleware
|
28
|
+
end
|
29
|
+
|
30
|
+
bus = Bus.new(middleware: middleware_stack)
|
31
|
+
```
|
32
|
+
|
33
|
+
You will also need to set metadata on each message (command/query) before dispatching it to the bus:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
command.set_metadata(:current_user, user)
|
37
|
+
execute(command)
|
38
|
+
```
|
39
|
+
|
40
|
+
How you set this data, and where you get the current user from is application specific.
|
41
|
+
|
42
|
+
For example, a helper included in all GraphQL types could look like this:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
module GraphQlCqrsHelpers
|
46
|
+
def execute(command)
|
47
|
+
command_executor.execute(enrich_message(command))
|
48
|
+
end
|
49
|
+
|
50
|
+
def query(query)
|
51
|
+
query_executor.execute(enrich_message(query))
|
52
|
+
end
|
53
|
+
|
54
|
+
def command_executor
|
55
|
+
@command_executor ||= $COMMAND_EXECUTOR
|
56
|
+
end
|
57
|
+
|
58
|
+
def query_executor
|
59
|
+
@query_executor ||= $QUERY_EXECUTOR
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def enrich_message(message)
|
65
|
+
message.set_metadata(:current_user, @context[:current_user])
|
66
|
+
message
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
You can then write various policies to setup authorisation in CQRS flows.
|
72
|
+
|
73
|
+
More information about policies can be found in the [Pundit documentation](https://github.com/varvet/pundit).
|
74
|
+
|
75
|
+
Remember to create a base policy at: `app/policies/application_policy.rb`
|
76
|
+
|
77
|
+
### Bus level policy
|
78
|
+
|
79
|
+
Create a policy class in `app/policies/my_message_policy.rb`
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class MyMessagePolicy < ApplicationPolicy
|
83
|
+
def initialize(user, message)
|
84
|
+
@user = user
|
85
|
+
@query = message
|
86
|
+
end
|
87
|
+
|
88
|
+
def authorise?
|
89
|
+
true
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
The `authorise?` method will be called before the message handler. If `authorise?` returns false, execution of the bus
|
95
|
+
will halt and a `Pundit::NotAuthorizedError` will be raised.
|
96
|
+
|
97
|
+
### Record level policy
|
98
|
+
|
99
|
+
You can write policies for records:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
class PostPolicy < ApplicationPolicy
|
103
|
+
def initialize(user, post)
|
104
|
+
@user = user
|
105
|
+
@post = post
|
106
|
+
end
|
107
|
+
|
108
|
+
def publish_post?
|
109
|
+
true
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
You can then authorise a particular `Post`s by calling the policy from a command handler:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
class PublishPostHandler < NexusCqrs::BaseCommandHandler
|
118
|
+
include NexusCqrsAuth
|
119
|
+
|
120
|
+
# @param [Commands::PublishPost] command
|
121
|
+
def call(command)
|
122
|
+
post = Post.find(command.post_id)
|
123
|
+
authorize(command, post)
|
124
|
+
post.is_published = true
|
125
|
+
post.save
|
126
|
+
end
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
The `NexusCqrsAuth` module must be included in the handler.
|
131
|
+
|
132
|
+
`authorize` should be called with the domain message (e.g. command) and the record. The policy for that record type
|
133
|
+
(e.g. `PostPolicy`) will be called and the scope with the same name as the command (`PublishPost` -> `publish_post?`)
|
134
|
+
will be called.
|
135
|
+
|
136
|
+
If the scope returns false, then a `Pundit::NotAuthorizedError` will be raised.
|
137
|
+
|
138
|
+
## Development
|
139
|
+
|
140
|
+
To contribute to this gem, simple clone the repository, run `bundle install` and run tests:
|
141
|
+
|
142
|
+
```shell script
|
143
|
+
bundle exec rspec
|
144
|
+
bundle exec rubocop
|
145
|
+
```
|
146
|
+
|
147
|
+
## Releasing
|
148
|
+
|
149
|
+
The release process is tied to the git tags. Simply creating a new tag and pushing will trigger a new release to rubygems.
|
data/nexus_cqrs_auth.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
%x(git ls-files -z).split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
16
|
end
|
17
17
|
spec.require_paths = ['lib']
|
18
|
-
spec.add_dependency('nexus_cqrs', '~>0.
|
18
|
+
spec.add_dependency('nexus_cqrs', '~>0.2')
|
19
19
|
spec.add_dependency('pundit')
|
20
20
|
spec.add_dependency('strings-case')
|
21
21
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nexus_cqrs_auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Harrison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nexus_cqrs
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: '0.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: '0.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: pundit
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
requirements: []
|
91
|
-
rubygems_version: 3.
|
91
|
+
rubygems_version: 3.2.26
|
92
92
|
signing_key:
|
93
93
|
specification_version: 4
|
94
94
|
summary: Authorisation for the Nexus CQRS pattern
|