Git Product home page Git Product logo

jrd / django-oauth2-authcodeflow Goto Github PK

View Code? Open in Web Editor NEW
14.0 14.0 8.0 356 KB

Authenticate with any OpenId Connect/Oauth2 provider through authorization code flow. PKCE is also supported

Home Page: https://pypi.org/project/django-oauth2-authcodeflow/

License: MIT License

Makefile 1.12% Python 98.21% Shell 0.67%
django oauth2 oauth2-client oidc oidc-client openid-connect openidconnect pkce

django-oauth2-authcodeflow's People

Contributors

cpontvieux-systra avatar dependabot[bot] avatar dgarceries avatar gabdug avatar jrd avatar osfog avatar pinoatrome avatar sannies avatar shachimi-systra avatar xuru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-oauth2-authcodeflow's Issues

Token authentication not working. Invalid audience

I am using the package for openidconnect on my API an it works well.
When I am trying to consume the API from the frontend, I always get login form instead of the requested resource.
I tried to test with postman to read log and i see that the problem is with the audience retrieved from the token.

Screenshot 2024-02-06 at 21 30 30

Login/logout events

Enhance the documentation (with examples) about auditing included in the README.md file using a proper documentation file.

Typo and question regarding the INSTALLED_APPS setting

First, a big THANK YOU for making this library available. Its the only one I found which works properly so far.

In your README you have the string django.contrib.session but I think it should actually be django.contrib.sessions (with an s at the end).

And my question: why does it matter where in the INSTALLED_APPS list I put the oauth2_authcodeflow? Isn't the position of e.g. oauth2_authcodeflow.middleware.LoginRequiredMiddleware within the MIDDLEWARE list much more important? From C# and Java I know that it is important to put the OpenID Connect middleware after the session middleware. But anything like this is not mentioned in the README. Or maybe I am wrong with my understand how middlewares are processed in Django (I am new to it)?

Thank you

Irreversible migrations

The unapply operation of migrations fails:

./manage.py migrate oauth2_authcodeflow zero

 File "/Users/pinoatrome/.virtualenvs/my-project/lib/python3.11/site-packages/django/db/migrations/migration.py", line 159, in unapply
    raise IrreversibleError(
django.db.migrations.exceptions.IrreversibleError: Operation <RunPython <function forwards at 0x108c94680>> in oauth2_authcodeflow.0003_auto_20210528_1432 is not reversible

This is because some migrations use RunPython with only forward function, missing reverse function.

From docs on RunPython

"The reverse_code argument is called when unapplying migrations. This callable should undo what is done in the code callable so that the migration is reversible. If reverse_code is None (the default), the RunPython operation is irreversible."

Problem with migration

It seems a migration was renamed and there are issues now:
I have this migration from previous version 0004_alter_blacklistedtoken_id_and_more, and now it's called 0004_blacklistedtoken_constraint.py.

It creates the following error:

django.db.utils.ProgrammingError: relation "unique_username_token" already exists

Any idea how to fix it?

CIAM token request fails with "The client MUST NOT use more than one authentication method in each"

After a valid login on CIAM the callback view invokes the authentication backend that POST a request to the OP to obtain a token
but the server response has 400 error status with content: b'{"error_description":"The client MUST NOT use more than one authentication method in each","error":"invalid_request"}'

the CIAM is configured to not accept requests with 'client_secret' params when using PKCE flow.

the current implementation (v.1.1.0) requires an empty value for OIDC_RP_CLIENT_SECRET settings entry

https:///oauth2/token
with params:
{
'grant_type': 'authorization_code',
'client_id': ,
'client_secret': ,
'redirect_uri': 'http://127.0.0.1/oidc/callback',
'code': ,
'code_verifier': <value from session (only if PKCE is enabled)>
}

the response is an error 400 with content
b'{"error_description":"The client MUST NOT use more than one authentication method in each","error":"invalid_request"}'

[bug] cannot logout with `django.contrib.auth.backends.ModelBackend`

Hi there, thanks for this package. I'm using both backends:

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
    "oauth2_authcodeflow.auth.AuthenticationBackend",
]

and when I'm trying to logout, having used the django.contrib.auth.backends.ModelBackend to login, I get the following error:

id_token is missing from the session, cannot logout

I think this is because of:

if constants.SESSION_ID_TOKEN not in request.session:
raise ValueError("id_token is missing from the session, cannot logout")

which is later used here:

BlacklistedToken.blacklist(id_token)

I think if the user has used the django backend, this check should not be done.

One possible solution is to add a blacklist, only if there's an id_token.

Thoughts? I can also submit a PR if needed.

Regards

Migration error on a Django web app with MySQL (8.0.36)

I encountered an error during the application of migrations in my Django web application when using a MySQL database. Specifically, when attempting to apply migration 0004_alter_blacklistedtoken_id_and_more.py, the following error is raised:

Applying oauth2_authcodeflow.0004_alter_blacklistedtoken_id_and_more...Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
  File "/usr/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 75, in execute
    return self.cursor.execute(query, args)
  File "/usr/lib/python3.8/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/usr/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/usr/lib/python3.8/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
MySQLdb.OperationalError: (1071, 'Specified key was too long; max key length is 3072 bytes')

Upon inspecting the previous migration (0003_auto_20210528_1432), I noticed that the issue is correctly handled and causes no problems. Below is the relevant snippet from 0003_auto_20210528_1432:

try:
    migrations.AddConstraint(
        model_name='blacklistedtoken',
        constraint=models.UniqueConstraint(fields=('username', 'token'), name='unique_username_token'),
    ),
except Exception:
    # no constraint on mysql, max key is 3072 bytes which is not enough
    pass

Skipping the problematic migration (0004_alter_blacklistedtoken_id_and_more.py) doesn't seem to affect the functionality of the library, but resolving this issue would be preferable for a full compatibility.

Login/logout auditing

Thank you for providing this module, that's the only one that actually work!

Authentication for my app is working fine with Okta integration. Now I need to add auditing, especially log all successful/unsuccessful login attempts and logout events.
I have configured the logger in my settings.py like this:

logger = logging.getLogger('oauth2_authcodeflow')
logger.addHandler(logging.StreamHandler())
if DEBUG:
    logger.setLevel(logging.DEBUG)
else:
    logger.setLevel(logging.INFO)

But I cannot find login/logout messages in the logs. Is it missing currently in the module code? Or I have misconfigured something?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.