Sha256: f6a9eda66f399e4dd5afeeffb4c478270223763f4ae4e7c0aa861de45b598c73

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

#encoding UTF-8
require 'base64'
require 'openssl'
require 'date'
require_relative 'errors'

module Fernet
  # Public: verifies Fernet Tokens
  class Verifier
    class UnknownTokenVersion < Fernet::Error; end

    attr_reader :token, :enforce_ttl
    attr_accessor :ttl

    # Internal: initializes a Verifier
    #
    # opts - a hash containing
    # * secret      - the secret used to create the token (required)
    # * token       - the fernet token string (required)
    # * enforce_ttl - whether to enforce TTL, defaults to Configuration.enforce_ttl
    # * ttl         - number of seconds the token is valid
    def initialize(opts = {})
      @enforce_ttl = opts.has_key?(:enforce_ttl) ? opts[:enforce_ttl] : Configuration.enforce_ttl
      @opts = opts
      create_token!
    end

    # Public: whether the verifier is valid. A verifier is valid if it's token
    #   is valid.
    #
    # Returns a boolean set to true if the token is valid, false otherwise
    def valid?
      @token.valid?
    end

    # Public: Returns the token's message
    def message
      @token.message
    end

    # Deprecated: returns the token's message
    def data
      puts "[WARNING] data is deprecated. Use message instead"
      message
    end

    # Public: String representation of this verifier, masks the secret to avoid
    #   leaks
    def inspect
      "#<Fernet::Verifier @secret=[masked] @token=#{@token} @message=#{@message.inspect} @ttl=#{@ttl} @enforce_ttl=#{@enforce_ttl}>"
    end
    alias to_s inspect

    # Public: sets the enforce_ttl configuration
    #
    # * val - whether to enforce TTL, defaults to Configuration.enforce_ttl
    def enforce_ttl=(val)
      @enforce_ttl = val
      create_token!
    end

  private
    def create_token!
      @token = Token.new(@opts.fetch(:token),
                           secret: @opts.fetch(:secret),
                           enforce_ttl: enforce_ttl,
                           ttl: @opts[:ttl],
                           now: @opts[:now])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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