Consider reverting https://github.com/aspnet/Security/pull/982
https://github.com/aspnet/Security/issues/930#issuecomment-261433813
Please consider, as it breaks some 3rd party security libraries. It breaks the openiddict's workflow, by falling into error even before reaching the controller that should handle the authentication.
/cc @Tratcher
@sebastienros FYI, this "by design" issue will likely impact Orchard 2's OpenID module when moving the oauth2 branch to ASP.NET Core 1.1.0, as the JWT bearer middleware will now collide with the cookies middleware set up by Identity.
@PinpointTownes your mileage might be better if you call it a breaking change, I had a bad feeling about that one :(
We've known for a long time now that having multiple active challenge middleware will cause conflicts, especially incompatible ones. The multiple auth types ambiguity can only be properly resolved by naming the desired type when issuing the challenge. I don't see how else the conflict can be deterministicly resolved in the short or long term.
I don't see how else the conflict can be deterministicly resolved in the short or long term.
I've always thought that the whole point of returning a boolean from HandleUnauthorizedAsync was to allow a middleware to prevent the other middleware higher in the pipeline from applying their own challenge when unwinding the pipeline (that's why the bearer middleware returns false - because multiple schemes are allowed - while the cookies or the OIDC middleware return true).
Obviously, this doesn't work properly (specially since this design change), but I'm not sure to understand what's the difficult point here.
That bool prevented the downstream handlers from being called during that specific call to Challenge, not for the whole response. As your original bug reported (#930) this didn't affect automatic unwind challenges. They never observed the original Challenge so they didn't know that someone else handled the response already. We "fixed" the automatic unwind challenge issue by enabling all the middleware to observe the initial Challenge, which just highlighted more conflicts between multiple automatic auth challenges.
That bool prevented the downstream handlers from being called during that specific call to Challenge, not for the whole response.
But technically, what prevents us from extending that concept to automatic challenges (applied via the OSH callback)?
IMHO, here's how it should ideally work:
-
When
Challengeis manually called:- Invoke the corresponding middleware when schemes are specified.
- Invoke the automatic challenge middleware when no scheme is specified (=
AutomaticScheme) until one of the middleware returnstrue. - Don't let the OSH callback invoke anything when unwinding the request pipeline.
-
When
Challengeis not manually called (i.e a simple 401 response is returned):- Invoke the automatic challenge middleware when no scheme is specified (=
AutomaticScheme) until one of the middleware returnstrueto stop the processing.
- Invoke the automatic challenge middleware when no scheme is specified (=
Hi @PinpointTownes , we discussed this at length with @Tratcher @blowdart @HaoK and some others and we have plans on how to mitigate this in the future, but we're not planning on changing this behavior in 1.1.
The mitigation plans are to do one of these:
- Try to detect this scenario at runtime and alert the developer that this is an unsupported scenario (having more than one “automatic” challenge registered).
- Change how automatic challenges are registered so that only one auth provider per app pipeline can be automatic. For example, instead of each provider having its own bool setting, you would instead have one "global" setting (i.e. per-IAppBuilder) where you specify which auth protocol is the automatic one (and all others are not automatic).
Tracking the vNext proposals here: https://github.com/aspnet/Security/issues/1054
@Eilon hey.
That's extremely unfortunate because it's really an annoying regression that impacts everyone, specially ASOS/OpenIddict and IdentityServer users (using a bearer middleware + Identity is extremely frequent in apps that use the code/implicit flow, which includes a lot of SPA apps).
Since there's nothing planned to revert it, I guess the most sensible option we have is to keep using 1.0.0, which works fine (like Katana used to, BTW).
/cc @leastprivilege
I still don't understand how this specific change has been an improvement as compared to previous versions...
The problem that existed both before this change as well as after it was that it wasn't very meaningful to have more than one auth provider that was "automatic." That configuration simply didn't make sense because the auth providers weren't design to coordinate with each other about any "hands-off" behavior if one provider already did a challenge. The fix should be easy: don't set more than one auth provider to be automatic. That the original behavior sometimes happened to work for some scenarios was entirely an accident.
BTW I don't think there should be a reason to stick to 1.0.0 because of this (because it wasn't supposed to work on 1.0.0 either!).
The fix should be easy: don't set more than one auth provider to be automatic.
We're obviously not living in the same world. In the real world, nothing is as easy as you imagine, I'm afraid :smile:
There are basically 2 options to work around this breaking change/regression in a mixed bearer middleware/cookie middleware scenario:
-
Set
AutomaticChallengetofalsein one of the two middleware. Since the default Identity templates rely on automatic challenge to work correctly (hem...), disabling automatic challenge for the "application cookie" middleware will have many side effects and will pretty much require rewriting the account controller. Of course, you can also disable automatic challenge in the other middleware, but in this case, you must decorate all your API controllers with[Authorize(ActiveAuthenticationSchemes = "Bearer")]so they're not redirected to the login page instead of getting a proper 401... -
Use
app.UseWhento ensure the two automatic middleware never run together (my favorite option):
// Add a middleware used to validate access tokens and protect the API endpoints.
app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), branch => {
branch.UseOAuthValidation();
});
// Warning: starting with ASP.NET Core 1.1.0, having multiple authentication middleware
// with AutomaticChallenge = true is no longer supported. To ensure the validation and
// cookies middleware don't collide, app.UseIdentity() is only invoked for non-API calls.
app.UseWhen(context => !context.Request.Path.StartsWithSegments("/api"), branch => {
branch.UseIdentity();
branch.UseGoogleAuthentication(...);
branch.UseTwitterAuthentication(...);
});
Now, if you're still convinced it's "easy" (hint for you: it's not :trollface:), you can meet real people in our Gitter rooms and tell them how trivial it is to fix their app after migrating to 1.1.0:
- https://gitter.im/aspnet-contrib/AspNet.Security.OpenIdConnect.Server
- https://gitter.im/IdentityServer/IdentityServer4
- https://gitter.im/openiddict/openiddict-core
BTW I don't think there should be a reason to stick to 1.0.0 because of this
A breaking change like this is always a good reason to keep using the previous version: targeting the 1.1.0 bits in our libs would de facto force our users to migrate to 1.1.0 and to fix their apps to work around this change... so unless you volunteer to answer all the "why did my app break when migrating to the latest aspnet-contrib bits?" questions for me, I will keep targeting the 1.0.0 bits (even if I'd really like to get rid of ugly bugs workarounds) :trollface:
And it looks like I'm not the only one: https://twitter.com/leastprivilege/status/801723059149357056
(because it wasn't supposed to work on 1.0.0 either!).
If automatic challenge was so "dangerous", then why enabling it by default in both the cookies middleware and the bearer middleware? If it doesn't work well when having multiple middleware, then it should be an opt-in feature... you realize that people might want to use them in the same app, right?
Oh - how I miss the times when I had the energy to write such long and passionate comments...now I mostly have the feeling that I am getting to old for this shit ;))
FWIW I agree that this is a breaking change and probably shouldn't have been made in a .1 release...
That said, this scenario was one of the earliest ones as @blowdart ran into this almost immediately
[Authorize(ActiveAuthenticationSchemes = "Bearer")] or something equivalent where you move the scheme into a policy [Authorize("Bearer")] is what was intended for this scenario.
See https://docs.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme
I have a headache from walking thru the code, but I have a feeling this change is related to another breaking change I came across today in 1.1: https://github.com/aspnet/Templates/issues/686#issuecomment-265329808
In short, can you please test account linking in 1.1 in the ASP.NET Identity templates to confirm if it's broken or not?
That bug is the bug tracking the work to fix linking for templates, basically the manage controller actions need to explicitly clear the cookie, see https://github.com/aspnet/Templates/pull/726
But yeah, those template breaks are related to: https://github.com/aspnet/Announcements/issues/201