Skip to content

Commit

Permalink
Switch default challenge method to S256
Browse files Browse the repository at this point in the history
Updated the code challenge implementation to set "S256" as the default method instead of "Empty". Removed the unused "Empty" challenge variant and added error handling for unsupported methods. These changes enhance security by defaulting to the stronger S256 challenge method, aligning with best practices.
  • Loading branch information
eliasjpr committed Oct 12, 2024
1 parent 6b21936 commit b5cf1e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions spec/code_challenge_builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module Authly
challenge = CodeChallengeBuilder.build

it "is a valid code challenge" do
challenge.should be_a CodeChallengeBuilder::Empty
challenge.valid?("").should be_true
challenge.should be_a CodeChallengeBuilder::S256
challenge.valid?("").should be_false
end
end
end
Expand Down
19 changes: 8 additions & 11 deletions src/authly/code_challenge_builder.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "digest/sha256"

module Authly
alias CodeChallenge = CodeChallengeBuilder::Plain | CodeChallengeBuilder::S256 | CodeChallengeBuilder::Empty
alias CodeChallenge = CodeChallengeBuilder::Plain | CodeChallengeBuilder::S256

module CodeChallengeBuilder
record Plain, code : String do
Expand All @@ -16,17 +16,14 @@ module Authly
end
end

record Empty, code : String do
def valid?(code_verifier)
true
end
end

def self.build(challenge : String = "", method : String = "")
def self.build(challenge : String = "", method : String = "S256")
case method
when "plain" then Plain.new(challenge)
when "S256" then S256.new(challenge)
else Empty.new(challenge)
when "plain"
Plain.new(challenge)
when "S256"
S256.new(challenge)
else
raise ArgumentError.new("Unsupported code challenge method: #{method}. Only 'plain' and 'S256' are supported.")
end
end
end
Expand Down

0 comments on commit b5cf1e6

Please sign in to comment.