umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.49k stars 2.69k forks source link

Clarify cookie auth usage and future #17407

Open enkelmedia opened 2 weeks ago

enkelmedia commented 2 weeks ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

14.2.0

Bug summary

I'm working on something that needs to check if the user is currently logged in to the backoffice from places outside of the backoffice. Some parts are also used inside the backoffice (so some kind of a hybrid-thing). This feature, that used to work on v13, uses the backoffice cookie configured for Umbraco.Cms.Core.Constants.Security.BackOfficeAuthenticationType to check if the current request comes from a backoffice authenticated user.

I've shared a snippet here that is similar to what I currently do: https://our.umbraco.com/forum/umbraco-9/106857-how-do-i-determine-if-a-backoffice-user-is-logged-in-from-a-razor-view#comment-334423

After moving to v14 it seems like this cookie is more or less not used by the backoffice anymore? After manually removing the cookie, the backoffice still works as expected and I'm not "thrown out". I know that the Management API uses a bearer token for auth, but the cookie is still set during login which makes it kind of tempting for me (an possibly other developers https://github.com/umbraco/Umbraco-CMS/issues/17358) to use this cookie.

There are a couple of potential issues here:

I'm kind of wondering if this cookie is just a left over from the past? At the same time, we have some nice use cases for this cookie, for example using it for test/stage environments so that we can force a backoffice user login before allowing someone to access the site. Without the cookie we would not be able to know that they are logged in.

So I guess the answer to the question about the future of this cookie will dictate of this is something that should be addressed or if I need to look for another solution. I've notice in the core-code that @kjac wrote some of the stuff around this, maybe you know more?

Cheers!

Update: It came to my mind that @nul800sebastiaan's great Hangfire-dashboard uses the same technique and would probably suffer from the same issues.

https://github.com/nul800sebastiaan/Cultiv.Hangfire/blob/bellissima/Cultiv.Hangfire/UmbracoBuilderExtensions.cs

github-actions[bot] commented 2 weeks ago

Hi there @enkelmedia!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

nul800sebastiaan commented 2 weeks ago

I'll just link this to https://github.com/nul800sebastiaan/Cultiv.Hangfire/issues/37 - yes I have a problem with cookie auth as well 😅

nul800sebastiaan commented 2 weeks ago

I had a chat with @kjac as well and I forgot exactly what conclusion we came to but I think it was some filter in the routing pipeline that I would need, correct Kenn?

Not sure if that applies to your use cases though, since it looks like you don't have 1 predictable route pattern like I do. Perhaps you can do something with a bearer token? Not entirely sure what your use case is!

enkelmedia commented 2 weeks ago

I kind of have multiple use cases =D

One of them is the "lock out screen" that where we basically check for the backoffice cookie and login the user if they're not already logged in.

Another is similar to yours where I'm rendering something inside a iframe. For this scenario that I'm working on (basically statically rendered HTML inside a iframe) I'm currently looking at using the srcDoc-attribute of the iframe element - basically fetching the HTML from a bearer token authed endpoint and render it.

I guess that it's not super uncommon for someone to want to render a iframe that uses something similar to your Hangfire-thing but that maybe requires navigation inside the iframe - in that case srcDoc would probably not be a viable work around.

nul800sebastiaan commented 2 weeks ago

I think I tried that as well, but I don't think it helped, since there's css and is links in the iframe that need auth as well. Not sure if I tried srcDoc specifically though!

nul800sebastiaan commented 2 weeks ago

Just remembered; yep that's exactly what I tried, and it's not a feasible path. Indeed because of clickable links in the iframe and references to css and is files that fail too.

kjac commented 2 weeks ago

Quick comment from my part 😄

The auth cookie should not be used for anything other than the core login process. It is solely there to make the login screen work. Indeed, if you use external login providers for the backoffice, I am actually not even sure it's ever set, because it's really not necessary for the login process to complete.

enkelmedia commented 2 weeks ago

Thanks for the info @kjac!

I guess then the question would be: What’s the recommended approach in a situation like this?

Could really be about anything that needs to be protected en only shown for logged in Users. Not sure if @rsoeteman is using any of this for Media Protect?