Class: WebAuthn::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/webauthn/encoder.rb

Constant Summary collapse

STANDARD_ENCODING =
:base64url

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoding = STANDARD_ENCODING) ⇒ Encoder

Returns a new instance of Encoder.



16
17
18
# File 'lib/webauthn/encoder.rb', line 16

def initialize(encoding = STANDARD_ENCODING)
  @encoding = encoding
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



14
15
16
# File 'lib/webauthn/encoder.rb', line 14

def encoding
  @encoding
end

Instance Method Details

#decode(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/webauthn/encoder.rb', line 33

def decode(data)
  case encoding
  when :base64
    Base64.strict_decode64(data)
  when :base64url
    Base64.urlsafe_decode64(data)
  when nil, false
    data
  else
    raise "Unsupported or unknown encoding: #{encoding}"
  end
end

#encode(data) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/webauthn/encoder.rb', line 20

def encode(data)
  case encoding
  when :base64
    Base64.strict_encode64(data)
  when :base64url
    Base64.urlsafe_encode64(data, padding: false)
  when nil, false
    data
  else
    raise "Unsupported or unknown encoding: #{encoding}"
  end
end