Class: PayPro::Signature
- Inherits:
-
Object
- Object
- PayPro::Signature
- Defined in:
- lib/pay_pro/signature.rb
Constant Summary collapse
- DEFAULT_TOLERANCE =
Default timestamp tolerance is 10 minutes
600
Instance Method Summary collapse
- #generate_signature ⇒ Object
-
#initialize(payload:, timestamp:, secret:, tolerance: DEFAULT_TOLERANCE) ⇒ Signature
constructor
A new instance of Signature.
- #verify(signature:) ⇒ Object
Constructor Details
#initialize(payload:, timestamp:, secret:, tolerance: DEFAULT_TOLERANCE) ⇒ Signature
Returns a new instance of Signature.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pay_pro/signature.rb', line 8 def initialize( payload:, timestamp:, secret:, tolerance: DEFAULT_TOLERANCE ) raise ArgumentError, 'timestamp must be an instance of Time' unless .is_a?(Time) raise ArgumentError, 'payload must be a String' unless payload.is_a?(String) raise ArgumentError, 'secret must be a String' unless secret.is_a?(String) raise ArgumentError, 'tolerance must be an Integer' unless tolerance.is_a?(Integer) @payload = payload @timestamp = .utc @secret = secret @tolerance = tolerance end |
Instance Method Details
#generate_signature ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/pay_pro/signature.rb', line 25 def generate_signature OpenSSL::HMAC.hexdigest( OpenSSL::Digest.new('sha256'), @secret, signature_string ) end |
#verify(signature:) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pay_pro/signature.rb', line 33 def verify(signature:) unless OpenSSL.secure_compare(signature, generate_signature) raise SignatureVerificationError.new( message: 'Signature does not match', http_body: @payload ) end if @timestamp < Time.now.utc - @tolerance = @timestamp.strftime('%F %T') raise SignatureVerificationError.new( message: "Timestamp is outside the tolerance zone: #{}", http_body: @payload ) end true end |