Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Post authentication redirect to requested URL #502

Closed
Roger-Sa opened this issue Jul 1, 2021 · 17 comments
Closed

[Question] Post authentication redirect to requested URL #502

Roger-Sa opened this issue Jul 1, 2021 · 17 comments
Labels

Comments

@Roger-Sa
Copy link

Roger-Sa commented Jul 1, 2021

Hi,

I'm using AAD authentication. Let's say I have an app at myapp.com and a user wants to access myapp.com/product?id=123 (which might be bookmarked, for instance). If the user is already logged in this works fine, but if he needs to log in, post authentication redirects the user to myapp.com not the requested URL. I know I can configure a post login redirect, but this needs to be a fixed URL. Is it somehow possible for post authentication to redirect the user to where he/she intended to go?

@anthonychu
Copy link
Member

I don't think this is possible right now, though it's something we should look into supporting. @mkarmark any suggestions?

@Roger-Sa
Copy link
Author

Roger-Sa commented Jul 9, 2021

I am reading here that, due to security implications:

According to the OAuth 2.0 specification (section 3.1.2 of RFC 6749), a redirection endpoint URI must be an absolute URI.

and:

If you have several subdomains and your scenario requires that, upon successful authentication, you redirect users to the same page from which they started, using a state parameter might be helpful.

I presume this can be done with custom authentication. I also presume most prefer to just use the built-in authentication. If you (Microsoft) implement this for the built-in authentication, and automatic redirection is out of the question for security reasons, then probably make the requested URL available somehow (some endpoint similar to /.auth/me perhaps?).

@rellis-of-rhindleton
Copy link

rellis-of-rhindleton commented Aug 17, 2021

I found this on Customize sign-ins and sign-outs — would it be applicable to Static Web Apps as well?

After users sign in to your app, they usually want to be redirected to the same section of the same page, such as /wiki/Main_Page#SectionZ. However, because URL fragments (for example, #SectionZ) are never sent to the server, they are not preserved by default after the OAuth sign-in completes and redirects back to your app. Users then get a suboptimal experience when they need to navigate to the desired anchor again. This limitation applies to all server-side authentication solutions.

In App Service authentication, you can preserve URL fragments across the OAuth sign-in. To do this, set an app setting called WEBSITE_AUTH_PRESERVE_URL_FRAGMENT to true.

@brenoalvs
Copy link

Any news on this?
Currently I think the only way to do it is by implementing this kind of logic on the app side which does not make any sense when you are using the built in auth.

@ghost
Copy link

ghost commented Oct 20, 2021

My company is facing the same issue and we really were using this for our documentation site to include articles and code documents at one place for the company. But this is a very bad end user experience since we have variety of readers of our documentation who like to have their fully qualified/favorited URLs within our HUGE site. They have to navigate through the content since they always end up landing at the Home Page post authentication.

@mishapos
Copy link

mishapos commented Nov 3, 2021

We now support adding "post_login_redirect_uri=.referrer" to achieve the behavior of redirecting back to the page before the login flow started. Query parameters and fragments are not supported though.

@Roger-Sa
Copy link
Author

Roger-Sa commented Nov 3, 2021

@mishapos
What is the correct usage exactly?

Tried this in staticwebapp.config.json:

{
   "route": "/login",
   "redirect": "/.auth/login/aad?post_login_redirect_uri=.referrer" 
}

But get redirected to .../.auth/.referrer

@anthonychu
Copy link
Member

anthonychu commented Nov 4, 2021

This is a fairly new feature and we're waiting for query string support to be added, so we haven't documented it yet. You can only use it in a response override:

{
  "routes": [
    {
      "route": "/*",
      "allowedRoles": [
        "authenticated"
      ]
    }
  ],
  "responseOverrides": {
    "401": {
      "statusCode": 302,
      "redirect": "/.auth/login/github?post_login_redirect_uri=.referrer"
    }
  }
}

@Roger-Sa
Copy link
Author

Roger-Sa commented Nov 4, 2021

Awesome! It works!

@Roger-Sa Roger-Sa closed this as completed Nov 4, 2021
@msardi23
Copy link

msardi23 commented Nov 5, 2021

This is a fairly new feature and we're waiting for query string support to be added, so we haven't documented it yet. You can only use it in a response override:

{
  "routes": [
    {
      "route": "/*",
      "allowedRoles": [
        "authenticated"
      ]
    }
  ],
  "responseOverrides": {
    "401": {
      "statusCode": 302,
      "redirect": "/.auth/login/github?post_login_redirect_uri=.referrer"
    }
  }
}

This is great and works once changes are pushed back to Azure Static Apps service.
However, the local auth emulator fails / hangs when the app is run locally with the swa cli.
Is there a fix in the works for the swa cli or a work around available?

@manlm
Copy link

manlm commented Nov 9, 2021

This is a fairly new feature and we're waiting for query string support to be added, so we haven't documented it yet. You can only use it in a response override:

{
  "routes": [
    {
      "route": "/*",
      "allowedRoles": [
        "authenticated"
      ]
    }
  ],
  "responseOverrides": {
    "401": {
      "statusCode": 302,
      "redirect": "/.auth/login/github?post_login_redirect_uri=.referrer"
    }
  }
}

When I click the link on facebook workplace chat, this does not work. Are there any update on this case?

@ghost
Copy link

ghost commented Nov 10, 2021

We now support adding "post_login_redirect_uri=.referrer" to achieve the behavior of redirecting back to the page before the login flow started. Query parameters and fragments are not supported though.

@mishapos does this mean the encoding is also not supported (like %20 for space)? I tested and see that redirection works only as long as the URL does not contain a space.

Example:
This works: https://something.com/articles/EXTERNAL/README.html
This does not work: https://something.com/articles/INTERNAL/Some%20Configuration/Some%20Add%20User/Some%20Add%20User.html

@gbelenky
Copy link

gbelenky commented Nov 23, 2021

Is it also supposed to work in the swa cli during the local development? It did not work for me

@waterskijubilant
Copy link

waterskijubilant commented Nov 24, 2021

This does not work if the link is clicked from another site.

Edit: Slight correction, if you open a fresh browser instance it works as expected for 30 seconds, then if you try it again it starts redirecting to the root page.

@niand
Copy link

niand commented Jan 19, 2022

I am getting the same behaviour as mentioned above. The redirect to a specific subpage works on fresh browser, afterwords I end up on the root url again.

#693

@MattParkerDev
Copy link

Has there been any progress on supporting query parameters in the post login redirect uri?

@brettnf
Copy link

brettnf commented May 3, 2024

Query parameters and fragments are not supported though.

@mishapos

Are there any plans to support fragments, as it is already the case for web apps?
Would be very helpful as we share links to our documentation where it is important to also jump directly to section headings...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests