Class: TOTP::CLI::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/totp/cli/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Token

Returns a new instance of Token.



42
43
44
45
46
# File 'lib/totp/cli/token.rb', line 42

def initialize(options = {})
  @id     = options[:id]
  @label  = options[:label]
  @secret = options[:secret]
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



40
41
42
# File 'lib/totp/cli/token.rb', line 40

def id
  @id
end

#labelObject

Returns the value of attribute label.



40
41
42
# File 'lib/totp/cli/token.rb', line 40

def label
  @label
end

#secretObject

Returns the value of attribute secret.



40
41
42
# File 'lib/totp/cli/token.rb', line 40

def secret
  @secret
end

Class Method Details

.allObject



8
9
10
# File 'lib/totp/cli/token.rb', line 8

def all
  store.read_all
end

.create(label, secret) ⇒ Object



28
29
30
31
32
# File 'lib/totp/cli/token.rb', line 28

def create(label, secret)
  store.save(label, secret)

  find_by_label(label)
end

.find_by_id(id) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/totp/cli/token.rb', line 12

def find_by_id(id)
  all.each do |token|
    return token if token.id == id
  end

  nil
end

.find_by_label(label) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/totp/cli/token.rb', line 20

def find_by_label(label)
  all.each do |token|
    return token if token.label == label
  end

  nil
end

.storeObject



34
35
36
37
# File 'lib/totp/cli/token.rb', line 34

def store
  filepath = File.join(Etc.getpwuid.dir, ".totp-cli")
  Store.new(filepath)
end

Instance Method Details

#delete!Object



58
59
60
# File 'lib/totp/cli/token.rb', line 58

def delete!
  self.class.store.remove(label)
end

#nowObject



48
49
50
51
52
53
54
55
56
# File 'lib/totp/cli/token.rb', line 48

def now
  otp = totp.now.to_s

  while otp.length < 6
    otp = "0" + otp
  end

  otp
end