Class: Mailgun::OptInHandler
- Inherits:
-
Object
- Object
- Mailgun::OptInHandler
- Defined in:
- lib/mailgun/lists/opt_in_handler.rb
Class Method Summary collapse
-
.generate_hash(mailing_list, secret_app_id, recipient_address) ⇒ String
Generates a hash that can be used to validate opt-in recipients.
-
.validate_hash(secret_app_id, unique_hash) ⇒ Hash or Boolean
Validates the hash provided from the generate_hash method.
Class Method Details
.generate_hash(mailing_list, secret_app_id, recipient_address) ⇒ String
Generates a hash that can be used to validate opt-in recipients. Encodes all the necessary data in the URL.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mailgun/lists/opt_in_handler.rb', line 18 def self.generate_hash(mailing_list, secret_app_id, recipient_address) innerPayload = {'l' => mailing_list, 'r' => recipient_address} innerPayloadEncoded = Base64.encode64(JSON.generate(innerPayload)) sha1_digest = OpenSSL::Digest.new('sha1') digest = OpenSSL::HMAC.hexdigest(sha1_digest, secret_app_id, innerPayloadEncoded) outerPayload = {'h' => digest, 'p' => innerPayloadEncoded} outerPayloadEncoded = Base64.encode64(JSON.generate(outerPayload)) URI.escape(outerPayloadEncoded) end |
.validate_hash(secret_app_id, unique_hash) ⇒ Hash or Boolean
Validates the hash provided from the generate_hash method.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mailgun/lists/opt_in_handler.rb', line 41 def self.validate_hash(secret_app_id, unique_hash) outerPayload = JSON.parse(Base64.decode64(URI.unescape(unique_hash))) sha1_digest = OpenSSL::Digest.new('sha1') generated_hash = OpenSSL::HMAC.hexdigest(sha1_digest, secret_app_id, outerPayload['p']) innerPayload = JSON.parse(Base64.decode64(URI.unescape(outerPayload['p']))) hash_provided = outerPayload['h'] if(generated_hash == hash_provided) return {'recipient_address' => innerPayload['r'], 'mailing_list' => innerPayload['l']} else return false end end |