Module: Shrine::Plugins::AwsLambda::AttacherClassMethods

Defined in:
lib/shrine/plugins/aws_lambda.rb

Instance Method Summary collapse

Instance Method Details

#lambda_authorize(headers, body) ⇒ Array, false

Parses the payload of the Lambda request to the ‘callbackUrl` and loads the Shrine Attacher from the received context. Fetches the signing key from the attacher’s record metadata and uses it for calculating the signature of the received from Lambda request. Then it compares the calculated and received signatures, returning an error if the signatures mismatch.

If the signatures are equal, it returns the attacher and the hash of the parsed result from Lambda, else - it returns false.

Parameters:

  • headers (Hash)

    from the Lambda request

  • body (String)

    of the Lambda request

Options Hash (headers):

  • 'User-Agent' (String)

    The AWS Lambda function user agent

  • 'Content-Type' (String)

    ‘application/json’

  • 'Host' (String)
  • 'X-Amz-Date' (String)

    The AWS Lambda function user agent

  • 'Authorization' (String)

    The AWS authorization string

Returns:

  • (Array)

    Shrine Attacher and the Lambda result (the request body parsed to a hash) if signature in received headers matches locally computed AWS signature

  • (false)

    if signature in received headers does’t match locally computed AWS signature



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/shrine/plugins/aws_lambda.rb', line 89

def lambda_authorize(headers, body)
  result = JSON.parse(body)
  context = result['context']

  context_record = context['record']
  record_class   = context_record[0]
  record_id      = context_record[1]
  record         = Object.const_get(record_class).find(record_id)
  attacher_name  = context['name']
  attacher       = record.__send__(:"#{attacher_name}_attacher")

  return false unless signature_matched?(attacher, headers, body)

  [attacher, result]
end

#lambda_process(attacher_class, record_class, record_id, name, file_data) ⇒ Object

Loads the attacher from the data, and triggers its instance AWS Lambda processing method. Intended to be used in a background job.



62
63
64
65
66
67
68
69
# File 'lib/shrine/plugins/aws_lambda.rb', line 62

def lambda_process(attacher_class, record_class, record_id, name, file_data)
  attacher_class = Object.const_get(attacher_class)
  record         = Object.const_get(record_class).find(record_id) # if using Active Record

  attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
  attacher.lambda_process
  attacher
end