hasura_handler 0.1.4 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 391dc0c3183a5a8ac53709ee48c14a0ee5fae6c085a21d200972556b732fcd1a
4
- data.tar.gz: 32122de81ca8c930934383944f0387bdeb9e566a026aea488be203f0610d7137
3
+ metadata.gz: b476da389358677671ebee56a1d014dc90779a73ade92eaf6d17f894192d0f4c
4
+ data.tar.gz: 1aef41672a1832e2abe57e78b260fdef87a0e0147c470898ccb17dfb3b05c791
5
5
  SHA512:
6
- metadata.gz: 687179bd480090e6a46e06f63aff4901f3b609f3321f91aba92b6a238a140bbb7220723261a25ce44cf30ce3dddbc023eded6e9e98c16c25a19bda3448b2928d
7
- data.tar.gz: e7b506c5cd0904a626390b9ed76e02d0da34729b9299264b5457fb248dcc398d134a198364fdca36768111b1a5cd3ceabcd69042cd1505e9f5b956ccc7fdc354
6
+ metadata.gz: 466c57748a94c57040c9d963af6982f4796370c30c582c0c3a306b5d4bc0573f3d3989d6a4e3702f7c162c8e5ad659ee50b032aecc7ac606003dddafca9ffe1a
7
+ data.tar.gz: 921d0f38484c4d6f127660e7a999ef5af1a0bce82013aeccace3abf1ca6d4468c567d11355dc35691c97480056ad88829cba2dd0984ba9b6208679f62ff2b02b
data/README.md CHANGED
@@ -1,28 +1,9 @@
1
- # HasuraHandler![tests](https://github.com/KazW/HasuraHandler/workflows/tests/badge.svg)
2
- Short description and motivation.
1
+ # HasuraHandler ![Gem Version](https://badge.fury.io/rb/hasura_handler.svg) [![tests](https://github.com/KazW/HasuraHandler/workflows/tests/badge.svg)](https://github.com/KazW/HasuraHandler/actions?query=workflow%3Atests) [![Maintainability](https://api.codeclimate.com/v1/badges/38864d7565ab11729b6b/maintainability)](https://codeclimate.com/github/KazW/HasuraHandler/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/38864d7565ab11729b6b/test_coverage)](https://codeclimate.com/github/KazW/HasuraHandler/test_coverage)
2
+ HasuraHandler is a Rails framework that makes building microservices for Hasura easy.
3
+ HasuraHandler also simplifies adding Hasura to an existing Rails app.
3
4
 
4
5
  ## Usage
