Module: Shrine::Plugins::Lambda::AttacherClassMethods
- Defined in:
- lib/shrine/plugins/shrine-lambda.rb
Instance Method Summary collapse
-
#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.
-
#lambda_process(data) ⇒ Object
Loads the attacher from the data, and triggers its instance AWS Lambda processing method.
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.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/shrine/plugins/shrine-lambda.rb', line 82 def (headers, body) result = JSON.parse(body) attacher = load(result.delete('context')) incoming_auth_header = auth_header_hash(headers['Authorization']) signer = build_signer( incoming_auth_header['Credential'].split('/'), JSON.parse(attacher.record.__send__(:"#{attacher.data_attribute}") || '{}').dig('metadata', 'key') || 'key', headers['x-amz-security-token'] ) signature = signer.sign_request(http_method: 'PUT', url: Shrine.opts[:callback_url], headers: { 'X-Amz-Date' => headers['X-Amz-Date'] }, body: body) calculated_signature = auth_header_hash(signature.headers['authorization'])['Signature'] return false if incoming_auth_header['Signature'] != calculated_signature [attacher, result] end |
#lambda_process(data) ⇒ Object
Loads the attacher from the data, and triggers its instance AWS Lambda processing method. Intended to be used in a background job.
58 59 60 61 62 |
# File 'lib/shrine/plugins/shrine-lambda.rb', line 58 def lambda_process(data) attacher = load(data) attacher.lambda_process(data) attacher end |