Skip to content

Commit

Permalink
Merge pull request #5 from ruby-passkeys/2023-06-23-refactor-add-rack…
Browse files Browse the repository at this point in the history
…-helpers-module
  • Loading branch information
tcannonfodder authored Jun 24, 2023
2 parents 602e3a3 + 80d2101 commit 8c1ef20
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
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

0 comments on commit 8c1ef20

Please sign in to comment.