Skip to content

Fix issue 636, pass request object to authenticate function. #643

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

Merged
merged 5 commits into from
Oct 20, 2019

Conversation

W0126
Copy link
Contributor

@W0126 W0126 commented Sep 9, 2018

Pass request object to authenticate function, request may used in authenticate function.

@coveralls
Copy link

coveralls commented Sep 9, 2018

Pull Request Test Coverage Report for Build 1163

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 94.697%

Totals Coverage Status
Change from base Build 1157: 0.0%
Covered Lines: 1250
Relevant Lines: 1320

💛 - Coveralls

@@ -572,7 +572,7 @@ def validate_user(self, username, password, client, request, *args, **kwargs):
"""
Check username and password correspond to a valid and active User
"""
u = authenticate(username=username, password=password)
u = authenticate(request, username=username, password=password)
if u is not None and u.is_active:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_active is already checked in django (>=1.11 I think) do we need to check this here again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix make sure request as a parameter to authenticate function, so authentication module can get request information.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cleder I agree regarding is_active, in fact Django also skips the check if user model doesn't implement is_active. Let's remove this part.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be a separate PR though. Approving as the fix is correct.

@adamchainz
Copy link
Contributor

This would be useful for me. I just discovered django-axes, requires request to be passed, and throws an exception if it's not: https://django-axes.readthedocs.io/en/latest/3_usage.html#authenticating-users

What's the blocker on this getting merged? I'm happy to take over.

adamchainz added a commit to adamchainz/django-axes that referenced this pull request Jul 8, 2019
The [usage documentation](https://django-axes.readthedocs.io/en/latest/3_usage.html) advises to create subclass of `AxesBackend` to ignore the lack of `request` if necessary. I've done this in a project using `django-oauth-toolkit`, which doesn't pass `request` (though it should as per [this PR](django-oauth/django-oauth-toolkit#643)).

This meant that the axes.W003 check was being triggered, so I've fixed it to check for subclasses of `AxesBackend` as well as the class itself.
aleksihakli pushed a commit to jazzband/django-axes that referenced this pull request Jul 9, 2019
The [usage documentation](https://django-axes.readthedocs.io/en/latest/3_usage.html) advises to create subclass of `AxesBackend` to ignore the lack of `request` if necessary. I've done this in a project using `django-oauth-toolkit`, which doesn't pass `request` (though it should as per [this PR](django-oauth/django-oauth-toolkit#643)).

This meant that the axes.W003 check was being triggered, so I've fixed it to check for subclasses of `AxesBackend` as well as the class itself.
simanto604newscred pushed a commit to simanto604newscred/django-axes that referenced this pull request Jul 31, 2019
The [usage documentation](https://django-axes.readthedocs.io/en/latest/3_usage.html) advises to create subclass of `AxesBackend` to ignore the lack of `request` if necessary. I've done this in a project using `django-oauth-toolkit`, which doesn't pass `request` (though it should as per [this PR](django-oauth/django-oauth-toolkit#643)).

This meant that the axes.W003 check was being triggered, so I've fixed it to check for subclasses of `AxesBackend` as well as the class itself.
simanto604newscred pushed a commit to simanto604newscred/django-axes that referenced this pull request Jul 31, 2019
The [usage documentation](https://django-axes.readthedocs.io/en/latest/3_usage.html) advises to create subclass of `AxesBackend` to ignore the lack of `request` if necessary. I've done this in a project using `django-oauth-toolkit`, which doesn't pass `request` (though it should as per [this PR](django-oauth/django-oauth-toolkit#643)).

This meant that the axes.W003 check was being triggered, so I've fixed it to check for subclasses of `AxesBackend` as well as the class itself.
@W0126
Copy link
Contributor Author

W0126 commented Aug 14, 2019

@adamchainz we may need at least 1 reviewers with write access to approve this.

@adamchainz
Copy link
Contributor

@cleder ? Also I volunteer myself to be given write access if that helps :)

Copy link
Contributor

@IvanAnishchuk IvanAnishchuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_active check could be improved but otherwise valid fix.

@@ -572,7 +572,7 @@ def validate_user(self, username, password, client, request, *args, **kwargs):
"""
Check username and password correspond to a valid and active User
"""
u = authenticate(username=username, password=password)
u = authenticate(request, username=username, password=password)
if u is not None and u.is_active:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cleder I agree regarding is_active, in fact Django also skips the check if user model doesn't implement is_active. Let's remove this part.

@@ -572,7 +572,7 @@ def validate_user(self, username, password, client, request, *args, **kwargs):
"""
Check username and password correspond to a valid and active User
"""
u = authenticate(username=username, password=password)
u = authenticate(request, username=username, password=password)
if u is not None and u.is_active:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be a separate PR though. Approving as the fix is correct.

@adamchainz
Copy link
Contributor

Please revert this, it's passing a non django request to authenticate() !

@W0126 W0126 deleted the fix-issue-636 branch November 27, 2019 16:55
@blu14x
Copy link

blu14x commented Mar 12, 2020

Please revert this, it's passing a non django request to authenticate() !

Exactly.
I hope this will be reverted sometime. Until then I use a PatchedTokenView for requests to /o/token/:

from django.contrib.auth import authenticate
from oauth2_provider.oauth2_validators import OAuth2Validator
from oauth2_provider.views import TokenView


class PatchedOAuth2Validator(OAuth2Validator):
    def validate_user(self, username, password, client, request, *args, **kwargs):
        u = authenticate(username=username, password=password)
        if u is not None and u.is_active:
            request.user = u
            return True
        return False


class PatchedTokenView(TokenView):
    validator_class = PatchedOAuth2Validator

@n2ygk
Copy link
Contributor

n2ygk commented Mar 13, 2020 via email

n2ygk added a commit that referenced this pull request Mar 14, 2020
n2ygk added a commit that referenced this pull request Mar 14, 2020
auvipy pushed a commit that referenced this pull request Mar 16, 2020
auvipy pushed a commit that referenced this pull request Mar 16, 2020
@indigodomo
Copy link

When is this going to get updated in pypi? The broken version is in there now. I really don't want to have to deploy with my own copy...

@n2ygk
Copy link
Contributor

n2ygk commented Mar 16, 2020

You can always use a git reference in your requirements.txt. For example:

git+https://github.com/jazzband/django-oauth-toolkit.git@9bd4c1bfcf26eecf8fde793baa48c386885c19be

@adamchainz
Copy link
Contributor

Or install from a commit has on github: https://adamj.eu/tech/2019/03/11/pip-install-from-a-git-repository/

wannacfuture pushed a commit to wannacfuture/Tuto_Django that referenced this pull request Jun 1, 2023
The [usage documentation](https://django-axes.readthedocs.io/en/latest/3_usage.html) advises to create subclass of `AxesBackend` to ignore the lack of `request` if necessary. I've done this in a project using `django-oauth-toolkit`, which doesn't pass `request` (though it should as per [this PR](django-oauth/django-oauth-toolkit#643)).

This meant that the axes.W003 check was being triggered, so I've fixed it to check for subclasses of `AxesBackend` as well as the class itself.
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

Successfully merging this pull request may close these issues.

9 participants