version 0.0.13: bug fix

This commit is contained in:
Anatoly 2019-08-15 14:23:09 +03:00
parent 663c003119
commit 2fc2b7daed
2 changed files with 35 additions and 20 deletions

View File

@ -102,7 +102,7 @@ class OAuth2ViewMixin(CsrfExemptMixin, OAuthLibMixin, BaseOAuth2ViewMixin):
# Sign in via Facebook
class OAuth2SignUpView(OAuth2ViewMixin, JWTCreateAPIView):
class OAuth2SignUpView(OAuth2ViewMixin, JWTAuthViewMixin):
"""
Implements an endpoint to convert a provider token to an access token
@ -211,7 +211,7 @@ class SignUpView(JWTCreateAPIView):
# Login by username|email + password
class LoginByUsernameOrEmailView(JWTCreateAPIView):
class LoginByUsernameOrEmailView(JWTAuthViewMixin):
"""Login by email and password"""
permission_classes = (permissions.AllowAny,)
serializer_class = serializers.LoginByUsernameOrEmailSerializer
@ -232,7 +232,8 @@ class RefreshTokenView(JWTGenericViewMixin):
serializer.is_valid(raise_exception=True)
response = Response(serializer.data, status=status.HTTP_201_CREATED)
access_token, refresh_token = self._get_tokens_from_cookies(request)
access_token = serializer.data.get('access_token')
refresh_token = serializer.data.get('refresh_token')
except utils_exceptions.LocaleNotExisted:
raise utils_exceptions.LocaleNotExisted(locale=_locale)
else:
@ -244,13 +245,23 @@ class RefreshTokenView(JWTGenericViewMixin):
# Logout
class LogoutView(generics.CreateAPIView):
class LogoutView(JWTAuthViewMixin):
"""Logout user"""
serializer_class = serializers.LogoutSerializer
def create(self, request, *args, **kwargs):
"""Override create method"""
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(status=status.HTTP_200_OK)
_locale = request.COOKIES.get('locale')
try:
locale = self._check_locale(locale=_locale)
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
response = Response(status=status.HTTP_200_OK)
except utils_exceptions.LocaleNotExisted:
raise utils_exceptions.LocaleNotExisted(locale=_locale)
else:
return self._put_cookies_in_response(
cookies=self._put_data_in_cookies(locale=locale),
response=response)

View File

@ -25,7 +25,9 @@ class JWTGenericViewMixin(generics.GenericAPIView):
raise exceptions.LocaleNotExisted()
return locale
def _put_data_in_cookies(self, locale: str, access_token: str, refresh_token: str):
def _put_data_in_cookies(self, locale: str,
access_token: str = None,
refresh_token: str = None):
"""
CHECK locale in cookies and PUT access and refresh tokens there.
cookies it is list that contain namedtuples
@ -40,15 +42,17 @@ class JWTGenericViewMixin(generics.GenericAPIView):
secure=False)
# Write to cookie access and refresh token with secure flag
_access_token = self.COOKIE(key='access_token',
value=access_token,
http_only=self.ACCESS_TOKEN_HTTP_ONLY,
secure=self.ACCESS_TOKEN_SECURE)
_refresh_token = self.COOKIE(key='refresh_token',
value=refresh_token,
http_only=self.REFRESH_TOKEN_HTTP_ONLY,
secure=self.REFRESH_TOKEN_SECURE)
COOKIES.extend((_locale, _access_token, _refresh_token))
if access_token and refresh_token:
_access_token = self.COOKIE(key='access_token',
value=access_token,
http_only=self.ACCESS_TOKEN_HTTP_ONLY,
secure=self.ACCESS_TOKEN_SECURE)
_refresh_token = self.COOKIE(key='refresh_token',
value=refresh_token,
http_only=self.REFRESH_TOKEN_HTTP_ONLY,
secure=self.REFRESH_TOKEN_SECURE)
COOKIES.extend((_access_token, _refresh_token))
COOKIES.append(_locale)
return COOKIES
def _put_cookies_in_response(self, cookies: list, response: Response):
@ -93,8 +97,8 @@ class JWTCreateAPIView(JWTGenericViewMixin, generics.CreateAPIView):
else:
return self._put_cookies_in_response(
cookies=self._put_data_in_cookies(locale=locale,
access_token=access_token,
refresh_token=refresh_token),
access_token=access_token.value,
refresh_token=refresh_token.value),
response=response)