Comments (9)
I know this is not an issue, but for people who end up here, I will leave my final class implementation. It's a simple workaround that fits my case:
from rest_framework_social_oauth2.backends import DjangoOAuth2
from oauth2_provider.models import AccessToken
from django.contrib.auth.models import User
class MyAppOAuth2(DjangoOAuth2):
def get_user_details(self, response):
if response.get(self.ID_KEY, None):
user = User.objects.get(pk=response[self.ID_KEY])
return {'username': user.username,
'email': user.email,
'fullname': user.get_full_name(),
'first_name': user.first_name,
'last_name': user.last_name
}
return {}
def user_data(self, access_token, *args, **kwargs):
try:
user_id = AccessToken.objects.get(token=access_token).user.pk
return {self.ID_KEY: user_id}
except AccessToken.DoesNotExist:
return None
def do_auth(self, access_token, *args, **kwargs):
"""Finish the auth process once the access_token was retrieved"""
data = self.user_data(access_token, *args, **kwargs)
response = kwargs.get('response') or {}
response.update(data or {})
kwargs.update({'response': response, 'backend': self})
if response.get(self.ID_KEY, None):
user = User.objects.get(pk=response[self.ID_KEY])
return user
else:
return None
Thank you, @PhilipGarnero for your attention and library.
from django-rest-framework-social-oauth2.
Where do i put this line of code?
from django-rest-framework-social-oauth2.
Hi, I don't know why DjangoOAuth2 not finished for work. This class is not completed!!!
from django-rest-framework-social-oauth2.
In case anyone stuck with where you should put this snippet (as I am).
You need to specify this new backend in settings.py
.
If you put this code in file myapp/inner_oauth.py
, your backends in settings should be like:
AUTHENTICATION_BACKENDS = (
# Facebook OAuth2
'social.backends.facebook.FacebookAppOAuth2',
'social.backends.facebook.FacebookOAuth2',
# MyAppOAuth2 based on django-rest-framework-social-oauth2
'myapp.inner_oauth.MyAppOAuth2',
# Django
'django.contrib.auth.backends.ModelBackend',
)
from django-rest-framework-social-oauth2.
I worked my way out of this by creating a superclass on DjangoOAuth2 that implemented get_user_details method and returning an empty dict.
class MyAppOAuth2(DjangoOAuth2):
def get_user_details(self, response):
return {}
from django-rest-framework-social-oauth2.
Yes sorry about the lack of documentation about this.
I didn't implement a default behavior because everyone is doing things differently so in the end inheriting from it seemed the best way to do it.
You figured it out by yourself so I guess it's fine like this.
from django-rest-framework-social-oauth2.
@ivanff Open source work is provided freely and people offer support on their free time. You are not entitled to anything. Instead of complaining and waiting for others to do your work, do it yourself.
from django-rest-framework-social-oauth2.
class MyAppOAuth2(DjangoOAuth2):
def get_user_details(self, response): if response.get(self.ID_KEY, None): user = User.objects.get(pk=response[self.ID_KEY]) return {'username': user.username, 'email': user.email, 'fullname': user.get_full_name(), 'first_name': user.first_name, 'last_name': user.last_name } return {} def user_data(self, access_token, *args, **kwargs): try: user_id = AccessToken.objects.get(token=access_token).user.pk return {self.ID_KEY: user_id} except AccessToken.DoesNotExist: return None def do_auth(self, access_token, *args, **kwargs): """Finish the auth process once the access_token was retrieved""" data = self.user_data(access_token, *args, **kwargs) response = kwargs.get('response') or {} response.update(data or {}) kwargs.update({'response': response, 'backend': self}) if response.get(self.ID_KEY, None): user = User.objects.get(pk=response[self.ID_KEY]) return user else: return None
Hi, can you please elaborate on where exactly this snippet should go, I tried every possible file, but still end up with the same error.
Help is much appreciated.
Thanks
from django-rest-framework-social-oauth2.
Hi, thank you for this solution, it works just fine.
The problem I have is that I can still use expired access_tokens. After calling /convert-token, I acquire an access_token that should expire in 10hrs. But after 10hrs, the token is still usable. I managed to fix this by adding a simple check in the do_auth
method, but I'm not sure about the correctness of this solution, as this is pretty important to not be checked within the package.
Maybe I'm missing something? Here's the improved do_auth
method.
def do_auth(self, access_token, *args, **kwargs):
"""Finish the auth process once the access_token was retrieved"""
token = AccessToken.objects.get(token=access_token)
if token.is_expired():
raise AuthenticationFailed(f"Token {token.token} has expired.")
data = self.user_data(token, *args, **kwargs)
response = kwargs.get('response') or {}
response.update(data or {})
kwargs.update({"response": response, "backend": self})
if response.get(self.ID_KEY, None):
user = User.objects.get(pk=response[self.ID_KEY])
return user
else:
return None
from django-rest-framework-social-oauth2.
Related Issues (20)
- Documentation is out of sync HOT 1
- Django rest framework social oauth2 causing unexpected error with Djongo for mongoDB conversion of django app HOT 2
- How can I access "access tokens" of registered users in Backend ? HOT 1
- installing to docker container HOT 1
- Getting error while creating new application. HOT 1
- AppleIdAuth not working HOT 6
- url oauth callback for twitter HOT 1
- Fix simple typo: internaly -> internally HOT 1
- How can I revoke the refresh tokens?
- Your credentials aren't allowed HOT 1
- Refresh Token Error HOT 1
- unsupported_grant_type when trying to refresh token HOT 1
- custom backend not working with convert token api
- Question HOT 10
- Quick fix to enable this package to work with Django 4 and the older versions HOT 1
- why im getting invalid client error while trying to post in postman HOT 2
- I am getting a 401 Error (Unauthorized) and Uncaught (in promise) Error. Same is working fine on PostMan
- How to associate an existing user with multiple social accounts (different emails)? [DRF_SOCIAL_OAUTH2]
- cannot import name 'url' from 'django.conf.urls' HOT 5
- Client ID Error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-rest-framework-social-oauth2.