class TalcOTP

Public Class Methods

new(file) click to toggle source
# File lib/talcotp.rb, line 5
def initialize file
  file = File.file?(file) ? File.read(file) : ''

  begin
    @accounts = JSON.parse(file)
  rescue JSON::ParserError
    @accounts = {}
  end
end

Public Instance Methods

[](account_name) click to toggle source
# File lib/talcotp.rb, line 26
def [](account_name)
  account = @accounts.find { |account| account["label"] == account_name }
  return auth_code(account)
end
all_codes_string() click to toggle source
# File lib/talcotp.rb, line 15
def all_codes_string
  output = ''
  @accounts.each do |account|
    account_auth_code = auth_code(account)

    output += "#{account['label']}: #{account_auth_code}\n"
  end

  output
end

Private Instance Methods

auth_code(account) click to toggle source
# File lib/talcotp.rb, line 33
def auth_code(account)
  account_settings = {
    interval: account['period'],
    digits: account['digits'],
    digest: account['algorithm']
  }
  return ROTP::TOTP.new(account['secret'], account_settings).now
end