lepture / authlib

The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.
https://authlib.org/
BSD 3-Clause "New" or "Revised" License
4.49k stars 448 forks source link

Passing `authorize_params` from env fails for client #519

Closed filimonic closed 1 year ago

filimonic commented 1 year ago

Describe the bug

.env
OAUTH_YANDEX_AUTHORIZE_PARAMS=force_confirm=1
load_dotenv()
app = Flask(__name__)
app.config.from_prefixed_env()
app.config.from_prefixed_env(prefix='OAUTH')
oauth = OAuth(app)
oauth.register(name='yandex')
...
@app.route('/login/yandex')
def login_yandex():
  ...
  oauth_yandex = oauth.create_client('yandex')
  return oauth_yandex.authorize_redirect(cb_url) 

Error Stacks

ValueError: dictionary update sequence element #0 has length 1; 2 is required

Traceback (most recent call last):
  File "S:\...\env\Lib\site-packages\flask\app.py", line 2548, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "S:\...\env\Lib\site-packages\flask\app.py", line 2528, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "S:\...\env\Lib\site-packages\flask\app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "S:\...\Lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "S:\...\env\Lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "S:\...\env\Lib\site-packages\flask\app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "S:\...\routes.py", line 21, in login_yandex
    return oauth_yandex.authorize_redirect(cb_url)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "S:\...\env\Lib\site-packages\authlib\integrations\flask_client\apps.py", line 43, in authorize_redirect
    rv = self.create_authorization_url(redirect_uri, **kwargs)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "S:\...\env\Lib\site-packages\authlib\integrations\base_client\sync_app.py", line 316, in create_authorization_url
    kwargs.update(self.authorize_params)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

Expected behavior

{name}_AUTHORIZE_PARAMS should be parsed someway if is str

Environment:

Additional context

Add any other context about the problem here.

lepture commented 1 year ago

Authlib Flask client reads the {name}_AUTHORIZE_PARAMS from Flask app config. If you set the config as dict, it would work well.

Because the environment values are treated as str by .from_prefixed_env, you should not set {name}_AUTHORIZE_PARAMS in environment. Instead, you can:

app.config.from_object('yourapplication.default_settings')

Set {name}_AUTHORIZE_PARAMS in your default_settings.