cloudfront-authorization-at-edge: Issues with the refresh endpoint endlessly redirecting after signin
We’re using v2.1.0 (vanilla, nothing fancy/special). Occasionally, a user will get stuck in this redirect loop. I suspect it’s when the JWT expires, and a lazy evaluation happens. Not all the time, but sometimes, it will put the browser into this constant back and forth redirect ping thing that keeps showing the message/image displayed below. If you look at the XHR request stuff, you’ll see that it’s just endlessly redirecting from the /refresh
endpoint, to the cognito /auth
and /login
endpoints. I can post the XHR’s (once I sanitize them), if needed.
This might be due to a refactor done recently for this version that has the JWT refreshed lazily perhaps?
I’ve tried with the JWT expiration set to 1 hour, 12 hours, 24 hours, and 5 minutes and the same thing. It just changes how fast the cycles go before this issue manifests/gets recreated.
For the end user, there’s no solution other than clearing cookies to correct this problem. Once they sign in again, the problem goes away for several refreshes, some unknown number (can’t recreate it consistently, it appears to be a timing thing) before it hits again.
I’m going to try reverting back to a previous version of the app (v2.0.19) to see if this has any positive impact.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 32
@HudsonAkridge could you finally solve this issue? As it seems like we’re experiencing the same problem I would appreciate any hints how to approach this.
@ottokruse @james1050 I’ve verified that this works with everything, and I was able to remove Amplify from the configuration and it’s all good!
I’ll try and briefly type up the approach with a more detailed explanation later when I have more time. For now, this is some o fthe things done to make this work starting from a fresh deploy.
auth.yourdomain.com
works for me.HttpHeaders
section, REMOVE theContent-Security-Policy
header section of the JSON completely. Leave the others in place.cookieSettings
, the cookies need to change to become:This should be the setting for all
configuration.json
files after deploy. You might need to manually update the Lambdas that have this and redeploy (RefreshAuthHandler
,ParseAuthHandler
,SignOutHandler
,HttpHeadersHandler
,CheckAuthHandler
) if it doesn’t show as correct like the above.Now, in the frontend client code. Remove Amplify. Like, all of it. You shouldn’t need it because this repo @ottokruse and co have built pretty much does it all for you.
Create or modify the method used to get the JWT for your application’s headers (in my case, I need the JWT to attach to a call to
api.yourdomain.com
withAuthorization: "Bearer <jwt>"
). Here’s the final code that’s worked well for me in testing/trialing so far:I’m not saying that the code above is the most optimized way to do this, or the cleanest, just that it’s fairly simple and it works.
Also, add the
backgroundLoad.json
file to your site, and include a.bundleignore
file at the root of your project (if you’re using Yarn that is) and just put a single entry in it forbackgroundLoad.json
. This should prevent it from bundling that JSON file in with other javascript/json stuff so your file size is kept at small as possible. You’re only going to be refreshing that when it’s close to time for the token to expire, so it’s not going to be creating much traffic unless your token is set to expire insanely fast for some reason.What should happen is that if a token is expired, it should do a fetch of that json file behind the scenes which will load the JSON file, hitting the route, firing off the
/refreshauth
lambda. Once that’s complete, it’ll fetch the updatedidToken
value and use that JWT value until it’s set to expire or close to an expiration.This prevents hammering anything on the server and only executes it once, when needed.
Occasionally an individual request will fail on the background fetch, and you just have to click a link somewhere else in your app again (assuming it’s always calling this
getUserToken
method for every authentication related call), I haven’t been able to totally solve for that yet, but I haven’t had a lot of time to dig in to looking at it.So far, so good.
I hope this helps someone else 🙂
One more thought.
If I understand your situation it is roughly this:
If that’s indeed true, then a good way forward would be to change 3 above: front the APIs also with the Auth@Edge CloudFront. Then refreshes will be seamless our of the box, as you’ve seen Auth@Edge redirects automatically to the refresh endpoint and back to the requested location. So then you don’t need to trigger token refresh manually in your SPA. Side benefit is that this also eliminates CORS preflight requests, so better latency.