Sha256: 9a5c45bfa131d943c25148fb42e56f74115ba56227586414a11efe74c0a088af

Contents?: true

Size: 1.19 KB

Versions: 237

Compression:

Stored size: 1.19 KB

Contents

require 'digest/md5'

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(*string.unpack("m*").first.split(' ', 2))
        end

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

        def to_s
          [([ @timestamp, digest ] * ' ')].pack("m*").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? && (@timestamp - Time.now.to_i) < self.class.time_limit
        end

        def fresh?
          !stale?
        end

      end
    end
  end
end

Version data entries

237 entries across 215 versions & 41 rubygems

Version Path
middleman-0.12.1 lib/middleman/vendor/gems/gems/rack-1.0.1/lib/rack/auth/digest/nonce.rb
middleman-0.12.0.pre3 lib/middleman/vendor/gems/gems/rack-1.0.1/lib/rack/auth/digest/nonce.rb
middleman-0.12.0.pre2 lib/middleman/vendor/gems/gems/rack-1.0.1/lib/rack/auth/digest/nonce.rb
colouringcode-passenger-0.1 vendor/rack-1.0.0-git/lib/rack/auth/digest/nonce.rb
middleman-0.12.0.pre lib/middleman/vendor/gems/gems/rack-1.0.1/lib/rack/auth/digest/nonce.rb
rango-0.0.6 vendor/rack/lib/rack/auth/digest/nonce.rb
rango-0.1.pre vendor/rack/lib/rack/auth/digest/nonce.rb
passenger-2.2.7 vendor/rack-1.0.0-git/lib/rack/auth/digest/nonce.rb
passenger-2.2.6 vendor/rack-1.0.0-git/lib/rack/auth/digest/nonce.rb
merb-core-1.1.0.pre spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/auth/digest/nonce.rb
middleman-0.10.17 vendor/gems/gems/rack-1.0.1/lib/rack/auth/digest/nonce.rb
middleman-0.10.16 vendor/gems/gems/rack-1.0.1/lib/rack/auth/digest/nonce.rb
middleman-0.10.15 vendor/gems/gems/rack-1.0.1/lib/rack/auth/digest/nonce.rb
rackjour-0.1.8 vendor/gems/gems/rack-1.0.1/lib/rack/auth/digest/nonce.rb
middleman-0.10.14 vendor/gems/gems/rack-1.0.1/lib/rack/auth/digest/nonce.rb
rack-1.0.1 lib/rack/auth/digest/nonce.rb
hiraku-rack-1.0.0.1 lib/rack/auth/digest/nonce.rb
passenger-2.2.5 vendor/rack-1.0.0-git/lib/rack/auth/digest/nonce.rb
webroar-0.2.2 src/admin_panel/vendor/rails/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/nonce.rb
passenger-2.2.3 vendor/rack-1.0.0-git/lib/rack/auth/digest/nonce.rb