|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'base64' |
| 4 | +require 'digest' |
| 5 | +require 'time' |
| 6 | +require 'jwt' |
| 7 | + |
| 8 | +module MessageBird |
| 9 | + class ValidationError < StandardError |
| 10 | + end |
| 11 | + |
| 12 | + ## |
| 13 | + # RequestValidator validates request signature signed by MessageBird services. |
| 14 | + # |
| 15 | + # @see https://developers.messagebird.com/docs/verify-http-requests |
| 16 | + class RequestValidator |
| 17 | + ALLOWED_ALGOS = %w[HS256 HS384 HS512].freeze |
| 18 | + |
| 19 | + ## |
| 20 | + # |
| 21 | + # @param [string] signature_key customer signature key. Can be retrieved through <a href="https://dashboard.messagebird.com/developers/settings">Developer Settings</a>. This is NOT your API key. |
| 22 | + # @param [bool] skip_url_validation whether url_hash claim validation should be skipped. Note that when true, no query parameters should be trusted. |
| 23 | + def initialize(signature_key, skip_url_validation = false) |
| 24 | + @signature_key = signature_key |
| 25 | + @skip_url_validation = skip_url_validation |
| 26 | + end |
| 27 | + |
| 28 | + ## |
| 29 | + # This method validates provided request signature, which is a JWT token. |
| 30 | + # This JWT is signed with a MessageBird account unique secret key, ensuring the request is from MessageBird and a specific account. |
| 31 | + # The JWT contains the following claims: |
| 32 | + # * "url_hash" - the raw URL hashed with SHA256 ensuring the URL wasn't altered. |
| 33 | + # * "payload_hash" - the raw payload hashed with SHA256 ensuring the payload wasn't altered. |
| 34 | + # * "jti" - a unique token ID to implement an optional non-replay check (NOT validated by default). |
| 35 | + # * "nbf" - the not before timestamp. |
| 36 | + # * "exp" - the expiration timestamp is ensuring that a request isn't captured and used at a later time. |
| 37 | + # * "iss" - the issuer name, always MessageBird. |
| 38 | + # @param [String] signature the actual signature taken from request header "MessageBird-Signature-JWT". |
| 39 | + # @param [String] url the raw url including the protocol, hostname and query string, e.g. "https://example.com/?example=42". |
| 40 | + # @param [Array] request_body the raw request body. |
| 41 | + # @return [Array] raw signature payload |
| 42 | + # @raise [ValidationError] if signature is invalid |
| 43 | + # @see https://developers.messagebird.com/docs/verify-http-requests |
| 44 | + def validate_signature(signature, url, request_body) |
| 45 | + raise ValidationError, 'Signature can not be empty' if signature.to_s.empty? |
| 46 | + raise ValidationError, 'URL can not be empty' if !@skip_url_validation && url.to_s.empty? |
| 47 | + |
| 48 | + claims = decode_signature signature |
| 49 | + validate_url(url, claims['url_hash']) unless @skip_url_validation |
| 50 | + validate_payload(request_body, claims['payload_hash']) |
| 51 | + |
| 52 | + claims |
| 53 | + end |
| 54 | + |
| 55 | + private # Applies to every method below this line |
| 56 | + |
| 57 | + def decode_signature(signature) |
| 58 | + begin |
| 59 | + claims, * = JWT.decode signature, @signature_key, true, |
| 60 | + algorithm: ALLOWED_ALGOS, |
| 61 | + iss: 'MessageBird', |
| 62 | + required_claims: %w[iss nbf exp], |
| 63 | + verify_iss: true, |
| 64 | + leeway: 1 |
| 65 | + rescue JWT::DecodeError => e |
| 66 | + raise ValidationError, e |
| 67 | + end |
| 68 | + |
| 69 | + claims |
| 70 | + end |
| 71 | + |
| 72 | + def validate_url(url, url_hash) |
| 73 | + expected_url_hash = Digest::SHA256.hexdigest url |
| 74 | + unless JWT::SecurityUtils.secure_compare(expected_url_hash, url_hash) |
| 75 | + raise ValidationError, 'invalid jwt: claim url_hash is invalid' |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + def validate_payload(body, payload_hash) |
| 80 | + if !body.to_s.empty? && !payload_hash.to_s.empty? |
| 81 | + unless JWT::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(body), payload_hash) |
| 82 | + raise ValidationError, 'invalid jwt: claim payload_hash is invalid' |
| 83 | + end |
| 84 | + elsif !body.to_s.empty? |
| 85 | + raise ValidationError, 'invalid jwt: claim payload_hash is not set but payload is present' |
| 86 | + elsif !payload_hash.to_s.empty? |
| 87 | + raise ValidationError, 'invalid jwt: claim payload_hash is set but actual payload is missing' |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | +end |
0 commit comments