Sha256: 8293310284b370dd8da9ae905936b1d95da190d9b054ee0d4105be36134088b7

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

require 'openssl'

module OmniAuth
  module Facebook
    class SignedRequest
      class UnknownSignatureAlgorithmError < NotImplementedError; end
      SUPPORTED_ALGORITHM = 'HMAC-SHA256'

      attr_reader :value, :secret

      def self.parse(value, secret)
        new(value, secret).payload
      end

      def initialize(value, secret)
        @value = value
        @secret = secret
      end

      def payload
        @payload ||= parse_signed_request
      end

      private

      def parse_signed_request
        signature, encoded_payload = value.split('.')
        return if signature.nil?

        decoded_hex_signature = base64_decode_url(signature)
        decoded_payload = MultiJson.decode(base64_decode_url(encoded_payload))

        unless decoded_payload['algorithm'] == SUPPORTED_ALGORITHM
          raise UnknownSignatureAlgorithmError, "unknown algorithm: #{decoded_payload['algorithm']}"
        end

        if valid_signature?(decoded_hex_signature, encoded_payload)
          decoded_payload
        end
      end

      def valid_signature?(signature, payload, algorithm = OpenSSL::Digest::SHA256.new)
        OpenSSL::HMAC.digest(algorithm, secret, payload) == signature
      end

      def base64_decode_url(value)
        value += '=' * (4 - value.size.modulo(4))
        Base64.decode64(value.tr('-_', '+/'))
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
omniauth-facebook-9.0.0 lib/omniauth/facebook/signed_request.rb
omniauth-facebook-8.0.0 lib/omniauth/facebook/signed_request.rb
omniauth-facebook-7.0.0 lib/omniauth/facebook/signed_request.rb
omniauth-facebook-6.0.0 lib/omniauth/facebook/signed_request.rb
omniauth-facebook-5.0.0 lib/omniauth/facebook/signed_request.rb
omniauth-facebook-4.0.0 lib/omniauth/facebook/signed_request.rb
omniauth-facebook-4.0.0.rc1 lib/omniauth/facebook/signed_request.rb
omniauth-facebook-3.0.0 lib/omniauth/facebook/signed_request.rb