Skip to content

Deal with relative url root installations - #903

Merged
BobbyMcWho merged 2 commits into
omniauth:2_0-indevfrom
oliverguenther:fix/relative-url-roots
Dec 10, 2020
Merged

BobbyMcWho merged 2 commits into
omniauth:2_0-indevfrom
oliverguenther:fix/relative-url-roots

Conversation

@oliverguenther

@oliverguenther oliverguenther commented Jun 13, 2017 •

Copy link
Copy Markdown
Contributor

This PR allows OmniAuth to transparently handle relative url root installations by correctly prefixing the default Strategy#request_path and Strategy#callback_path with SCRIPT_NAME as provided by Rack.

Also, in order to detect the full current path, uses Rack::Request#path instead of #path_info, as the latter does not include SCRIPT_NAME.

A deeper question remains: What should happen with custom callback and request paths? I can't think of a case where a user with a relative url root installation would not want to prefix its paths, regardless of setting a custom path or not.

However, since some people may have configured custom paths in order to deal with relative root, including this change may be breaking.

I have thus included a specific spec that asserts that custom request paths are not prefixed.

This deals with:

#767
#753

We've stumbled over these bugs while fixing our own OmniAuth relative url root path helpers over at opf/openproject#4169

This is #837 updated to current master.

@BobbyMcWho

Copy link
Copy Markdown
Member

Closing to trigger CI

@BobbyMcWho BobbyMcWho closed this Dec 10, 2020
@BobbyMcWho BobbyMcWho reopened this Dec 10, 2020
oliverguenther and others added 2 commits December 10, 2020 12:19
This commit allows OmniAuth to handle relative url root installations
by correctly prefixing the default `Strategy#request_path` and
`Strategy#callback_path` with `SCRIPT_NAME` as provided by Rack.

Also, in order to detect the full current path, uses
`Rack::Request#path` instead of `#path_info`, as the latter does not
include SCRIPT_NAME.

A deeper question remains: What should happen with custom callback and
request paths? I can't think of a case where a user with a relative url
root installation would *not* want to prefix its paths, regardless of
setting a custom path or not.
However, since some people may have configured custom paths in order to
deal with relative root, including this change may be breaking.
@oliverguenther

Copy link
Copy Markdown
Contributor Author

Great to see this repository is being worked on again. We're keeping a fork with this patch included until now. I will look into the potential changes and push again.

@BobbyMcWho
BobbyMcWho force-pushed the fix/relative-url-roots branch from 51f579c to f9a6c0f Compare December 10, 2020 17:29
@BobbyMcWho

BobbyMcWho commented Dec 10, 2020 •

Copy link
Copy Markdown
Member

@oliverguenther I just rebased, fixed some tests and force pushed to your branch

@BobbyMcWho
BobbyMcWho merged commit cc7e285 into omniauth:2_0-indev Dec 10, 2020
@oliverguenther

Copy link
Copy Markdown
Contributor Author

@BobbyMcWho Excellent, thank you very much. Let us know if we can be of any help here

@oliverguenther
oliverguenther deleted the fix/relative-url-roots branch December 10, 2020 18:45
@janko

janko commented Jan 22, 2021

Copy link
Copy Markdown
Contributor

I've read the issues linked, but I couldn't understand where exactly the issue was before.

The problem is that this change seems to break usage with omniauth-facebook and omniauth-google_oauth2 when using relative roots (e.g. Rack's map or Rails' mount), because both of them unfortunately override OmniAuth's Strategy#callback_url to full_host + script_name + callback_path, which will result in duplicated script_name part since now the :callback_path option needs to include the script name.

I feel like it would be possible to solve the original issue with relative roots without requiring the inclusion of the script name into :request_path and :callback_path, but I don't yet understand enough where the original issue was to be certain.

@BobbyMcWho

Copy link
Copy Markdown
Member

Those strategies did that specifically because omniauth base didn't handle it correctly. With the 2.0 release, it's noted as a breaking change, so if the strategy needs updated to reflect the change in omniauth, then that should be done on their end.

@janko

janko commented Jan 23, 2021 •

Copy link
Copy Markdown
Contributor

Yeah, I agree that ultimately the problem is on the end of these external strategies. However, my understanding was that they redefined #callback_url because they wanted to remove the query string part, as that's the only thing that was missing compared to the original #callback_url implementation. But the correct way to do that would be something like super.sub(/\?.+/, ""), which would work with both OmniAuth 1.x and 2.x.

I just wasn't able to understand what exactly was the original problem this PR solves, because with relative root path-matching of request/callback requests worked just fine, and #callback_url returned the correct value (it included the script name). The only thing is that retrieving Strategy#request_path and Strategy#callback_path directly wouldn't include the script name, but I didn't consider that to be wrong behaviour (an OmniAuth configuration doesn't need to be aware that it's running under a relative root, it just needs to work, which IMO it did).

Anyway, regardless of whether I understand it or not it's done now, and I appreciate the reply. I'll then send PRs for omniauth-facebook and omniauth-google_oauth2.

@oliverguenther

oliverguenther commented Jan 23, 2021 via email

Copy link
Copy Markdown
Contributor Author

jessieay added a commit to jessieay/omniauth-azure-activedirectory-v2 that referenced this pull request Sep 7, 2022
* One of the breaking changes in OmniAuth 2.0+ relates to how relative
  URL installations are handled. See: omniauth/omniauth#903 and https://github.com/omniauth/omniauth/wiki/Upgrading-to-2.0#relative-root-apps
* As a result, when `azure_activedirectory_v2` is used with OmniAuth
  2.0+ for an app that lives at a relative URl, the `#callback_url` is incorrect (the relative URL is included twice).
* This is because OmniAuth is now prefixing the default
  Strategy#request_path and Strategy#callback_path with SCRIPT_NAME, but `azure_activedirectory_v2` is also adding `script_name` to `callback_url`.
* This update makes this gem compatible with OmniAuth 2.0+ but will break
  relative URL installatons for OmniAuth 1.x so I've also updated the
  gemspec to rely on a version of omniauth-oauth2 that has a dependency
  on omniauth 2.0: omniauth/omniauth-oauth2#152 (comment)
jessieay added a commit to jessieay/omniauth-azure-activedirectory-v2 that referenced this pull request Sep 7, 2022
* One of the breaking changes in OmniAuth 2.0+ relates to how relative
  URL installations are handled. See: omniauth/omniauth#903 and https://github.com/omniauth/omniauth/wiki/Upgrading-to-2.0#relative-root-apps
* As a result, when `azure_activedirectory_v2` is used with OmniAuth
  2.0+ for an app that lives at a relative URl, the `#callback_url` is incorrect (the relative URL is included twice).
* This is because OmniAuth is now prefixing the default
  Strategy#request_path and Strategy#callback_path with SCRIPT_NAME, but `azure_activedirectory_v2` is also adding `script_name` to `callback_url`.
* This update makes this gem compatible with OmniAuth 2.0+ but will break
  relative URL installatons for OmniAuth 1.x so I've also updated the
  gemspec to rely on a version of omniauth-oauth2 that has a dependency
  on omniauth 2.0: omniauth/omniauth-oauth2#152 (comment)
* Similar change to omniauth-google-oauth2 was made here: zquestz/omniauth-google-oauth2#403
whithajess added a commit to whithajess/xero-oauth2-omniauth-strategy that referenced this pull request Sep 13, 2022
@sferik sferik mentioned this pull request Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants