Module: Duo

Extended by:
Duo
Included in:
Duo
Defined in:
lib/duo.rb,
lib/duo/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#sign_request(skey, ikey, username) ⇒ Object

Sign a Duo request with skey, ikey, and the local username



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/duo.rb', line 7

def sign_request(skey, ikey, username)
  exp = Time.now.to_i + 300

  val = [username, ikey, exp].join('|')
  
  b64 = Base64.encode64(val).strip
  cookie = "TX|#{b64}"

  sig = hmac_sha1(skey, cookie)

  [cookie, sig].join('|')
end

#verify_response(skey, sig_response) ⇒ Object

Check a response from Duo with the skey.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/duo.rb', line 21

def verify_response(skey, sig_response)
  ts = Time.now.to_i
  u_prefix, u_b64, u_sig = sig_response.to_s.split('|')
  
  sig = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'),
    skey, [u_prefix, u_b64].join('|'))
    
  return nil if hmac_sha1(skey, sig) != hmac_sha1(skey, u_sig)
  
  return nil if u_prefix != 'AUTH'
  
  user, ikey, exp = Base64.decode64(u_b64).to_s.split('|')
  
  return nil if ts >= exp.to_i
  
  return user
end