Class: WalleeRubySdk::WebhookEncryptionKeysService

Inherits:
Object
  • Object
show all
Defined in:
lib/wallee-ruby-sdk/service/webhook_encryption_keys_service.rb

Constant Summary collapse

CACHE =
Concurrent::Map.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client = ApiClient.default) ⇒ WebhookEncryptionKeysService

Returns a new instance of WebhookEncryptionKeysService.



30
31
32
# File 'lib/wallee-ruby-sdk/service/webhook_encryption_keys_service.rb', line 30

def initialize(api_client = ApiClient.default)
  @api_client = api_client
end

Instance Attribute Details

#api_clientObject

Returns the value of attribute api_client.



26
27
28
# File 'lib/wallee-ruby-sdk/service/webhook_encryption_keys_service.rb', line 26

def api_client
  @api_client
end

Instance Method Details

#get_webhooks_encryption_keys_id(id, opts = {}) ⇒ String

Retrieve a webhook encryption key

Parameters:

  • id (String)
  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:

  • (String)


37
38
39
40
# File 'lib/wallee-ruby-sdk/service/webhook_encryption_keys_service.rb', line 37

def get_webhooks_encryption_keys_id(id, opts = {})
  data, _status_code, _headers = get_webhooks_encryption_keys_id_with_http_info(id, opts)
  data
end

#get_webhooks_encryption_keys_id_with_http_info(id, opts = {}) ⇒ Array<(String, Integer, Hash)>

Returns String data, response status code and response headers.

Parameters:

  • id (String)
  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:

  • (Array<(String, Integer, Hash)>)

    String data, response status code and response headers



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/wallee-ruby-sdk/service/webhook_encryption_keys_service.rb', line 47

def get_webhooks_encryption_keys_id_with_http_info(id, opts = {})
  if @api_client.config.debugging
    @api_client.config.logger.debug 'Calling API: WebhookEncryptionKeysService.get_webhooks_encryption_keys_id ...'
  end
  # verify the required parameter 'id' is set
  if @api_client.config.client_side_validation && id.nil?
    fail ArgumentError, "Missing the required parameter 'id' when calling WebhookEncryptionKeysService.get_webhooks_encryption_keys_id"
  end
  # resource path
  local_var_path = '/webhooks/encryption-keys/{id}'.sub('{' + 'id' + '}', CGI.escape(id.to_s))

  # query parameters
  query_params = opts[:query_params] || {}

  # header parameters
  header_params = opts[:header_params] || {}
  # HTTP header 'Accept' (if needed)
  header_params['Accept'] = @api_client.select_header_accept(['text/plain', 'application/json']) unless header_params['Accept']

  # form parameters
  form_params = opts[:form_params] || {}

  # connection timeout
  connection_timeout = @api_client.config.timeout

  # http body (model)
  post_body = opts[:debug_body]

  # return_type
  return_type = opts[:debug_return_type] || 'String'

  new_options = opts.merge(
    :operation => :"WebhookEncryptionKeysService.get_webhooks_encryption_keys_id",
    :header_params => header_params,
    :query_params => query_params,
    :form_params => form_params,
    :body => post_body,
    :return_type => return_type
  )

  data, status_code, headers = @api_client.call_api(:GET.to_sym.downcase, local_var_path, new_options, connection_timeout)
  if @api_client.config.debugging
    @api_client.config.logger.debug "API called: WebhookEncryptionKeysService#get_webhooks_encryption_keys_id\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}\nConnection Timeout: #{connection_timeout}"
  end
  return data, status_code, headers
end

#is_content_valid(signature_header, content_to_verify) ⇒ bool

Returns if webhook content body conforms with signature header.

Parameters:

  • signature_header (String)

    Signature header ‘X-Signature’ value from the Http request

  • content_to_verify (String)

    Raw webhook content in String format

Returns:

  • (bool)

    if webhook content body conforms with signature header



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wallee-ruby-sdk/service/webhook_encryption_keys_service.rb', line 100

def is_content_valid(signature_header, content_to_verify)
  # Regular expression to extract algorithm, keyId, and signature from the signature_header
  regex = /^algorithm=([a-zA-Z0-9]+),\skeyId=([a-z0-9\-]+),\ssignature=([a-zA-Z0-9+\/=]+)$/
  matcher = signature_header.match(regex)

  if matcher
    signature_algorithm = matcher[1]
    public_key_id = matcher[2]
    content_signature = matcher[3]

    if CACHE.key?(public_key_id)
      public_key = CACHE[public_key_id]
    else
      fetched_key, = get_webhooks_encryption_keys_id_with_http_info(public_key_id)
      raise WalleeSdkException.new(ErrorCode::UNKNOWN_WEBHOOK_ENCRYPTION_PUBLIC_KEY,
      "Could not retrieve public key with ID: #{public_key_id}") if fetched_key.nil? || fetched_key.strip.empty?

      CACHE[public_key_id] = fetched_key
      public_key = fetched_key
    end

  return EncryptionUtil.is_content_valid(
    content_to_verify,
    content_signature,
    public_key,
    signature_algorithm
  )
  else
    raise WalleeSdkException.new(ErrorCode::INVALID_WEBHOOK_ENCRYPTION_HEADER_FORMAT,
    "Invalid webhook encryption header format. Expected format: 'algorithm=<algorithm>, keyId=<keyId>, signature=<signature>'")
  end
end