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
classiccms-0.7.0 vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
rack-1.2.8 lib/rack/auth/digest/nonce.rb
rack-1.1.6 lib/rack/auth/digest/nonce.rb
rack-1.2.7 lib/rack/auth/digest/nonce.rb
rack-1.1.5 lib/rack/auth/digest/nonce.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
rack-1.2.6 lib/rack/auth/digest/nonce.rb
rack-1.1.4 lib/rack/auth/digest/nonce.rb
challah-0.9.0 vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
classiccms-0.6.9 vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
jquery_regex-0.0.3 vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
jquery_regex-0.0.1 vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/rack-1.4.1/lib/rack/auth/digest/nonce.rb