You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Rack::Builder#to_app is not designed to be called for every request. #1083
moduleOmniAuthclassBuilder < Rack::Builderdefinitializedefault_app=nil, &blocksuper@app=to_appifdefault_appenddefcallenv(@app || to_app).call(env)end# The rest same as current OmniAuth::Builder class,# not duplicated here....endend
It ought to cover people who are currently using it in both ways, as middleware as suggested in the docs or as Rack::Builder is expected to be used.
The problem is that omniauth has been telling people to use OmniAuth::Builder as middleware. E.g.,
useOmniAuth::Builderdoprovider:developerend
Any Rack-like use(middleware, app, *args, &block) implementation is going to push those arguments onto a stack, and when it finally assembles the Rack app, it's going to call OmniAuth::Builder.new(app, *args, &block). So that means that OmniAuth has no control over which class method is called, and the only two places to fix this in existing applications written this way are ::new or #initialize. (And Rack::Builder already has a class method, ::app, that calls #to_app.)
This would work for applications using OmniAuth::Builder as middleware with no changes needed. It would break for people (like me) who have been subclassing Builder, but at least it wouldn't be subtle (TypeError (superclass must be a Class (Module given))) and could be resolved by subclassing Rack::Builder and including the OmniAuth::Builder module.
It would break for people who have been instantiating Builder and using the instance directly, though.
One more option -- rename the current Builder class to something else and define a new Builder to use it internally.
moduleOmniAuthclassAuthBuilder < Rack::Builder# ...same as current OmniAuth::Builder, except no #callundef_method:callendclassBuilderdefinitializeapp, &block@app=AuthBuilder.app(app, &block)enddefcallenv@app.call(env)endendend
If you had a preference on how you wanted this resolved (or the priorities to take into consideration, e.g., what level of backward-compatibility, etc), I'd be willing to take a crack at it and submit a PR.
I think the biggest downstream consumer is devise, so we'd wanna look if/how devise uses the builder and try and at least keep backwards compatibility for that
I searched the devise code. They don't use Builder. The only reference to "builder" is a form builder. I also ran their test suite against the code in the PR just in case, and everything passes.
I opened another PR, with the Builder-as-a-module idea. I don't really have a preference between them, but the module version means there won't be an extra do-nothing piece of middleware in the stack since it just returns the built app.
I've just not had the time to manage a large refactor at the moment, I'd love to be able to take care of a few different long standing issues w/ omniauth in the next major release, I just haven't had the time to work on OmniAuth outside of critical maintenance. Namely I'd like to resolve this issue, completely remove the hashie dependency, not dup the entire strategy w/ every request, and shortcircuit out of omniauth early when the request is not on a path that omniauth cares about.
I've just not had the time to manage a large refactor at the moment, I'd love to be able to take care of a few different long standing issues w/ omniauth in the next major release, I just haven't had the time to work on OmniAuth outside of critical maintenance. Namely I'd like to resolve this issue, completely remove the hashie dependency, not dup the entire strategy w/ every request, and shortcircuit out of omniauth early when the request is not on a path that omniauth cares about.
Makes sense! What do you think of doing those changes incrementally? I can help.
Fixed in c29ad9b: OmniAuth::Builder#call now builds the middleware stack on the first request and reuses it, instead of calling to_app on every request. It'll ship in the next release.
Activity
BobbyMcWho commentedon Aug 23, 2022
Thanks for the report, this is an area of the library I haven't looked much into. Would memoizing the to_app call be sufficient?
ioquatix commentedon Aug 23, 2022
I think you'd want to do it at build time because memoization is not thread safe.
speckins commentedon Sep 23, 2022
It's also not necessary to redefine
#callsince it's the same as the superclass.https://github.com/rack/rack/blob/ab9cd7416c1b5253cd280d75704bafd87510c120/lib/rack/builder.rb#L261-L263
If OmniAuth::Builder is intended to be used as shown in the README, maybe overridding
::newwould be a low-impact way of resolving this?ioquatix commentedon Sep 23, 2022
maybe consider this???
speckins commentedon Sep 24, 2022
Except that
OmniAuth::Builderadds a few methods of its own, of which, the#providermethod is probably the main reason for using it.omniauth/lib/omniauth/builder.rb
Line 29 in 7d90ba2
What about this:
It ought to cover people who are currently using it in both ways, as middleware as suggested in the docs or as
Rack::Builderis expected to be used.ioquatix commentedon Sep 24, 2022
I don't think side effects in
#initializeare a good idea.I like your original proposal: #1083 (comment)
I'd even suggest just doing it like this:
Then use
OmniAuth::Builder.build(...).BobbyMcWho commentedon Sep 24, 2022
I'm not opposed to a major bump if need be, so the change can be as breaking as needed
speckins commentedon Sep 26, 2022
The problem is that omniauth has been telling people to use
OmniAuth::Builderas middleware. E.g.,Any Rack-like
use(middleware, app, *args, &block)implementation is going to push those arguments onto a stack, and when it finally assembles the Rack app, it's going to callOmniAuth::Builder.new(app, *args, &block). So that means that OmniAuth has no control over which class method is called, and the only two places to fix this in existing applications written this way are::newor#initialize. (And Rack::Builder already has a class method,::app, that calls#to_app.)What about...
This would work for applications using
OmniAuth::Builderas middleware with no changes needed. It would break for people (like me) who have been subclassing Builder, but at least it wouldn't be subtle (TypeError (superclass must be a Class (Module given))) and could be resolved by subclassingRack::Builderand including theOmniAuth::Buildermodule.It would break for people who have been instantiating Builder and using the instance directly, though.
speckins commentedon Sep 26, 2022
One more option -- rename the current Builder class to something else and define a new Builder to use it internally.
speckins commentedon Oct 3, 2022
If you had a preference on how you wanted this resolved (or the priorities to take into consideration, e.g., what level of backward-compatibility, etc), I'd be willing to take a crack at it and submit a PR.
BobbyMcWho commentedon Oct 3, 2022
I think the biggest downstream consumer is devise, so we'd wanna look if/how devise uses the builder and try and at least keep backwards compatibility for that
ioquatix commentedon Oct 3, 2022
If you want an easy way to test external dependencies, you might like https://github.com/ioquatix/bake-test-external
Rename Builder to AuthBuilder and create Builder wrapper
speckins commentedon Oct 4, 2022
I searched the devise code. They don't use Builder. The only reference to "builder" is a form builder. I also ran their test suite against the code in the PR just in case, and everything passes.
Change Builder from a class to a module and only build app once
Change Builder from a class to a module and only build app once
speckins commentedon Oct 11, 2022
I opened another PR, with the Builder-as-a-module idea. I don't really have a preference between them, but the module version means there won't be an extra do-nothing piece of middleware in the stack since it just returns the built app.
BobbyMcWho commentedon Oct 11, 2022
Thanks @speckins I will take a look as soon as I have some time, I've been very busy
jcmfernandes commentedon Jan 16, 2023
I tripped on this earlier today. How can I help get #1094 merged?
BobbyMcWho commentedon Jan 16, 2023
I've just not had the time to manage a large refactor at the moment, I'd love to be able to take care of a few different long standing issues w/ omniauth in the next major release, I just haven't had the time to work on OmniAuth outside of critical maintenance. Namely I'd like to resolve this issue, completely remove the hashie dependency, not dup the entire strategy w/ every request, and shortcircuit out of omniauth early when the request is not on a path that omniauth cares about.
jcmfernandes commentedon Jan 17, 2023
Makes sense! What do you think of doing those changes incrementally? I can help.
Build OmniAuth::Builder's middleware stack once
sferik commentedon Oct 20, 2026
Fixed in c29ad9b:
OmniAuth::Builder#callnow builds the middleware stack on the first request and reuses it, instead of callingto_appon every request. It'll ship in the next release.