Sha256: 3bffefaf8f989b764a38b6511cede9b5caa94fff9a4c7f70e05903104960d18a

Contents?: true

Size: 860 Bytes

Versions: 1

Compression:

Stored size: 860 Bytes

Contents

require 'base64'
require 'json'
require 'openssl'
require 'date'

module Fernet
  class Verifier

    attr_reader :secret, :token, :data
    attr_writer :seconds_valid

    def initialize(secret)
      @secret        = secret
    end

    def verify_token(token)
      @token = token
      deconstruct

      custom_verification = yield self

      signatures_match? && token_recent_enough? && custom_verification
    end

    private

    def deconstruct
      @data               = JSON.parse(Base64.decode64(token))
      @received_signature = @data.delete('signature')
      @regenerated_mac    = OpenSSL::HMAC.hexdigest('sha256', JSON.dump(@data), secret)
    end

    def token_recent_enough?
      DateTime.parse(data['issued_at']) > (DateTime.now - 60)
    end

    def signatures_match?
      @regenerated_mac == @received_signature
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fernet-0.0.1 lib/fernet/verifier.rb