Sha256: a5267b4671610dd859fe5f595787a231fb1daef99d438d24aad7fe8f0f13818e

Contents?: true

Size: 1.24 KB

Versions: 70

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'digest/md5'
require 'base64'

module Rack
  module Auth
    module Digest
      # Rack::Auth::Digest::Nonce is the default nonce generator for the
      # Rack::Auth::Digest::MD5 authentication handler.
      #
      # +private_key+ needs to set to a constant string.
      #
      # +time_limit+ can be optionally set to an integer (number of seconds),
      # to limit the validity of the generated nonces.

      class Nonce

        class << self
          attr_accessor :private_key, :time_limit
        end

        def self.parse(string)
          new(*Base64.decode64(string).split(' ', 2))
        end

        def initialize(timestamp = Time.now, given_digest = nil)
          @timestamp, @given_digest = timestamp.to_i, given_digest
        end

        def to_s
          Base64.encode64("#{@timestamp} #{digest}").strip
        end

        def digest
          ::Digest::MD5.hexdigest("#{@timestamp}:#{self.class.private_key}")
        end

        def valid?
          digest == @given_digest
        end

        def stale?
          !self.class.time_limit.nil? && (Time.now.to_i - @timestamp) > self.class.time_limit
        end

        def fresh?
          !stale?
        end

      end
    end
  end
end

Version data entries

70 entries across 55 versions & 14 rubygems

Version Path
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.5 vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.4 vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.3 vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/digest/nonce.rb
tdiary-5.1.3 vendor/bundle/ruby/2.6.0/gems/rack-2.2.2/lib/rack/auth/digest/nonce.rb
rack-2.2.3 lib/rack/auth/digest/nonce.rb
rack-2.1.4 lib/rack/auth/digest/nonce.rb
tdiary-5.1.2 vendor/bundle/ruby/2.7.0/gems/rack-2.2.2/lib/rack/auth/digest/nonce.rb