Class: OTP::Base

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/otp/base.rb

Direct Known Subclasses

HOTP, TOTP

Constant Summary collapse

DEFAULT_DIGITS =
6
DEFAULT_ALGORITHM =
"SHA1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret = nil, algorithm = nil, digits = nil) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
# File 'lib/otp/base.rb', line 15

def initialize(secret=nil, algorithm=nil, digits=nil)
  self.secret = secret
  self.algorithm = algorithm || DEFAULT_ALGORITHM
  self.digits = digits || DEFAULT_DIGITS
end

Instance Attribute Details

#accountnameObject

Returns the value of attribute accountname.



13
14
15
# File 'lib/otp/base.rb', line 13

def accountname
  @accountname
end

#algorithmObject

Returns the value of attribute algorithm.



12
13
14
# File 'lib/otp/base.rb', line 12

def algorithm
  @algorithm
end

#digitsObject

Returns the value of attribute digits.



12
13
14
# File 'lib/otp/base.rb', line 12

def digits
  @digits
end

#issuerObject

Returns the value of attribute issuer.



13
14
15
# File 'lib/otp/base.rb', line 13

def issuer
  @issuer
end

#secretObject

Returns the value of attribute secret.



12
13
14
# File 'lib/otp/base.rb', line 12

def secret
  @secret
end

Instance Method Details

#extract_uri_params(params) ⇒ Object



61
62
63
64
65
66
# File 'lib/otp/base.rb', line 61

def extract_uri_params(params)
  self.secret = params["secret"]
  self.issuer = issuer || params["issuer"]
  self.algorithm = params["algorithm"] || algorithm
  self.digits = (params["digits"] || digits).to_i
end

#moving_factorObject

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/otp/base.rb', line 33

def moving_factor
  raise NotImplementedError
end

#new_secret(num_bytes = 10) ⇒ Object



21
22
23
# File 'lib/otp/base.rb', line 21

def new_secret(num_bytes=10)
  self.raw_secret = OpenSSL::Random.random_bytes(num_bytes)
end

#password(generation = 0) ⇒ Object



37
38
39
# File 'lib/otp/base.rb', line 37

def password(generation=0)
  return otp(algorithm, raw_secret, moving_factor+generation, digits)
end

#raw_secretObject



29
30
31
# File 'lib/otp/base.rb', line 29

def raw_secret
  return OTP::Base32.decode(secret)
end

#raw_secret=(bytes) ⇒ Object



25
26
27
# File 'lib/otp/base.rb', line 25

def raw_secret=(bytes)
  self.secret = OTP::Base32.encode(bytes)
end

#to_uriObject



48
49
50
# File 'lib/otp/base.rb', line 48

def to_uri
  return OTP::URI.format(self)
end

#uri_paramsObject



52
53
54
55
56
57
58
59
# File 'lib/otp/base.rb', line 52

def uri_params
  params = {}
  params[:secret] = secret
  params[:issuer] = issuer if issuer
  params[:algorithm] = algorithm if algorithm != DEFAULT_ALGORITHM
  params[:digits] = digits if digits != DEFAULT_DIGITS
  return params
end

#verify(given_pw, last: 0, post: 0) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
# File 'lib/otp/base.rb', line 41

def verify(given_pw, last:0, post:0)
  raise ArgumentError, "last must be greater than or equal to 0" if last < 0
  raise ArgumentError, "post must be greater than or equal to 0" if post < 0
  return false if given_pw.nil? || given_pw.empty?
  return (-last..post).any?{|i| otp_compare(password(i), given_pw) }
end