mozilla / mozilla-django-oidc

A django OpenID Connect library
https://mozilla-django-oidc.readthedocs.io
Mozilla Public License 2.0
452 stars 169 forks source link

Problems with mozilla-django-oidc callback #421

Closed leuat closed 3 years ago

leuat commented 3 years ago

Hi there! OIDC & django n00b here, so please bear with me.

Set up mozilla-django-oidc on a https apache server with rs256 and correct endpoints (OIDC_OP_JWKS_ENDPOINT,OIDC_RP_SIGN_ALGO,OIDC_OP_TOKEN_ENDPOINT, OIDC_OP_AUTHORIZATION_ENDPOINT) all set etc.

I manage to log in - but afterwards, the server crashes with a json format error (basically crashing on "self.user = auth.authenticate(**kwargs)" in the MDO views.py

I have no idea what is going on here, and I'm unable to find any information on google about what this error could mean - any tips would be helpful!

python 3.6, django 3.2.4, mozilla-django-oidc 1.2.4

Screenshot 2021-06-15 at 16 30 07
leuat commented 3 years ago

`Environment:

Request Method: GET Request URL: https://www.nutil.org/oidc/callback/?state=UcE4K8MaU5FsnwgeenrAOWR72YdhQxOT&session_state=cb1ce972-0be7-4427-8bb5-78ff8449e728&code=243462ae-5bb1-4d11-9faa-fe3dc45d534d.cb1ce972-0be7-4427-8bb5-78ff8449e728.360bb004-2b5f-4d68-bcb0-c71b030be07c

Django Version: 3.2.4 Python Version: 3.6.9 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'mozilla_django_oidc'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback (most recent call last): File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, callback_kwargs) File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/django/views/generic/base.py", line 70, in view return self.dispatch(request, *args, *kwargs) File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/django/views/generic/base.py", line 98, in dispatch return handler(request, args, kwargs) File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/mozilla_django_oidc/views.py", line 99, in get self.user = auth.authenticate(*kwargs) File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/django/views/decorators/debug.py", line 42, in sensitive_variables_wrapper return func(func_args, func_kwargs) File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/django/contrib/auth/init.py", line 76, in authenticate user = backend.authenticate(request, credentials) File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/mozilla_django_oidc/auth.py", line 286, in authenticate return self.get_or_create_user(access_token, id_token, payload) File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/mozilla_django_oidc/auth.py", line 307, in get_or_create_user user_info = self.get_userinfo(access_token, id_token, payload) File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/mozilla_django_oidc/auth.py", line 245, in get_userinfo return user_response.json() File "/home/ubuntu/nutilweb/nutilenv/lib/python3.6/site-packages/requests/models.py", line 900, in json return complexjson.loads(self.text, **kwargs) File "/usr/lib/python3.6/json/init.py", line 354, in loads return _default_decoder.decode(s) File "/usr/lib/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None

Exception Type: JSONDecodeError at /oidc/callback/ Exception Value: Expecting value: line 2 column 1 (char 1) `

variable commented 3 years ago

Any chance you can display the corrupted json content?

leuat commented 3 years ago

Back from holdays, gotta look into this again.

in auth.py "get_userinfo", user_response.json() crashes - also whenever I'm trying to print it to the log, so the format conversion fails (sys.stderr.write(user_response.json() crashes with the same message). Any ideas on how to actually view the response object?

leuat commented 3 years ago

resolved

rhclayto commented 2 years ago

resolved

@leuat

How did you resolve it? I'm having the same problem.


Edit: My working code change below. (Works with Authelia OpenID provider.) In auth.py

import json
def get_userinfo(self, access_token, id_token, payload):
        """Return user details dictionary. The id_token and payload are not used in
        the default implementation, but may be used when overriding this method"""

        user_response = requests.get(
            self.OIDC_OP_USER_ENDPOINT,
            headers={
                'Authorization': 'Bearer {0}'.format(access_token)
            },
            verify=self.get_settings('OIDC_VERIFY_SSL', True),
            timeout=self.get_settings('OIDC_TIMEOUT', None),
            proxies=self.get_settings('OIDC_PROXY', None))
        user_response.raise_for_status()
        msg = user_response.text
        utf8 = msg.encode('utf-8')
        jws = JWS.from_compact(utf8)
        jwsjson = json.loads(jws.payload)
        return jwsjson
yasirgit commented 1 year ago

resolved

How did you resolve this? Can I get some references? Struggling with the same issue. image