Skip to content

Enhancement encoder #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -14,3 +14,5 @@
/gemfiles/*.gemfile.lock
.byebug_history
/spec/conformance/metadata.zip
.idea/
.ruby-version
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -6,3 +6,7 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in webauthn.gemspec
gemspec

group :development, :test do
gem 'rspec'
end
131 changes: 94 additions & 37 deletions lib/webauthn/encoder.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,112 @@
# frozen_string_literal: true

require "base64"

module WebAuthn
def self.standard_encoder
@standard_encoder ||= Encoder.new
module Config
module Encoder
# Default encoding type used for WebAuthn operations
DEFAULT_ENCODING = :base64url

# Supported encoding types and their corresponding Base64 methods
ENCODINGS = {
base64: :strict,
base64url: :urlsafe
}.freeze
Comment on lines +10 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not using the values of this hash right? Why not just storing the encodings as an array?


# Error message template for unsupported encoding types
INVALID_ENCODING_ERROR = "Unsupported or unknown encoding: %s".freeze
end
Comment on lines +4 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just moving these constants inside Webauthn::Encoder?

end

class Encoder
# https://www.w3.org/TR/webauthn-2/#base64url-encoding
STANDARD_ENCODING = :base64url
# Custom error class for encoding-related errors
class EncodingError < StandardError; end

# Handles encoding and decoding of WebAuthn data
class Encoder
attr_reader :encoding

def initialize(encoding = STANDARD_ENCODING)
# Initialize encoder with specified encoding type
# @param encoding [Symbol] the encoding type to use (:base64url by default)
def initialize(encoding = Config::Encoder::DEFAULT_ENCODING)
@encoding = encoding
end

# Encode data using the specified encoding type
# @param data [String] the data to encode
# @return [String] encoded data
def encode(data)
case encoding
when :base64
[data].pack("m0") # Base64.strict_encode64(data)
when :base64url
data = [data].pack("m0") # Base64.urlsafe_encode64(data, padding: false)
data.chomp!("==") or data.chomp!("=")
data.tr!("+/", "-_")
data
when nil, false
data
else
raise "Unsupported or unknown encoding: #{encoding}"
end
return data if skip_encoding?

validate_encoding!
send("encode_#{encoding}", data)
end

# Decode data using the specified encoding type
# @param data [String] the data to decode
# @return [String] decoded data
def decode(data)
case encoding
when :base64
data.unpack1("m0") # Base64.strict_decode64(data)
when :base64url
if !data.end_with?("=") && data.length % 4 != 0 # Base64.urlsafe_decode64(data)
data = data.ljust((data.length + 3) & ~3, "=")
data.tr!("-_", "+/")
else
data = data.tr("-_", "+/")
end
data.unpack1("m0")
when nil, false
data
else
raise "Unsupported or unknown encoding: #{encoding}"
end
return data if skip_encoding?

validate_encoding!
send("decode_#{encoding}", data)
end

private

# Check if encoding should be skipped
# @return [Boolean] true if encoding is nil or false
def skip_encoding?
encoding.nil? || encoding == false
end

# Validate that the current encoding type is supported
# @raise [EncodingError] if encoding type is not supported
def validate_encoding!
return if Config::Encoder::ENCODINGS.key?(encoding)

raise EncodingError, Config::Encoder::INVALID_ENCODING_ERROR % encoding
end

# Encode data using standard Base64 encoding
# @param data [String] the data to encode
# @return [String] Base64 encoded data
def encode_base64(data)
Base64.strict_encode64(data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end

# Encode data using URL-safe Base64 encoding without padding
# @param data [String] the data to encode
# @return [String] URL-safe Base64 encoded data
def encode_base64url(data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to have these methods public in some way, so that they can be used in our specs to encode/decode data and we can remove the base64 dependency altogether – see this PR.

Base64.urlsafe_encode64(data, padding: false)
end

# Decode standard Base64 encoded data
# @param data [String] the Base64 encoded data
# @return [String] decoded data
def decode_base64(data)
Base64.strict_decode64(data)
end

# Decode URL-safe Base64 encoded data
# @param data [String] the URL-safe Base64 encoded data
# @return [String] decoded data
def decode_base64url(data)
Base64.urlsafe_decode64(ensure_padding(data))
end

# Ensure proper Base64 padding
# @param data [String] the data to pad
# @return [String] properly padded data
def ensure_padding(data)
return data if data.end_with?("=")

data.ljust((data.length + 3) & ~3, "=")
end
end

# Factory method to create a standard encoder instance
# @return [Encoder] a new encoder instance with default settings
def self.standard_encoder
@standard_encoder ||= Encoder.new
end
end
2 changes: 1 addition & 1 deletion lib/webauthn/relying_party.rb
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ def self.if_pss_supported(algorithm)

def initialize(
algorithms: DEFAULT_ALGORITHMS.dup,
encoding: WebAuthn::Encoder::STANDARD_ENCODING,
encoding: Config::Encoder::DEFAULT_ENCODING,
origin: nil,
id: nil,
name: nil,