dbe109f29ac523cbe7f059c29f426a00e12924d8
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / vendor / rack-1.0 / rack / auth / digest / nonce.rb
1 require 'digest/md5'
2
3 module Rack
4 module Auth
5 module Digest
6 # Rack::Auth::Digest::Nonce is the default nonce generator for the
7 # Rack::Auth::Digest::MD5 authentication handler.
8 #
9 # +private_key+ needs to set to a constant string.
10 #
11 # +time_limit+ can be optionally set to an integer (number of seconds),
12 # to limit the validity of the generated nonces.
13
14 class Nonce
15
16 class << self
17 attr_accessor :private_key, :time_limit
18 end
19
20 def self.parse(string)
21 new(*string.unpack("m*").first.split(' ', 2))
22 end
23
24 def initialize(timestamp = Time.now, given_digest = nil)
25 @timestamp, @given_digest = timestamp.to_i, given_digest
26 end
27
28 def to_s
29 [([ @timestamp, digest ] * ' ')].pack("m*").strip
30 end
31
32 def digest
33 ::Digest::MD5.hexdigest([ @timestamp, self.class.private_key ] * ':')
34 end
35
36 def valid?
37 digest == @given_digest
38 end
39
40 def stale?
41 !self.class.time_limit.nil? && (@timestamp - Time.now.to_i) < self.class.time_limit
42 end
43
44 def fresh?
45 !stale?
46 end
47
48 end
49 end
50 end
51 end