Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with mozilla-django-oidc callback #421

Closed
leuat opened this issue Jun 15, 2021 · 6 comments
Closed

Problems with mozilla-django-oidc callback #421

leuat opened this issue Jun 15, 2021 · 6 comments

Comments

@leuat
Copy link

leuat commented Jun 15, 2021

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
Copy link
Author

leuat commented Jun 15, 2021

`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
Copy link

variable commented Jun 16, 2021

Any chance you can display the corrupted json content?

@leuat
Copy link
Author

leuat commented Aug 9, 2021

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
Copy link
Author

leuat commented Oct 18, 2021

resolved

@leuat leuat closed this as completed Oct 18, 2021
@rhclayto
Copy link

rhclayto commented Dec 27, 2021

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
Copy link

resolved

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

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

No branches or pull requests

4 participants