Skip to content

Commit

Permalink
Merge 69f0dd8 into 9527b88
Browse files Browse the repository at this point in the history
  • Loading branch information
st4lk committed Mar 12, 2019
2 parents 9527b88 + 69f0dd8 commit 0b69926
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 169 deletions.
47 changes: 38 additions & 9 deletions README.md
Expand Up @@ -17,7 +17,7 @@ Requirements
- djangorestframework (>=3.1, <4.0)
- social-auth-core (>=3.0, <4.0)
- social-auth-app-django (>=3.1, <4.0)
- [optional] djangorestframework-jwt (>=1.7.2)
- [optional] djangorestframework-simplejwt (>=4.0.0)
- [optional] django-rest-knox (>=3.2.0)

Release notes
Expand Down Expand Up @@ -85,19 +85,33 @@ Quick start

4.1 [session authentication](http://www.django-rest-framework.org/api-guide/authentication/#sessionauthentication)

url(r'^api/login/', include('rest_social_auth.urls_session')),
```python
url(r'^api/login/', include('rest_social_auth.urls_session')),
```

4.2 [token authentication](http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication)

url(r'^api/login/', include('rest_social_auth.urls_token')),
```python
url(r'^api/login/', include('rest_social_auth.urls_token')),
```

4.3 [jwt authentication](https://github.com/davesque/django-rest-framework-simplejwt)

4.3 [jwt authentication](http://getblimp.github.io/django-rest-framework-jwt/)
```python
url(r'^api/login/', include('rest_social_auth.urls_jwt_pair')),
```

url(r'^api/login/', include('rest_social_auth.urls_jwt')),
or / and

```python
url(r'^api/login/', include('rest_social_auth.urls_jwt_sliding')),
```

4.4 [knox authentication](https://github.com/James1345/django-rest-knox/)

url(r'^api/login/', include('rest_social_auth.urls_knox')),
```python
url(r'^api/login/', include('rest_social_auth.urls_knox')),
```

5. You are ready to login users

Expand Down Expand Up @@ -161,14 +175,29 @@ Quick start

5.3 jwt authentication

- POST /api/login/social/jwt/
- POST /api/login/social/jwt_user/
- POST /api/login/social/jwt-pair/
- POST /api/login/social/jwt-pair-user/

Similar to token authentication, but token is JSON Web Token.

See [JWT.io](http://jwt.io/) for details.

To use it, [django-rest-framework-jwt](https://github.com/GetBlimp/django-rest-framework-jwt) must be installed.
To use it, [django-rest-framework-simplejwt](https://github.com/davesque/django-rest-framework-simplejwt) must be installed.

For `jwt-pair`, the response will include additional "refresh" token:
```json
{
"token": "...",
"refresh": "..."
}
```

##### Or sliding JWT token:

- POST /api/login/social/jwt-sliding/
- POST /api/login/social/jwt-sliding-user/

Check [docs of simplejwt](https://github.com/davesque/django-rest-framework-simplejwt#token-types) for pair/sliding token difference.

5.4 knox authentication

Expand Down
6 changes: 3 additions & 3 deletions example_project/config/urls.py
Expand Up @@ -14,15 +14,15 @@

url(r'^api/login/', include('rest_social_auth.urls_session')),
url(r'^api/login/', include('rest_social_auth.urls_token')),
url(r'^api/login/', include('rest_social_auth.urls_simplejwt_pair')),
url(r'^api/login/', include('rest_social_auth.urls_simplejwt_sliding')),
url(r'^api/login/', include('rest_social_auth.urls_jwt_pair')),
url(r'^api/login/', include('rest_social_auth.urls_jwt_sliding')),
url(r'^api/login/', include('rest_social_auth.urls_jwt')),
url(r'^api/login/', include('rest_social_auth.urls_knox')),

url(r'^api/logout/session/$', views.LogoutSessionView.as_view(), name='logout_session'),
url(r'^api/user/session/', views.UserSessionDetailView.as_view(), name="current_user_session"),
url(r'^api/user/token/', views.UserTokenDetailView.as_view(), name="current_user_token"),
url(r'^api/user/jwt/', views.UserSimpleJWTDetailView.as_view(), name="current_user_jwt"),
url(r'^api/user/jwt/', views.UserJWTDetailView.as_view(), name="current_user_jwt"),
url(r'^api/user/jwt-old/', views.UserJWTOldDetailView.as_view(), name="current_user_jwt_old"),
url(r'^api/user/knox/', views.UserKnoxDetailView.as_view(), name="current_user_knox"),
]
Expand Down
6 changes: 3 additions & 3 deletions example_project/templates/home_jwt.html
Expand Up @@ -74,16 +74,16 @@ <h2>Raw JWT payload</h2>
angular.module('JWTApp', ['satellizer'])
.config(function($authProvider) {
$authProvider.facebook({
url: "{% url 'login_social_simplejwt_pair_user' provider='facebook' %}",
url: "{% url 'login_social_jwt_pair_user' provider='facebook' %}",
clientId: '{{ facebook_key }}'
});
$authProvider.google({
url: "{% url 'login_social_simplejwt_pair_user' provider='google-oauth2' %}",
url: "{% url 'login_social_jwt_pair_user' provider='google-oauth2' %}",
clientId: '{{ googleoauth2_key }}',
redirectUri: window.location.origin + '/'
});
$authProvider.twitter({
url: "{% url 'login_social_simplejwt_pair_user' provider='twitter' %}",
url: "{% url 'login_social_jwt_pair_user' provider='twitter' %}",
});
$authProvider.authToken = 'JWT';
$authProvider.tokenPrefix = 'satellizer_simple_jwt'; // to not collide with regular token auth
Expand Down
2 changes: 1 addition & 1 deletion example_project/users/views.py
Expand Up @@ -63,7 +63,7 @@ class UserJWTOldDetailView(JWTAuthMixin, BaseDetailView):
pass


class UserSimpleJWTDetailView(SimpleJWTAuthMixin, BaseDetailView):
class UserJWTDetailView(SimpleJWTAuthMixin, BaseDetailView):
pass


Expand Down
76 changes: 41 additions & 35 deletions rest_social_auth/serializers.py
Expand Up @@ -43,36 +43,6 @@ class UserTokenSerializer(TokenSerializer, UserSerializer):
pass


class JWTSerializer(TokenSerializer):

def get_token(self, obj):
try:
from rest_framework_jwt.settings import api_settings
except ImportError:
warnings.warn('djangorestframework-jwt must be installed for JWT authentication',
ImportWarning)
raise

jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

payload = jwt_payload_handler(self.get_jwt_payload(obj))
token = jwt_encode_handler(payload)

return token

def get_jwt_payload(self, obj):
"""
Define here, what data shall be encoded in JWT.
By default, entire object will be encoded.
"""
return obj


class UserJWTSerializer(JWTSerializer, UserSerializer):
pass


class KnoxSerializer(TokenSerializer):
def get_token(self, obj):
try:
Expand All @@ -89,7 +59,7 @@ class UserKnoxSerializer(KnoxSerializer, UserSerializer):
pass


class SimpleJWTBaseSerializer(serializers.Serializer):
class JWTBaseSerializer(serializers.Serializer):

jwt_token_class_name = None

Expand Down Expand Up @@ -120,7 +90,7 @@ def get_token_payload(self, user):
return {}


class SimpleJWTPairSerializer(SimpleJWTBaseSerializer):
class JWTPairSerializer(JWTBaseSerializer):
token = serializers.SerializerMethodField()
refresh = serializers.SerializerMethodField()

Expand All @@ -133,15 +103,15 @@ def get_refresh(self, obj):
return str(self.get_token_instance())


class UserSimplePairJWTSerializer(SimpleJWTPairSerializer, UserSerializer):
class UserJWTPairSerializer(JWTPairSerializer, UserSerializer):

def get_token_payload(self, user):
payload = dict(UserSerializer(user).data)
payload.pop('id', None)
return payload


class SimpleJWTSlidingSerializer(SimpleJWTBaseSerializer):
class JWTSlidingSerializer(JWTBaseSerializer):
token = serializers.SerializerMethodField()

jwt_token_class_name = 'SlidingToken'
Expand All @@ -150,9 +120,45 @@ def get_token(self, obj):
return str(self.get_token_instance())


class UserSimpleJWTSlidingSerializer(SimpleJWTSlidingSerializer, UserSerializer):
class UserJWTSlidingSerializer(JWTSlidingSerializer, UserSerializer):

def get_token_payload(self, user):
payload = dict(UserSerializer(user).data)
payload.pop('id', None)
return payload


# Depcreated Seraizlisers
class JWTSerializer(TokenSerializer):

def get_token(self, obj):
warnings.warn(
'Support of djangorestframework-jwt will be removed in 3.0.0 version. '
'Use rest_framework_simplejwt instead.',
DeprecationWarning,
)
try:
from rest_framework_jwt.settings import api_settings
except ImportError:
warnings.warn('djangorestframework-jwt must be installed for JWT authentication',
ImportWarning)
raise

jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

payload = jwt_payload_handler(self.get_jwt_payload(obj))
token = jwt_encode_handler(payload)

return token

def get_jwt_payload(self, obj):
"""
Define here, what data shall be encoded in JWT.
By default, entire object will be encoded.
"""
return obj


class UserJWTSerializer(JWTSerializer, UserSerializer):
pass
Expand Up @@ -6,10 +6,10 @@
urlpatterns = (
# returns token only
url(r'^social/jwt-pair/(?:(?P<provider>[a-zA-Z0-9_-]+)/?)?$',
views.SocialSimpleJWTPairOnlyAuthView.as_view(),
name='login_social_simplejwt_pair'),
views.SocialJWTPairOnlyAuthView.as_view(),
name='login_social_jwt_pair'),
# returns token + user_data
url(r'^social/jwt-pair-user/(?:(?P<provider>[a-zA-Z0-9_-]+)/?)?$',
views.SocialSimpleJWTPairUserAuthView.as_view(),
name='login_social_simplejwt_pair_user'),
views.SocialJWTPairUserAuthView.as_view(),
name='login_social_jwt_pair_user'),
)
Expand Up @@ -6,10 +6,10 @@
urlpatterns = (
# returns token only
url(r'^social/jwt-sliding/(?:(?P<provider>[a-zA-Z0-9_-]+)/?)?$',
views.SocialSimpleJWTSlidingOnlyAuthView.as_view(),
name='login_social_simplejwt_sliding'),
views.SocialJWTSlidingOnlyAuthView.as_view(),
name='login_social_jwt_sliding'),
# returns token + user_data
url(r'^social/jwt-sliding-user/(?:(?P<provider>[a-zA-Z0-9_-]+)/?)?$',
views.SocialSimpleJWTSlidingUserAuthView.as_view(),
name='login_social_simplejwt_sliding_user'),
views.SocialJWTSlidingUserAuthView.as_view(),
name='login_social_jwt_sliding_user'),
)
75 changes: 38 additions & 37 deletions rest_social_auth/views.py
Expand Up @@ -27,18 +27,18 @@
from requests.exceptions import HTTPError

from .serializers import (
JWTPairSerializer,
JWTSerializer,
JWTSlidingSerializer,
KnoxSerializer,
OAuth1InputSerializer,
OAuth2InputSerializer,
SimpleJWTPairSerializer,
SimpleJWTSlidingSerializer,
TokenSerializer,
UserJWTSerializer,
UserJWTSlidingSerializer,
UserKnoxSerializer,
UserJWTPairSerializer,
UserSerializer,
UserSimpleJWTSlidingSerializer,
UserSimplePairJWTSerializer,
UserTokenSerializer,
)

Expand Down Expand Up @@ -228,31 +228,6 @@ class SocialTokenUserAuthView(BaseSocialAuthView):
authentication_classes = (TokenAuthentication, )


class JWTAuthMixin(object):
def get_authenticators(self):
warnings.warn(
'Support of djangorestframework-jwt will be removed in 3.0.0 version. '
'Use rest_framework_simplejwt instead.',
DeprecationWarning,
)
try:
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
except ImportError:
warnings.warn('djangorestframework-jwt must be installed for JWT authentication',
ImportWarning)
raise

return [JSONWebTokenAuthentication()]


class SocialJWTOnlyAuthView(JWTAuthMixin, BaseSocialAuthView):
serializer_class = JWTSerializer


class SocialJWTUserAuthView(JWTAuthMixin, BaseSocialAuthView):
serializer_class = UserJWTSerializer


class KnoxAuthMixin(object):
def get_authenticators(self):
try:
Expand Down Expand Up @@ -286,17 +261,43 @@ def get_authenticators(self):
return [JWTAuthentication()]


class SocialSimpleJWTPairOnlyAuthView(SimpleJWTAuthMixin, BaseSocialAuthView):
serializer_class = SimpleJWTPairSerializer
class SocialJWTPairOnlyAuthView(SimpleJWTAuthMixin, BaseSocialAuthView):
serializer_class = JWTPairSerializer


class SocialSimpleJWTPairUserAuthView(SimpleJWTAuthMixin, BaseSocialAuthView):
serializer_class = UserSimplePairJWTSerializer
class SocialJWTPairUserAuthView(SimpleJWTAuthMixin, BaseSocialAuthView):
serializer_class = UserJWTPairSerializer


class SocialSimpleJWTSlidingOnlyAuthView(SimpleJWTAuthMixin, BaseSocialAuthView):
serializer_class = SimpleJWTSlidingSerializer
class SocialJWTSlidingOnlyAuthView(SimpleJWTAuthMixin, BaseSocialAuthView):
serializer_class = JWTSlidingSerializer


class SocialSimpleJWTSlidingUserAuthView(SimpleJWTAuthMixin, BaseSocialAuthView):
serializer_class = UserSimpleJWTSlidingSerializer
class SocialJWTSlidingUserAuthView(SimpleJWTAuthMixin, BaseSocialAuthView):
serializer_class = UserJWTSlidingSerializer


# Deprecated views
class JWTAuthMixin(object):
def get_authenticators(self):
warnings.warn(
'Support of djangorestframework-jwt will be removed in 3.0.0 version. '
'Use rest_framework_simplejwt instead.',
DeprecationWarning,
)
try:
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
except ImportError:
warnings.warn('djangorestframework-jwt must be installed for JWT authentication',
ImportWarning)
raise

return [JSONWebTokenAuthentication()]


class SocialJWTOnlyAuthView(JWTAuthMixin, BaseSocialAuthView):
serializer_class = JWTSerializer


class SocialJWTUserAuthView(JWTAuthMixin, BaseSocialAuthView):
serializer_class = UserJWTSerializer

0 comments on commit 0b69926

Please sign in to comment.