Class: Rack::Auth::Digest::Nonce

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/auth/digest/nonce.rb

Overview

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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp = Time.now, given_digest = nil) ⇒ Nonce


26
27
28
# File 'lib/rack/auth/digest/nonce.rb', line 26

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

Class Attribute Details

.private_keyObject

Returns the value of attribute private_key


19
20
21
# File 'lib/rack/auth/digest/nonce.rb', line 19

def private_key
  @private_key
end

.time_limitObject

Returns the value of attribute time_limit


19
20
21
# File 'lib/rack/auth/digest/nonce.rb', line 19

def time_limit
  @time_limit
end

Class Method Details

.parse(string) ⇒ Object


22
23
24
# File 'lib/rack/auth/digest/nonce.rb', line 22

def self.parse(string)
  new(*string.unpack("m*").first.split(' ', 2))
end

Instance Method Details

#digestObject


34
35
36
# File 'lib/rack/auth/digest/nonce.rb', line 34

def digest
  ::Digest::MD5.hexdigest([ @timestamp, self.class.private_key ] * ':')
end

#fresh?Boolean


46
47
48
# File 'lib/rack/auth/digest/nonce.rb', line 46

def fresh?
  !stale?
end

#stale?Boolean


42
43
44
# File 'lib/rack/auth/digest/nonce.rb', line 42

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

#to_sObject


30
31
32
# File 'lib/rack/auth/digest/nonce.rb', line 30

def to_s
  [([ @timestamp, digest ] * ' ')].pack("m*").strip
end

#valid?Boolean


38
39
40
# File 'lib/rack/auth/digest/nonce.rb', line 38

def valid?
  digest == @given_digest
end