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

Inherits:
Object
  • Object
show all
Defined in:
rack/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

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

Returns a new instance of Nonce



24
25
26
# File 'rack/rack/auth/digest/nonce.rb', line 24

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

Class Attribute Details

+ (void) private_key

Returns the value of attribute private_key



17
18
19
# File 'rack/rack/auth/digest/nonce.rb', line 17

def private_key
  @private_key
end

+ (void) time_limit

Returns the value of attribute time_limit



17
18
19
# File 'rack/rack/auth/digest/nonce.rb', line 17

def time_limit
  @time_limit
end

Class Method Details

+ (void) parse(string)



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

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

Instance Method Details

- (void) digest



32
33
34
# File 'rack/rack/auth/digest/nonce.rb', line 32

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

- (Boolean) fresh?

Returns:

  • (Boolean)


44
45
46
# File 'rack/rack/auth/digest/nonce.rb', line 44

def fresh?
  !stale?
end

- (Boolean) stale?

Returns:

  • (Boolean)


40
41
42
# File 'rack/rack/auth/digest/nonce.rb', line 40

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

- (void) to_s



28
29
30
# File 'rack/rack/auth/digest/nonce.rb', line 28

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

- (Boolean) valid?

Returns:

  • (Boolean)


36
37
38
# File 'rack/rack/auth/digest/nonce.rb', line 36

def valid?
  digest == @given_digest
end