Class: Clarion::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/clarion/authenticator.rb

Defined Under Namespace

Classes: Error, InvalidKey

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authn, u2f, counter, store) ⇒ Authenticator

Returns a new instance of Authenticator.



9
10
11
12
13
14
# File 'lib/clarion/authenticator.rb', line 9

def initialize(authn, u2f, counter, store)
  @authn = authn
  @u2f = u2f
  @counter = counter
  @store = store
end

Instance Attribute Details

#authnObject (readonly)

Returns the value of attribute authn.



16
17
18
# File 'lib/clarion/authenticator.rb', line 16

def authn
  @authn
end

#counterObject (readonly)

Returns the value of attribute counter.



16
17
18
# File 'lib/clarion/authenticator.rb', line 16

def counter
  @counter
end

#storeObject (readonly)

Returns the value of attribute store.



16
17
18
# File 'lib/clarion/authenticator.rb', line 16

def store
  @store
end

#u2fObject (readonly)

Returns the value of attribute u2f.



16
17
18
# File 'lib/clarion/authenticator.rb', line 16

def u2f
  @u2f
end

Instance Method Details

#requestObject



18
19
20
# File 'lib/clarion/authenticator.rb', line 18

def request
  [u2f.app_id, u2f.authentication_requests(authn.keys.map(&:handle)), u2f.challenge]
end

#verify!(challenge, response_json) ⇒ Object



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
47
48
49
# File 'lib/clarion/authenticator.rb', line 22

def verify!(challenge, response_json)
  response = U2F::SignResponse.load_from_json(response_json)
  key = authn.key_for_handle(response.key_handle)
  unless key
    raise InvalidKey, "#{response.key_handle.inspect} is invalid token for authn #{authn.id}"
  end
  count = counter ? counter.get(key) : 0

  u2f.authenticate!(
    challenge,
    response,
    Base64.decode64(key.public_key),
    count,
  )

  unless authn.verify(key)
    raise Authenticator::InvalidKey
  end

  key.counter = response.counter
  if counter
    counter.store(key)
  end

  store.store_authn(authn)

  true
end