Repository navigation
feat: add a cache challenge store #152
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c9b8aa1
feat: add a cache challenge store for API clients without a session
RenzoMinelli ed47062
docs(`CHANGELOG`): link API client entries to #152
RenzoMinelli ccb4ea8
docs: mention the cache challenge store in the install template
RenzoMinelli 0e9c958
docs: say the cache challenge store needs a cache shared by all servers
RenzoMinelli df689de
refactor: let the cache challenge store read the credential from the …
RenzoMinelli 3a05cec
refactor: read the cache store challenge with `WebAuthn::ClientData`
RenzoMinelli f338d33
refactor: leave API client support for a follow-up PR
RenzoMinelli 9e0433c
refactor: memoize the cache store challenge, including nil
RenzoMinelli 7604b25
refactor: key cache store entries by a digest of the challenge
RenzoMinelli 34ddab9
refactor: split the cache store consume checks into guard clauses
RenzoMinelli f8a3318
test: check pending? after consume and after expiry
RenzoMinelli 292bd3a
test: remove the cache challenge store request spec
RenzoMinelli 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Devise | ||
| module Webauthn | ||
| module ChallengeStores | ||
| class Cache | ||
| cattr_accessor :cache | ||
| cattr_accessor :expires_in, default: 5.minutes | ||
|
|
||
| def initialize(request) | ||
| @request = request | ||
| end | ||
|
|
||
| def write(purpose, challenge) | ||
| store.write(key(purpose, challenge), true, expires_in: expires_in) | ||
| end | ||
|
|
||
| def pending?(purpose) | ||
| challenge.present? && store.exist?(key(purpose, challenge)) | ||
| end | ||
|
|
||
| # `exist?` honours expiry, which MemoryStore#delete does not. `delete` decides which concurrent request wins. | ||
| def consume(purpose) | ||
| return if challenge.blank? | ||
| return unless store.exist?(key(purpose, challenge)) | ||
| return unless store.delete(key(purpose, challenge)) | ||
|
|
||
| challenge | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def store | ||
| cache || Rails.cache | ||
| end | ||
|
|
||
| def key(purpose, challenge) | ||
| "devise_webauthn:challenge:#{purpose}:#{Digest::SHA256.hexdigest(challenge)}" | ||
| end | ||
|
|
||
| def challenge | ||
| return @challenge if defined?(@challenge) | ||
|
|
||
| @challenge = begin | ||
| client_data_json = credential.dig("response", "clientDataJSON") if credential.is_a?(Hash) | ||
| client_data = WebAuthn::ClientData.new(encoder.decode(client_data_json)) if client_data_json.is_a?(String) | ||
| encoder.encode(client_data.challenge) if client_data | ||
| rescue JSON::ParserError, ArgumentError, TypeError, EncodingError, NoMethodError | ||
| nil | ||
| end | ||
| end | ||
|
|
||
| def credential | ||
| @credential ||= JSON.parse(@request.params[:public_key_credential]) | ||
| end | ||
|
santiagorodriguez96 marked this conversation as resolved.
|
||
|
|
||
| def encoder | ||
| WebAuthn.configuration.encoder | ||
| end | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
| require "webauthn/fake_client" | ||
|
|
||
| RSpec.describe Devise::Webauthn::ChallengeStores::Cache do | ||
| include ActiveSupport::Testing::TimeHelpers | ||
|
|
||
| let(:store) { described_class.new(instance_double(ActionDispatch::Request, params: { public_key_credential: credential })) } | ||
| let(:client) { WebAuthn::FakeClient.new(WebAuthn.configuration.allowed_origins.first) } | ||
| let(:challenge) { WebAuthn::Credential.options_for_get.challenge } | ||
| let(:credential) do | ||
| client.create(challenge: WebAuthn::Credential.options_for_get.challenge) | ||
| client.get(challenge: challenge).to_json | ||
| end | ||
|
|
||
| around do |example| | ||
| original_cache = described_class.cache | ||
| described_class.cache = ActiveSupport::Cache::MemoryStore.new | ||
| example.run | ||
| ensure | ||
| described_class.cache = original_cache | ||
| end | ||
|
|
||
| # rubocop:disable RSpec/MultipleExpectations | ||
| it "finds the challenge through the credential's client data and consumes it once" do | ||
| store.write(:passkey_authentication, challenge) | ||
|
|
||
| expect(store.pending?(:passkey_authentication)).to be(true) | ||
| expect(store.consume(:passkey_authentication)).to eq(challenge) | ||
| expect(store.pending?(:passkey_authentication)).to be(false) | ||
| expect(store.consume(:passkey_authentication)).to be_nil | ||
| end | ||
|
santiagorodriguez96 marked this conversation as resolved.
|
||
| # rubocop:enable RSpec/MultipleExpectations | ||
|
|
||
| it "keys the cache entry by a digest of the challenge" do | ||
| allow(described_class.cache).to receive(:write).and_call_original | ||
|
|
||
| store.write(:passkey_authentication, challenge) | ||
|
|
||
| expect(described_class.cache).to have_received(:write) | ||
| .with("devise_webauthn:challenge:passkey_authentication:#{Digest::SHA256.hexdigest(challenge)}", true, | ||
| expires_in: described_class.expires_in) | ||
| end | ||
|
|
||
| it "keeps challenges for different purposes apart" do | ||
| store.write(:two_factor_authentication, challenge) | ||
|
|
||
| expect(store.pending?(:passkey_authentication)).to be(false) | ||
| end | ||
|
|
||
| it "expires challenges" do | ||
| store.write(:passkey_authentication, challenge) | ||
|
|
||
| travel_to(described_class.expires_in.from_now + 1.second) do | ||
| expect(store.pending?(:passkey_authentication)).to be(false) | ||
| expect(store.consume(:passkey_authentication)).to be_nil | ||
|
santiagorodriguez96 marked this conversation as resolved.
|
||
| end | ||
| end | ||
|
|
||
| context "when credential is malformed" do | ||
| let(:credential) { "{}" } | ||
|
|
||
| it "ignores it" do | ||
| expect(store.pending?(:passkey_authentication)).to be(false) | ||
| end | ||
| end | ||
|
|
||
| context "when the client data has no challenge" do | ||
| let(:credential) { { response: { clientDataJSON: WebAuthn.configuration.encoder.encode("{}") } }.to_json } | ||
|
|
||
| it "ignores it" do | ||
| expect(store.pending?(:passkey_authentication)).to be(false) | ||
| end | ||
| end | ||
| end | ||
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 think I'd like to give more thought to this interface, but we can leave for a follow up PR 🙂
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.
Sounds good, let's do that in a follow-up.