lib/otp/base.rb in otp-0.0.2 vs lib/otp/base.rb in otp-0.0.3
- old
+ new
@@ -1,20 +1,25 @@
require "otp/utils"
require "otp/base32"
+require "otp/uri"
module OTP
class Base
include OTP::Utils
+ DEFAULT_DIGITS = 6
+ DEFAULT_ALGORITHM = "SHA1"
+
attr_accessor :secret
attr_accessor :algorithm
attr_accessor :digits
+ attr_accessor :issuer, :accountname
- def initialize(secret=nil, algorithm="SHA1", digits=6)
+ def initialize(secret=nil, algorithm=nil, digits=nil)
self.secret = secret
- self.algorithm = algorithm
- self.digits = digits
+ self.algorithm = algorithm || DEFAULT_ALGORITHM
+ self.digits = digits || DEFAULT_DIGITS
end
def new_secret(num_bytes=10)
s = (0...num_bytes).map{ Random.rand(256).chr }.join
self.secret = OTP::Base32.encode(s)
@@ -37,8 +42,22 @@
end
def verify(otp)
return false if otp.nil? || otp.empty?
return compare(password, otp)
+ end
+
+ ## URI related methods
+
+ def to_uri
+ OTP::URI.format(self)
+ end
+
+ def type_specific_uri_params
+ raise NotImplementedError
+ end
+
+ def extract_type_specific_uri_param
+ raise NotImplementedError
end
end
end