Module: OTP::URI

Defined in:
lib/otp/uri.rb

Constant Summary collapse

SCHEME =
"otpauth"

Class Method Summary collapse

Class Method Details

.format(otp) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/otp/uri.rb', line 25

def format(otp)
  raise "secret must be set" if otp.secret.nil?
  raise "accountname must be set" if otp.accountname.nil?
  typename = otp.class.name.split("::")[-1].downcase
  label = otp.accountname.dup
  label.prepend("#{otp.issuer}:") if otp.issuer
  return "%s://%s/%s?%s" % [
    SCHEME,
    ::URI.encode(typename),
    ::URI.encode(label),
    ::URI.encode_www_form(otp.uri_params)
  ]
end

.parse(uri_string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/otp/uri.rb', line 9

def parse(uri_string)
  uri = ::URI.parse(uri_string)
  if uri.scheme.downcase != SCHEME
    raise "URI scheme not match: #{uri.scheme}"
  end
  otp = type_to_class(uri).new
  unless m = %r{/(?:([^:]*): *)?(.+)}.match(::URI.decode(uri.path))
    raise "account name must be present: #{uri_string}"
  end
  otp.issuer = m[1] if m[1]
  otp.accountname = m[2]
  query = Hash[::URI.decode_www_form(uri.query)]
  otp.extract_uri_params(query)
  return otp
end

.type_to_class(uri) ⇒ Object



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

def type_to_class(uri)
  klass = OTP.const_get(uri.host.upcase)
  raise unless klass.is_a?(Class)
  raise unless klass.ancestors.include?(OTP::Base)
  return klass
rescue
  raise "unknown OTP type: #{uri.host}"
end