5
- How to use my plugin.
6
-
7
- ## Installation
8
- Add this line to your application's Gemfile:
9
-
10
- ```ruby
11
- gem 'hasura_handler'
12
- ```
13
-
14
- And then execute:
15
- ```bash
16
- $ bundle
17
- ```
18
-
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install hasura_handler
22
- ```
23
-
24
- ## Contributing
25
- Contribution directions go here.
6
+ Please see the [documentation site](https://kazw.github.io/HasuraHandler).
26
7
 
27
8
  ## License
28
9
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -2,29 +2,27 @@ require_dependency 'hasura_handler/application_controller'
2
2
 
3
3
  module HasuraHandler
4
4
  class ActionsController < ApplicationController
5
+ before_action :check_header
6
+
5
7
  def index
6
- unless HasuraHandler::Action.hasura_actions.keys.include?(action_params['action']['name'])
8
+ unless HasuraHandler::Action.hasura_actions.keys.include?(raw_params['action']['name'])
7
9
  render json: { error: true, message: 'action name not registered' }, status: 404
8
10
  return
9
11
  end
10
12
 
11
- klass = HasuraHandler::Action.hasura_actions[action_params['action']['name']]
12
- action = klass.new(action_params['session_variables'].to_h, action_params['input'].to_h)
13
- action.run
13
+ klass = HasuraHandler::Action.hasura_actions[raw_params['action']['name']]
14
+ action = klass.new(
15
+ clean_headers,
16
+ raw_params['session_variables'].to_h,
17
+ raw_params['input'].to_h
18
+ )
14
19
 
20
+ action.run
15
21
  if action.error_message.present?
16
22
  render json: { error: true, message: action.error_message }, status: 400
17
23
  else
18
24
  render json: action.output
19
25
  end
20
26
  end
21
-
22
- def action_params
23
- full_params.permit(
24
- action: [:name],
25
- input: {},
26
- session_variables: {}
27
- )
28
- end
29
27
  end
30
28
  end
@@ -1,6 +1,8 @@
1
1
  module HasuraHandler
2
2
  class ApplicationController < ActionController::API
3
- before_action :check_header
3
+ def protect_against_forgery?
4
+ false
5
+ end
4
6
 
5
7
  private
6
8
 
@@ -10,8 +12,17 @@ module HasuraHandler
10
12
  end
11
13
  end
12
14
 
13
- def full_params
14
- ActionController::Parameters.new(JSON.parse(request.body.read))
15
+ def raw_params
16
+ @raw_params ||= JSON.parse(request.raw_post)
17
+ end
18
+
19
+ def clean_headers
20
+ request.
21
+ headers.
22
+ reject{ |k,v| k.include?('.') }.
23
+ to_h.
24
+ select{ |k,v| k =~ /\AHTTP_/ }.
25
+ to_h
15
26
  end
16
27
  end
17
28
  end
@@ -0,0 +1,39 @@
1
+ require_dependency "hasura_handler/application_controller"
2
+
3
+ module HasuraHandler
4
+ class AuthHookController < ApplicationController
5
+ def get_mode
6
+ @headers = clean_headers
7
+ authenticate
8
+ end
9
+
10
+ def post_mode
11
+ @headers = raw_params['headers'].
12
+ to_h.
13
+ map{ |k,v| [standardize_header(k), v] }.
14
+ to_h
15
+
16
+ authenticate
17
+ end
18
+
19
+ private
20
+
21
+ def authenticate
22
+ @authenticator = HasuraHandler.
23
+ authenticator.
24
+ to_s.
25
+ constantize.
26
+ new(@headers)
27
+
28
+ if @authenticator.success?
29
+ render json: @authenticator.response, status: 200
30
+ else
31
+ render json: { error: true, message: @authenticator.error_message }, status: 401
32
+ end
33
+ end
34
+
35
+ def standardize_header(header)
36
+ "HTTP_#{header.to_s.gsub('-', '_').upcase}"
37
+ end
38
+ end
39
+ end
@@ -2,8 +2,10 @@ require_dependency 'hasura_handler/application_controller'
2
2
 
3
3
  module HasuraHandler
4
4
  class EventsController < ApplicationController
5
+ before_action :check_header
6
+
5
7
  def index
6
- processor = HasuraHandler::EventProcessor.new(event_params.to_h)
8
+ processor = HasuraHandler::EventProcessor.new(raw_params)
7
9
 
8
10
  unless processor.event.valid?
9
11
  error_response(processor.event.errors)
@@ -30,29 +32,5 @@ module HasuraHandler
30
32
  response.set_header('Retry-After', HasuraHandler.retry_after)
31
33
  render json: { success: false, errors: errors }, status: 400
32
34
  end
33
-
34
- def event_params
35
- full_params.permit(
36
- :id,
37
- :created_at,
38
- table: [
39
- :schema,
40
- :name
41
- ],
42
- trigger: [
43
- :name
44
- ],
45
- event: [
46
- :op,
47
- {
48
- session_variables: {},
49
- data: {
50
- new: {},
51
- old: {}
52
- }
53
- }
54
- ]
55
- )
56
- end
57
35
  end
58
36
  end
@@ -1,4 +1,9 @@
1
1
  HasuraHandler::Engine.routes.draw do
2
+ if HasuraHandler.authentication_enabled
3
+ get '/auth', to: 'auth_hook#get_mode'
4
+ post '/auth', to: 'auth_hook#post_mode'
5
+ end
6
+
2
7
  if HasuraHandler.events_enabled
3
8
  post '/events', to: 'events#index'
4
9
  end
@@ -1,4 +1,5 @@
1
1
  require 'hasura_handler/engine'
2
+ require 'hasura_handler/authenticator'
2
3
  require 'hasura_handler/event_handler'
3
4
  require 'hasura_handler/event_processor'
4
5
  require 'hasura_handler/event'
@@ -8,6 +9,8 @@ module HasuraHandler
8
9
  class << self
9
10
  mattr_accessor :auth_header,
10
11
  :auth_key,
12
+ :authentication_enabled,
13
+ :authenticator,
11
14
  :events_enabled,
12
15
  :actions_enabled,
13
16
  :event_job_queue,
@@ -17,6 +20,8 @@ module HasuraHandler
17
20
  :retry_after
18
21
 
19
22
  self.auth_header = 'HTTP_X_HASURA_SERVICE_KEY'
23
+ self.authentication_enabled = false
24
+ self.authenticator = nil
20
25
  self.events_enabled = true
21
26
  self.async_events = true
22
27
  self.fanout_events = true
@@ -28,8 +33,13 @@ module HasuraHandler
28
33
 
29
34
  def self.setup(&block)
30
35
  yield self
31
- [:auth_key].each do |key|
32
- raise "HasuraHandler requires the #{key} to be configured." if self.send(key).blank?
36
+
37
+ if (self.events_enabled || self.actions_enabled) && self.auth_key.blank?
38
+ raise 'HasuraHandler requires the auth_key to be configured if actions or events are enabled.'
39
+ end
40
+
41
+ if self.authentication_enabled && self.authenticator.blank?
42
+ raise 'HasuraHandler requires the authenticator to be configured if authentication hook is enabled.'
33
43
  end
34
44
  end
35
45
  end
@@ -17,11 +17,13 @@ module HasuraHandler
17
17
  end
18
18
 
19
19
  attr_reader :session_variables,
20
+ :headers,
20
21
  :input,
21
22
  :output,
22
23
  :error_message
23
24
 
24
- def initialize(session_variables, input)
25
+ def initialize(headers, session_variables, input)
26
+ @headers = headers
25
27
  @session_variables = session_variables
26
28
  @input = input
27
29
  @output = {}
@@ -0,0 +1,22 @@
1
+ module HasuraHandler
2
+ class Authenticator
3
+ attr_reader :headers
4
+ attr_accessor :response,
5
+ :error_message
6
+
7
+ def initialize(headers)
8
+ @headers = headers
9
+ @response = {}
10
+ end
11
+
12
+ def success?
13
+ begin
14
+ authenticate
15
+ ensure
16
+ return false if @response.blank?
17
+ end
18
+
19
+ @response.present? || @error_message.blank?
20
+ end
21
+ end
22
+ end
@@ -2,5 +2,13 @@ module HasuraHandler
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace HasuraHandler
4
4
  config.generators.api_only = true
5
+
6
+ if Rails.version.to_f < 6
7
+ config.eager_load_paths += Dir[Rails.root.join('app', '{actions,reactions}', '*.rb')]
8
+ end
9
+
10
+ config.to_prepare do
11
+ Dir[Rails.root.join('app', '{actions,reactions}', '*.rb')].each{ |file| require_dependency file }
12
+ end
5
13
  end
6
14
  end
@@ -1,5 +1,7 @@
1
1
  module HasuraHandler
2
2
  class EventHandler
3
+ extend ActiveSupport::DescendantsTracker
4
+
3
5
  class << self
4
6
  attr_reader :hasura_matchers
5
7
 
@@ -8,8 +10,8 @@ module HasuraHandler
8
10
  allowed_matchers = [:table, :trigger, :op]
9
11
 
10
12
  matchers.keys.each do |matcher|
11
- raise 'invalid matcher' unless allowed_matchers.include?(matcher)
12
- raise 'invalid matcher value' unless matchers[matcher].is_a?(String)
13
+ raise "invalid matcher: #{matcher}" unless allowed_matchers.include?(matcher)
14
+ raise "invalid matcher value for: #{matcher}" unless matchers[matcher].is_a?(String)
13
15
  end
14
16
 
15
17
  @hasura_matchers = matchers
metadata CHANGED
@@ -1,36 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hasura_handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaz Walker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-26 00:00:00.000000000 Z
11
+ date: 2020-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 6.0.3
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: 6.0.3.2
19
+ version: '5.0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: 6.0.3
30
24
  - - ">="
31
25
  - !ruby/object:Gem::Version
32
- version: 6.0.3.2
33
- description: Provides an easy way to build custom backends for Hasura.
26
+ version: '5.0'
27
+ description: HasuraHandler is a Rails framework that makes building microservices
28
+ for Hasura easy.
34
29
  email:
35
30
  - me@kaz.codes
36
31
  executables: []
@@ -42,6 +37,7 @@ files:
42
37
  - Rakefile
43
38
  - app/controllers/hasura_handler/actions_controller.rb
44
39
  - app/controllers/hasura_handler/application_controller.rb
40
+ - app/controllers/hasura_handler/auth_hook_controller.rb
45
41
  - app/controllers/hasura_handler/events_controller.rb
46
42
  - app/jobs/hasura_handler/application_job.rb
47
43
  - app/jobs/hasura_handler/event_handler_job.rb
@@ -49,16 +45,20 @@ files:
49
45
  - config/routes.rb
50
46
  - lib/hasura_handler.rb
51
47
  - lib/hasura_handler/action.rb
48
+ - lib/hasura_handler/authenticator.rb
52
49
  - lib/hasura_handler/engine.rb
53
50
  - lib/hasura_handler/event.rb
54
51
  - lib/hasura_handler/event_handler.rb
55
52
  - lib/hasura_handler/event_processor.rb
56
- - lib/hasura_handler/version.rb
57
53
  - lib/tasks/hasura_handler_tasks.rake
58
- homepage: https://github.com/KazW/HasuraHandler
54
+ homepage: https://kazw.github.io/HasuraHandler
59
55
  licenses:
60
56
  - MIT
61
- metadata: {}
57
+ metadata:
58
+ bug_tracker_uri: https://github.com/KazW/HasuraHandler/issues
59
+ documentation_uri: https://kazw.github.io/HasuraHandler
60
+ homepage_uri: https://kazw.github.io/HasuraHandler
61
+ source_code_uri: https://github.com/KazW/HasuraHandler
62
62
  post_install_message:
63
63
  rdoc_options: []
64
64
  require_paths:
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  requirements: []
77
- rubygems_version: 3.1.4
77
+ rubygems_version: 3.1.2
78
78
  signing_key:
79
79
  specification_version: 4
80
80
  summary: Integrates Hasura with Rails
@@ -1,3 +0,0 @@
1
- module HasuraHandler
2
- VERSION = '0.1.4'
3
- end