Skip to content
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

Add Warden::WebAuthn::RackHelpers; prepend to StrategyHelpers #5

Merged
merged 5 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [Unreleased]

- Refactor `relying_party_key` into `Warden::WebAuthn::RackHelpers`
- https://github.com/ruby-passkeys/warden-webauthn/issues/4

## [0.1.0] - 2023-02-04

- Initial release
1 change: 1 addition & 0 deletions lib/warden/webauthn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "webauthn/version"
require_relative "webauthn/error_key_finder"
require_relative "webauthn/rack_helpers"
require_relative "webauthn/strategy_helpers"
require_relative "webauthn/strategy"
require_relative "webauthn/authentication_initiation_helpers"
Expand Down
17 changes: 17 additions & 0 deletions lib/warden/webauthn/rack_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Warden
module WebAuthn
# Helpers that can be mixed in to any Rack middleware or application, in order
# to setup the environment for `Warden::WebAuthn`, such as the Relying Party
module RackHelpers
def relying_party_key
"warden.webauthn.relying_party"
end

def set_relying_party_in_request_env
request.env[relying_party_key] = relying_party
end
end
end
end
1 change: 1 addition & 0 deletions lib/warden/webauthn/strategy_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module WebAuthn
# Helpers that can be mixed in to any WebAuthn-related code, such as custom strategies or
# an app's authentication flow
module StrategyHelpers
prepend RackHelpers
class NoStoredCredentialFound < StandardError; end

# rubocop:disable Metrics/MethodLength
Expand Down
53 changes: 53 additions & 0 deletions test/warden/test_rack_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require "test_helper"

class Warden::TestRackHelpers < Minitest::Test
class TestClass
include Warden::WebAuthn::RackHelpers

attr_accessor :request

def initialize
self.request = Rack::Request.new({})
end
end

class CustomizedClass
include Warden::WebAuthn::RackHelpers

attr_accessor :request

def initialize
self.request = Rack::Request.new({})
end

def relying_party_key
"custom_relying_party"
end

def relying_party
"dummy_relying_party_value"
end
end

def test_default_keys
assert_equal "warden.webauthn.relying_party", TestClass.new.relying_party_key
end

def test_custom_keys
assert_equal "custom_relying_party", CustomizedClass.new.relying_party_key
end

def test_raises_name_error_if_no_relying_party_method
assert_raises NameError do
TestClass.new.set_relying_party_in_request_env
end
end

def test_raises_uses_defined_relying_party_method
instance = CustomizedClass.new
instance.set_relying_party_in_request_env
assert_equal "dummy_relying_party_value", instance.request.env["custom_relying_party"]
end
end