Class: Legion::Extensions::Hooks::Base

Inherits:
Object
  • Object
show all
Extended by:
Definitions
Includes:
Legion::Extensions::Helpers::Lex
Defined in:
lib/legion/extensions/hooks/base.rb

Constant Summary

Constants included from Definitions

Definitions::DEFAULTS

Constants included from Legion::Extensions::Helpers::Base

Legion::Extensions::Helpers::Base::NAMESPACE_BOUNDARIES

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Definitions

definition, definition_for, definitions

Methods included from Legion::Extensions::Helpers::Lex

#default_settings, included, #runner_desc

Methods included from Legion::Extensions::Helpers::Base

#actor_class, #actor_const, #actor_name, #amqp_prefix, #calling_class, #calling_class_array, #from_json, #full_path, #lex_class, #lex_const, #lex_name, #lex_slug, #log_tag, #normalize, #runner_const, #runner_name, #segments, #settings_path, #table_prefix, #to_dotted_hash

Methods included from Legion::Extensions::Helpers::Task

#generate_task_id, #generate_task_log, #task_update

Methods included from Legion::Extensions::Helpers::Logger

#handle_runner_exception

Methods included from Legion::Extensions::Helpers::Secret

reset_identity!, resolve_identity!, #secret

Methods included from Legion::Extensions::Helpers::Core

#find_setting

Class Attribute Details

.mount_pathObject (readonly)

Returns the value of attribute mount_path.



56
57
58
# File 'lib/legion/extensions/hooks/base.rb', line 56

def mount_path
  @mount_path
end

.route_field_nameObject (readonly)

Returns the value of attribute route_field_name.



56
57
58
# File 'lib/legion/extensions/hooks/base.rb', line 56

def route_field_name
  @route_field_name
end

.route_header_nameObject (readonly)

Returns the value of attribute route_header_name.



56
57
58
# File 'lib/legion/extensions/hooks/base.rb', line 56

def route_header_name
  @route_header_name
end

.route_mappingObject (readonly)

Returns the value of attribute route_mapping.



56
57
58
# File 'lib/legion/extensions/hooks/base.rb', line 56

def route_mapping
  @route_mapping
end

.route_typeObject (readonly)

Returns the value of attribute route_type.



56
57
58
# File 'lib/legion/extensions/hooks/base.rb', line 56

def route_type
  @route_type
end

.verify_configObject (readonly)

Returns the value of attribute verify_config.



56
57
58
# File 'lib/legion/extensions/hooks/base.rb', line 56

def verify_config
  @verify_config
end

.verify_typeObject (readonly)

Returns the value of attribute verify_type.



56
57
58
# File 'lib/legion/extensions/hooks/base.rb', line 56

def verify_type
  @verify_type
end

Class Method Details

.mount(path) ⇒ Object

DSL: declare a sub-path suffix appended to the auto-generated hook route

mount '/callback'  # e.g. /api/extensions/microsoft_teams/hooks/auth/callback


52
53
54
# File 'lib/legion/extensions/hooks/base.rb', line 52

def mount(path)
  @mount_path = path
end

.route_field(field_name, mapping = {}) ⇒ Object

DSL: route based on a payload field value

route_field :event_type,
  'build.completed' => :on_build,
  'deploy.started'  => :on_deploy


27
28
29
30
31
# File 'lib/legion/extensions/hooks/base.rb', line 27

def route_field(field_name, mapping = {})
  @route_type = :field
  @route_field_name = field_name.to_sym
  @route_mapping = mapping.transform_keys(&:to_s)
end

.route_header(header_name, mapping = {}) ⇒ Object

DSL: route based on a request header value

route_header 'X-GitHub-Event',
  'push'         => :on_push,
  'pull_request' => :on_pull_request


17
18
19
20
21
# File 'lib/legion/extensions/hooks/base.rb', line 17

def route_header(header_name, mapping = {})
  @route_type = :header
  @route_header_name = header_name.upcase.tr('-', '_')
  @route_mapping = mapping.transform_keys(&:to_s)
end

.verify_hmac(header:, secret:, algorithm: 'SHA256', prefix: 'sha256=') ⇒ Object

DSL: verify via HMAC signature (GitHub, Slack, Stripe pattern)

verify_hmac header: 'X-Hub-Signature-256',
           secret: :webhook_secret,
           algorithm: 'SHA256',
           prefix: 'sha256='


38
39
40
41
# File 'lib/legion/extensions/hooks/base.rb', line 38

def verify_hmac(header:, secret:, algorithm: 'SHA256', prefix: 'sha256=')
  @verify_type = :hmac
  @verify_config = { header: header.upcase.tr('-', '_'), secret: secret, algorithm: algorithm, prefix: prefix }
end

.verify_token(header: 'Authorization', secret: :webhook_token) ⇒ Object

DSL: verify via bearer/static token in a header

verify_token header: 'Authorization', secret: :webhook_token


45
46
47
48
# File 'lib/legion/extensions/hooks/base.rb', line 45

def verify_token(header: 'Authorization', secret: :webhook_token)
  @verify_type = :token
  @verify_config = { header: header.upcase.tr('-', '_'), secret: secret }
end

Instance Method Details

#route(headers, payload) ⇒ Object

Determine which runner function to call. Returns a symbol (function name) or nil (unhandled).



64
65
66
67
68
69
70
71
72
73
# File 'lib/legion/extensions/hooks/base.rb', line 64

def route(headers, payload)
  case self.class.route_type
  when :header
    route_by_header(headers)
  when :field
    route_by_field(payload)
  else
    :handle # deprecated fallback; prefer explicit route_header/route_field
  end
end

#runner_classObject

Which runner class handles this hook’s functions. Default: the first runner in the extension, or one matching the hook name.



90
91
92
# File 'lib/legion/extensions/hooks/base.rb', line 90

def runner_class
  nil
end

#verify(headers, body) ⇒ Object

Verify the request is authentic. Returns true/false.



77
78
79
80
81
82
83
84
85
86
# File 'lib/legion/extensions/hooks/base.rb', line 77

def verify(headers, body)
  case self.class.verify_type
  when :hmac
    verify_hmac(headers, body)
  when :token
    verify_token(headers)
  else
    true
  end
end