Class: YubiOATH

Inherits:
Object
  • Object
show all
Defined in:
lib/yubioath.rb,
lib/yubioath/put.rb,
lib/yubioath/list.rb,
lib/yubioath/reset.rb,
lib/yubioath/delete.rb,
lib/yubioath/select.rb,
lib/yubioath/response.rb,
lib/yubioath/calculate.rb,
lib/yubioath/calculate_all.rb

Defined Under Namespace

Classes: Calculate, CalculateAll, Delete, List, Put, Reset, Response, Select

Constant Summary collapse

AID =
[0xA0, 0x00, 0x00, 0x05, 0x27, 0x21, 0x01, 0x01]
ALGORITHMS =
{ sha1: 0x1, sha256: 0x2 }
TYPES =
{ hotp: 0x1, totp: 0x2 }
RequestFailed =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(card) ⇒ YubiOATH

Returns a new instance of YubiOATH.



12
13
14
15
# File 'lib/yubioath.rb', line 12

def initialize(card)
  @card = card
  select(AID)
end

Instance Method Details

#calculate(name:, timestamp:) ⇒ Object

Raises:



17
18
19
20
21
22
23
# File 'lib/yubioath.rb', line 17

def calculate(name:, timestamp:)
  data = Calculate::Request::Data.new(name: name, timestamp: timestamp.to_i / 30)
  request = Calculate::Request.new(data: data.to_binary_s)
  response = Response.read(@card.transmit(request.to_binary_s))
  raise RequestFailed, response unless response.success?
  Calculate::Response.read(response.data).code.to_s
end

#calculate_all(timestamp:) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
# File 'lib/yubioath.rb', line 25

def calculate_all(timestamp:)
  data = CalculateAll::Request::Data.new(timestamp: timestamp.to_i / 30)
  request = CalculateAll::Request.new(data: data.to_binary_s)
  response = Response.read(@card.transmit(request.to_binary_s))
  raise RequestFailed, response unless response.success?
  CalculateAll::Response.read(response.data)[:codes].map do |code|
    [code.name, code.code.to_s]
  end.to_h
end

#delete(name:) ⇒ Object



35
36
37
38
39
# File 'lib/yubioath.rb', line 35

def delete(name:)
  data = Delete::Request::Data.new(name: name)
  request = Delete::Request.new(data: data.to_binary_s)
  Response.read(@card.transmit(request.to_binary_s))
end

#listObject

Raises:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yubioath.rb', line 41

def list
  request = List::Request.new.to_binary_s
  response = Response.read(@card.transmit(request))
  raise RequestFailed, response unless response.success?
  List::Response.read(response.data)[:codes].map do |code|
    [code.name, {
      type: TYPES.key(code.type),
      algorithm: ALGORITHMS.key(code.algorithm),
    }]
  end.to_h
end

#put(name:, secret:, algorithm:, type:, digits:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yubioath.rb', line 53

def put(name:, secret:, algorithm:, type:, digits:)
  data = Put::Request::Data.new(
    name: name,
    type: TYPES.fetch(type),
    algorithm: ALGORITHMS.fetch(algorithm),
    digits: digits,
    secret: secret,
  )
  request = Put::Request.new(data: data.to_binary_s)
  Response.read(@card.transmit(request.to_binary_s))
end

#resetObject



65
66
67
# File 'lib/yubioath.rb', line 65

def reset
  Response.read(@card.transmit(Reset::Request.new.to_binary_s))
end