Class: GAVerify::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/ga_verify/handler.rb

Direct Known Subclasses

Server

Instance Method Summary collapse

Constructor Details

#initializeHandler

Returns a new instance of Handler.



8
9
10
11
# File 'lib/ga_verify/handler.rb', line 8

def initialize
  @last_seen   = Hash.new(0)
  @used_tokens = Hash.new{Hash.new(0)}
end

Instance Method Details

#check_user(user, token) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ga_verify/handler.rb', line 13

def check_user user, token
  unless user =~ /^[a-z]+$/
    return GAVerify::Result::BAD_USER
  end
  unless File.exists? "/home/#{user}"
    return GAVerify::Result::BAD_USER
  end
  path = GAVerify::Paths.config_path(user)
  unless File.exists? path
    return GAVerify::Result::NO_GOOGLE_AUTH
  end
  now = Time.now.to_i
  # Max one login every 15s
  if @last_seen[user] >= now - 15
    return GAVerify::Result::BAD_TOKEN
  end
  @last_seen[user] = now

  secret = File.open(path, 'r').first.strip
  totp = ROTP::TOTP.new(secret)

  # Allow +- 1 token
  times = [now - 30, now, now + 30]
  if times.any?{|time| totp.verify(token, time)}
    # disallow token re-use within 10 minutes
    if @used_tokens[user][token] < now - 600
      @used_tokens[user][token] = now
      # Cleanup
      @used_tokens[user].reject!{|k,v| used_tokens[user][token] < now - 600}
      return GAVerify::Result::SUCCESS
    end
  end
  return GAVerify::Result::BAD_TOKEN
end