singingwolfboy / flask-dance

Doing the OAuth dance with style using Flask, requests, and oauthlib.
https://pypi.python.org/pypi/Flask-Dance/
MIT License
1k stars 157 forks source link

GitHub oAuth - Flask - AWS Beanstalk #413

Closed QAInsights closed 1 year ago

QAInsights commented 1 year ago

I implemented GitHub oAuth for my app. Upon clicking Login using GitHub link, it is not logging in. From the logs, I can see the below error:

WARNING:flask_dance.consumer.oauth2:OAuth 2 authorization error: redirect_uri_mismatch description: The redirect_uri MUST match the registered callback URL for this application. uri: https://docs.github.com/apps/managing-oauth-apps/troubleshooting-authorization-request-errors/#redirect-uri-mismatch

My auth callback URL: https://perfgpt.qainsights.com/login/github/authorized

I went through this issue, but it is not solving my case.

Any insights please?

singingwolfboy commented 1 year ago

My auth callback URL: https://perfgpt.qainsights.com/login/github/authorized

The error message says that given URL does not match the registered callback URL. You need to edit your OAuth app on GitHub and make sure that this URL is in the "Authorization callback URL" field.

QAInsights commented 1 year ago

Thanks @singingwolfboy I was able to fix it. Below is the code I used. Auth call URL is configured correctly.

class ReverseProxied(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        # if one of x_forwarded or preferred_url is https, prefer it.
        forwarded_scheme = environ.get("HTTP_X_FORWARDED_PROTO", None)
        preferred_scheme = application.config.get("PREFERRED_URL_SCHEME", None)
        if "https" in [forwarded_scheme, preferred_scheme]:
            environ["wsgi.url_scheme"] = "https"
        return self.app(environ, start_response)
application.config.update(dict(
    PREFERRED_URL_SCHEME='https'
))
application.wsgi_app = ReverseProxied(application.wsgi_app)