Skip to content

Fix UnknownAuthStrategy raised when custom auth strategy inherits from Grape::Middleware::Auth::Base#2674

Merged
ericproulx merged 1 commit intoruby-grape:masterfrom
dblock:fix/auth-base-subclass-issue-2669
Apr 6, 2026
Merged

Fix UnknownAuthStrategy raised when custom auth strategy inherits from Grape::Middleware::Auth::Base#2674
ericproulx merged 1 commit intoruby-grape:masterfrom
dblock:fix/auth-base-subclass-issue-2669

Conversation

@dblock
Copy link
Copy Markdown
Member

@dblock dblock commented Apr 5, 2026

Summary

Fixes #2669.

Problem

PR #2563 moved the strategy lookup in Grape::Middleware::Auth::Base#initialize from request-time to initialization-time. However, when a custom auth strategy class inherits from Grape::Middleware::Auth::Base (as documented in the README and explicitly supported via Grape::Middleware::Auth::Strategies.add), calling StrategyInfo#create instantiates that class via auth_class.new(app) — without any options. The inherited initialize then tries to look up options[:type] which is nil, causing:

Grape::Exceptions::UnknownAuthStrategy: unknown auth strategy:

(Note the blank strategy name — because options[:type] is nil.)

Fix

Guard the strategy lookup so it only runs when :type is present in options. This preserves compile-time validation for the outer Grape::Middleware::Auth::Base middleware wrapper while allowing subclasses to be instantiated cleanly as the actual strategy implementations.

def initialize(app, **options)
  super
  return unless options.key?(:type)

  @auth_strategy = Grape::Middleware::Auth::Strategies[options[:type]].tap do |auth_strategy|
    raise Grape::Exceptions::UnknownAuthStrategy.new(strategy: options[:type]) unless auth_strategy
  end
end

Test

Added a spec that registers a custom auth middleware class inheriting from Grape::Middleware::Auth::Base and verifies it works correctly with auth :custom_token in an API.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 5, 2026

Danger Report

No issues found.

View run

@dblock
Copy link
Copy Markdown
Member Author

dblock commented Apr 5, 2026

Needs #2670.

…m Grape::Middleware::Auth::Base

When a custom auth strategy class inherits from Grape::Middleware::Auth::Base
and is registered via Grape::Middleware::Auth::Strategies.add, it would raise
Grape::Exceptions::UnknownAuthStrategy with a blank strategy name upon being
instantiated by StrategyInfo#create.

The root cause: PR ruby-grape#2563 moved the strategy lookup from request-time (_call)
to initialization-time (initialize). When StrategyInfo#create instantiates
the custom class via auth_class.new(app) (without options), the inherited
initialize tries to look up options[:type] which is nil, causing the error.

The fix: guard the strategy lookup so it only runs when :type is present in
options. This preserves compile-time validation for the outer middleware
wrapper while allowing subclasses to be instantiated as actual strategies.

Fixes ruby-grape#2669.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dblock dblock force-pushed the fix/auth-base-subclass-issue-2669 branch from b891b0f to d510262 Compare April 6, 2026 02:34
@ericproulx ericproulx merged commit 4638481 into ruby-grape:master Apr 6, 2026
40 checks passed
@dblock dblock deleted the fix/auth-base-subclass-issue-2669 branch April 7, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnknownAuthStrategy raised after upgrading from grape 2.3.0 to 2.4.0

2 participants