Skip to content

Conversation

ddelemeny
Copy link

Hello there,

Following my (unanswered) thread on google groups regarding the possibility to have another model than Django's AUTH_USER_MODEL as the Resource Owner for tokens (I've used django-organizations OrganizationUser as an example), I've been experimenting a little bit with the idea.

This PR attempts to implement a configurable setting allowing developers to change the model considered as Resource Owner for Grants, AccessToken and RefreshToken.

Usage:

  • set OAUTH2_PROVIDER_RESOURCE_OWNER_MODEL setting as the import path of the desired Resource Owner Model. ex OAUTH2_PROVIDER_RESOURCE_OWNER_MODEL = 'myapp.MyResourceOwner'
  • Override OAuthLibCore with a custom _extract_resource_owner method, to get a resource owner instance from the request

It's been a bit hard for me to follow the path of a request in the multiple reciprocal calls between DOT and OAuthLib, so I don't really know how far I am from a complete implementation. This patch works for my use case and passes existing tests.

What do you think ? Is it a sane way to achieve this kind of flexibility ? Is it desirable in DOT ?
Please review and comment.

- change relevant ForeignKeys
- add resource owner extractors in backend and validator
- add configurable user lookup in views
@coveralls
Copy link

coveralls commented Mar 9, 2017

Coverage Status

Coverage decreased (-0.2%) to 97.192% when pulling 539bd13 on ddelemeny:swappable-resource-owner into 34f3b7b on evonove:master.

@jleclanche
Copy link
Collaborator

I'm torn. On the one hand this a pretty common pattern, but on the other hand we've talked about making all the token models etc abstract so that you can swap them out for your own version.

I'm +0 on this. We could have both.

@jleclanche jleclanche force-pushed the master branch 2 times, most recently from 14cfaa3 to 92311a5 Compare March 9, 2017 06:40
@ddelemeny
Copy link
Author

@jleclanche I understand, I had a closer look at #252 and #347 and it would indeed make it easier to swap in this pattern.
Having my proposal implemented in DOT or left in a cookbook for the expected abstract models is fine by me, as long as it it made possible.

What would be needed to make either solution go forward ?

@jleclanche
Copy link
Collaborator

I think they're not mutually exclusive. We definitely need swappable models but #347 is just not mergeable. The PR here is more reasonable. I'm happy to review it but I need to find the time, and I don't want to be the only one making this decision. @clintonb can you chime in?

Copy link
Contributor

@clintonb clintonb left a comment

Choose a reason for hiding this comment

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

This approach seems clear. You'll want to add documentation so that others know how to take advantage of this functionality.

:param credentials: Authorization credentials dictionary containing
`client_id`, `state`, `redirect_uri`, `response_type`
:param allow: True if the user authorize the client, otherwise False
:param allow: True if the resource owner authorize the client, otherwise False
Copy link
Contributor

Choose a reason for hiding this comment

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

s/authorize/authorized

UserModel = get_user_model()



Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: This extra newline is not necessary.


from ..models import get_application_model, Grant, AccessToken, RefreshToken
from ..models import get_application_model, get_resource_owner_model
from ..models import Grant, AccessToken, RefreshToken
Copy link
Contributor

Choose a reason for hiding this comment

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

These two lines can be combined.

self.user = UserModel.objects.create_user("test_user", "test@user.com", "123456")

def test_model(self):
self.user.resource_owners.all()
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment explaining what you're testing here.

@jleclanche jleclanche force-pushed the master branch 2 times, most recently from 80a3973 to f59509f Compare March 14, 2017 22:06
@menecio
Copy link

menecio commented Mar 27, 2017

@jleclanche maybe you will be interested on reviewing this one? #467

@menecio
Copy link

menecio commented Apr 27, 2017

As the swappable models feature is already pushed to master, could we work on this? I can help you if needed.

Maybe adding a detailed entry in the docs on how to use swappable models to accomplish this behavior can be enough?

@jleclanche
Copy link
Collaborator

I'll always take docs PRs. I still think they're not mutually exclusive, so if you guys still consider this wanted, it'll have to be rebased.

@ddelemeny
Copy link
Author

ddelemeny commented Apr 29, 2017 via email

@jleclanche
Copy link
Collaborator

Closing since this has stalled for too long and isn't ready to land.

@jleclanche jleclanche closed this Apr 9, 2018
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.

5 participants