-
Notifications
You must be signed in to change notification settings - Fork 60
Remove base64
dependency
#459
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
Open
santiagorodriguez96
wants to merge
6
commits into
master
Choose a base branch
from
sr--encoder-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8f68213
refactor: extract encoding logic into its own classes
santiagorodriguez96 d1973f8
refactor: move logic for loading the correct encoder to `Encoders` mo…
santiagorodriguez96 3da05e4
refactor!: move `STANDARD_ENCODING` const inside `Encorders` module
santiagorodriguez96 ec81c6a
refactor: move `WebAuthn.standard_encoder` to `lib/webauthn/encoders.rb`
santiagorodriguez96 69492d0
refactor: remove default value from `Encoders.lookup` `encoding` argu…
santiagorodriguez96 54d025c
refactor: remove `base64` dependency
santiagorodriguez96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "base64" | ||
require "webauthn/encoders" | ||
|
||
module WebAuthn | ||
def self.standard_encoder | ||
@standard_encoder ||= Encoder.new | ||
end | ||
|
||
class Encoder | ||
# https://www.w3.org/TR/webauthn-2/#base64url-encoding | ||
STANDARD_ENCODING = :base64url | ||
|
||
attr_reader :encoding | ||
extend Forwardable | ||
|
||
def initialize(encoding = STANDARD_ENCODING) | ||
@encoding = encoding | ||
end | ||
|
||
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 | ||
end | ||
def_delegators :@encoder_klass, :encode, :decode | ||
|
||
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 | ||
def initialize(encoding = Encoders::STANDARD_ENCODING) | ||
@encoder_klass = Encoders.lookup(encoding) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module WebAuthn | ||
def self.standard_encoder | ||
@standard_encoder ||= Encoders.lookup(Encoders::STANDARD_ENCODING) | ||
end | ||
|
||
module Encoders | ||
# https://www.w3.org/TR/webauthn-2/#base64url-encoding | ||
STANDARD_ENCODING = :base64url | ||
|
||
class << self | ||
def lookup(encoding) | ||
case encoding | ||
when :base64 | ||
Base64Encoder | ||
when :base64url | ||
Base64UrlEncoder | ||
when nil, false | ||
NullEncoder | ||
else | ||
raise "Unsupported or unknown encoding: #{encoding}" | ||
end | ||
end | ||
end | ||
|
||
class Base64Encoder | ||
def self.encode(data) | ||
[data].pack("m0") # Base64.strict_encode64(data) | ||
end | ||
|
||
def self.decode(data) | ||
data.unpack1("m0") # Base64.strict_decode64(data) | ||
end | ||
end | ||
|
||
class Base64UrlEncoder | ||
def self.encode(data) | ||
data = [data].pack("m0") # Base64.urlsafe_encode64(data, padding: false) | ||
data.chomp!("==") or data.chomp!("=") | ||
data.tr!("+/", "-_") | ||
data | ||
end | ||
|
||
def self.decode(data) | ||
if !data.end_with?("=") && data.length % 4 != 0 # Base64.urlsafe_decode64(data) | ||
data = data.ljust((data.length + 3) & ~3, "=") | ||
end | ||
|
||
data = data.tr("-_", "+/") | ||
data.unpack1("m0") | ||
end | ||
end | ||
|
||
class NullEncoder | ||
def self.encode(data) | ||
data | ||
end | ||
|
||
def self.decode(data) | ||
data | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that in the base64 gem, the method
tr
has a bang in the if branch, but not in the else branch 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! I'll open a separate PR to fix that 🙂