Make OAuth flow work for local development #745
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I've modified the OAuth redirect logic to support the workflow where a developer runs the frontend locally while using the production website's backend API. When the initial request's Origin header is localhost or 127.0.0.1, that hostname is used as the redirect target, rather than the configured value that would normally be used.
To do this, we're now constructing an
OAuth2Session
instance per request. In OSMCha/osmcha-frontend#743 (comment) I speculated that this might not be safe, but I now believe that it is. The front page of requests-oauthlib's docs actually demonstrates exactly this pattern.Once this is deployed to production, you'll be able to run osmcha-frontend using
yarn run start
(which by default usesREACT_APP_PRODUCTION_API_URL='https://osmcha.org/api/v1'
), and click the "Sign in" button to log in as if you were logging in on osmcha.org. Upon successful authentication you'll be redirected back to your local development frontend.I tested this manually by poking the API server with curl. In the below examples, the development server thinks that it's running on
http://osmcha.example.com
(andOAUTH_REDIRECT_URI
is set accordingly).localhost:
127.0.0.1:
HTTPS localhost:
Production domain:
No
Origin
header:Malformed
Origin
header:Malicious domain no. 1 (unrelated domain attempting to authenticate via OSMCha)
Malicious domain no. 2 (contains "localhost" as a substring):