Class: LanguageOperator::Dsl::WebhookAuthentication

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/dsl/webhook_authentication.rb

Overview

Defines authentication configuration for webhooks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWebhookAuthentication

Returns a new instance of WebhookAuthentication.



9
10
11
12
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 9

def initialize
  @type = nil
  @config = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 7

def config
  @config
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 7

def type
  @type
end

Instance Method Details

#all_of(&block) ⇒ Object

Allow multiple authentication methods (all must succeed)



103
104
105
106
107
108
109
110
111
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 103

def all_of(&block)
  @type = :all_of
  @config[:methods] = []
  @current_methods = @config[:methods]
  instance_eval(&block) if block
  @current_methods = nil
  # Restore type in case it was overwritten by method calls
  @type = :all_of
end

#any_of(&block) ⇒ Object

Allow multiple authentication methods (any can succeed)



92
93
94
95
96
97
98
99
100
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 92

def any_of(&block)
  @type = :any_of
  @config[:methods] = []
  @current_methods = @config[:methods]
  instance_eval(&block) if block
  @current_methods = nil
  # Restore type in case it was overwritten by method calls
  @type = :any_of
end

#verify_api_key(header:, key:) ⇒ Object

Verify API key from header

Parameters:

  • header (String)

    Header name containing the API key

  • key (String)

    Expected API key value



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 37

def verify_api_key(header:, key:)
  if @current_methods
    auth = WebhookAuthentication.new
    auth.verify_api_key(header: header, key: key)
    @current_methods << auth
  else
    @type = :api_key
    @config[:header] = header
    @config[:key] = key
  end
end

#verify_basic_auth(username:, password:) ⇒ Object

Verify basic auth credentials

Parameters:

  • username (String)

    Expected username

  • password (String)

    Expected password



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 65

def verify_basic_auth(username:, password:)
  if @current_methods
    auth = WebhookAuthentication.new
    auth.verify_basic_auth(username: username, password: password)
    @current_methods << auth
  else
    @type = :basic_auth
    @config[:username] = username
    @config[:password] = password
  end
end

#verify_bearer_token(token:) ⇒ Object

Verify bearer token

Parameters:

  • token (String)

    Expected bearer token value



51
52
53
54
55
56
57
58
59
60
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 51

def verify_bearer_token(token:)
  if @current_methods
    auth = WebhookAuthentication.new
    auth.verify_bearer_token(token: token)
    @current_methods << auth
  else
    @type = :bearer_token
    @config[:token] = token
  end
end

#verify_custom {|context| ... } ⇒ Object

Custom authentication callback

Yields:

  • (context)

    Block receives request context hash

Yield Returns:

  • (Boolean)

    true if authenticated, false otherwise



80
81
82
83
84
85
86
87
88
89
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 80

def verify_custom(&block)
  if @current_methods
    auth = WebhookAuthentication.new
    auth.verify_custom(&block)
    @current_methods << auth
  else
    @type = :custom
    @config[:callback] = block
  end
end

#verify_signature(header:, secret:, algorithm: :sha256, prefix: nil) ⇒ Object

Verify HMAC signature (GitHub/Stripe style)

Parameters:

  • header (String)

    Header name containing the signature

  • secret (String)

    Secret key for HMAC

  • algorithm (Symbol) (defaults to: :sha256)

    Hash algorithm (:sha1, :sha256, :sha512)

  • prefix (String, nil) (defaults to: nil)

    Optional prefix to strip from signature (e.g., "sha256=")



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/language_operator/dsl/webhook_authentication.rb', line 19

def verify_signature(header:, secret:, algorithm: :sha256, prefix: nil)
  if @current_methods
    # Inside any_of/all_of - create child auth object
    auth = WebhookAuthentication.new
    auth.verify_signature(header: header, secret: secret, algorithm: algorithm, prefix: prefix)
    @current_methods << auth
  else
    @type = :signature
    @config[:header] = header
    @config[:secret] = secret
    @config[:algorithm] = algorithm
    @config[:prefix] = prefix
  end
end