django-oauth-toolkit icon indicating copy to clipboard operation
django-oauth-toolkit copied to clipboard

AttributeError at /o/token/: 'User' object has no attribute 'id'

Open codewriter3000 opened this issue 9 months ago • 9 comments

Describe the bug

I input my body for /o/token/ and I get this error:

Internal Server Error: /o/token/
Traceback (most recent call last):
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\generic\base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\decorators\csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\generic\base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\decorators\debug.py", line 92, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\views\base.py", line 261, in post
    url, headers, body, status = self.create_token_response(request)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\views\mixins.py", line 124, in create_token_response
    return core.create_token_response(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\oauth2_backends.py", line 156, in create_token_response
    headers, body, status = self.server.create_token_response(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\oauth2\rfc6749\endpoints\base.py", line 112, in wrapper
    return f(endpoint, uri, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\oauth2\rfc6749\endpoints\token.py", line 114, in create_token_response
    return grant_type_handler.create_token_response(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\openid\connect\core\grant_types\dispatchers.py", line 101, in create_token_response
    return handler.create_token_response(request, token_handler)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\oauth2\rfc6749\grant_types\authorization_code.py", line 310, in create_token_response
    token = modifier(token, token_handler, request)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\openid\connect\core\grant_types\authorization_code.py", line 43, in add_id_token
    return super().add_id_token(token, token_handler, request, nonce=nonce)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\openid\connect\core\grant_types\base.py", line 142, in add_id_token
    token['id_token'] = self.request_validator.finalize_id_token(id_token, token, token_handler, request)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\oauth2_validators.py", line 825, in finalize_id_token
    claims, expiration_time = self.get_id_token_dictionary(token, token_handler, request)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\oauth2_validators.py", line 806, in get_id_token_dictionary
    claims = self.get_oidc_claims(token, token_handler, request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\oauth2_validators.py", line 786, in get_oidc_claims
    data = self.get_claim_dict(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\oauth2_validators.py", line 768, in get_claim_dict
    claims = {"sub": str(request.user.id)}
                         ^^^^^^^^^^^^^^^
AttributeError: 'User' object has no attribute 'id'

To Reproduce

Use this to represent your user ID in models.py:

user_id = models.BigAutoField(primary_key=True)

Expected behavior

I'm expecting not to get this error. Version

2.3.0

  • [X] I have tested with the latest published release and it's still a problem.
  • [ ] I have tested with the master branch and it's still a problem.

Additional context

I believe that I temporarily fixed it on my end by editing the source code to replace .user.id with .user.user_id

codewriter3000 avatar Oct 05 '23 02:10 codewriter3000

I get this error after I apply my temporary fix as described in "Additional context" above:

Internal Server Error: /o/token/
Traceback (most recent call last):
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwcrypto\jwk.py", line 1002, in import_from_pem
    cert = x509.load_pem_x509_certificate(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\cryptography\x509\base.py", line 583, in load_pem_x509_certificate
    return rust_x509.load_pem_x509_certificate(data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unable to load PEM file. See https://cryptography.io/en/latest/faq/#why-can-t-i-import-my-pem-file for more details. MalformedFraming

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\generic\base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\decorators\csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\generic\base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\decorators\debug.py", line 92, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\views\base.py", line 261, in post
    url, headers, body, status = self.create_token_response(request)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\views\mixins.py", line 124, in create_token_response
    return core.create_token_response(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\oauth2_backends.py", line 156, in create_token_response
    headers, body, status = self.server.create_token_response(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\oauth2\rfc6749\endpoints\base.py", line 112, in wrapper
    return f(endpoint, uri, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\oauth2\rfc6749\endpoints\token.py", line 114, in create_token_response
    return grant_type_handler.create_token_response(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\openid\connect\core\grant_types\dispatchers.py", line 101, in create_token_response
    return handler.create_token_response(request, token_handler)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\oauth2\rfc6749\grant_types\authorization_code.py", line 310, in create_token_response
    token = modifier(token, token_handler, request)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\openid\connect\core\grant_types\authorization_code.py", line 43, in add_id_token
    return super().add_id_token(token, token_handler, request, nonce=nonce)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauthlib\openid\connect\core\grant_types\base.py", line 142, in add_id_token
    token['id_token'] = self.request_validator.finalize_id_token(id_token, token, token_handler, request)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\oauth2_validators.py", line 838, in finalize_id_token
    header["kid"] = request.client.jwk_key.thumbprint()
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2_provider\models.py", line 237, in jwk_key
    return jwk.JWK.from_pem(oauth2_settings.OIDC_RSA_PRIVATE_KEY.encode("utf8"))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwcrypto\jwk.py", line 1070, in from_pem
    obj.import_from_pem(data, password)
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwcrypto\jwk.py", line 1007, in import_from_pem
    raise e
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwcrypto\jwk.py", line 992, in import_from_pem
    key = serialization.load_pem_private_key(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\cryptography\hazmat\primitives\serialization\base.py", line 25, in load_pem_private_key
    return ossl.load_pem_private_key(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 747, in load_pem_private_key
    return self._load_key(
           ^^^^^^^^^^^^^^^
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 929, in _load_key
    self._handle_key_loading_error()
  File "C:\Users\alex.MICHARSKI\AppData\Local\Programs\Python\Python311\Lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 984, in _handle_key_loading_error
    raise ValueError(
ValueError: ('Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).', 
[<OpenSSLError(code=503841036, lib=60, reason=524556, reason_text=unsupported)>])

codewriter3000 avatar Oct 05 '23 02:10 codewriter3000

image

codewriter3000 avatar Oct 05 '23 13:10 codewriter3000

Same error here

Mani1124 avatar Oct 06 '23 09:10 Mani1124

Algorithm keep blank, try it.

AakrutiPatel avatar Oct 07 '23 07:10 AakrutiPatel

Algorithm keep blank, try it.

It works when the algorithm is blank, but I want to apply an algorithm.

codewriter3000 avatar Oct 07 '23 16:10 codewriter3000

https://django-oauth-toolkit.readthedocs.io/en/latest/oidc.html#setting-up-oidc-enabled-clients

When you use OIDC, you can choose algorithm.

https://django-oauth-toolkit.readthedocs.io/en/latest/getting_started.html#authorization-code

When you use Authorization Code, Client Type can choose confidential.

AakrutiPatel avatar Oct 08 '23 02:10 AakrutiPatel

I can't use confidential. This application is running on a mobile app and this app is unable to use a registered client secret

https://oauth.net/2/client-types

codewriter3000 avatar Oct 08 '23 05:10 codewriter3000

Can you provide more information on what computer technologies are primarily used?

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

You could try using the Fetch API to get the data.

AakrutiPatel avatar Oct 08 '23 11:10 AakrutiPatel

The Fetch API is for JavaScript and this is Python.

codewriter3000 avatar Oct 09 '23 02:10 codewriter3000