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 # Sign in via Facebook
class OAuth2SignUpView(OAuth2ViewMixin, JWTCreateAPIView): class OAuth2SignUpView(OAuth2ViewMixin, JWTAuthViewMixin):
""" """
Implements an endpoint to convert a provider token to an access token Implements an endpoint to convert a provider token to an access token
@ -211,7 +211,7 @@ class SignUpView(JWTCreateAPIView):
# Login by username|email + password # Login by username|email + password
class LoginByUsernameOrEmailView(JWTCreateAPIView): class LoginByUsernameOrEmailView(JWTAuthViewMixin):
"""Login by email and password""" """Login by email and password"""
permission_classes = (permissions.AllowAny,) permission_classes = (permissions.AllowAny,)
serializer_class = serializers.LoginByUsernameOrEmailSerializer serializer_class = serializers.LoginByUsernameOrEmailSerializer
@ -232,7 +232,8 @@ class RefreshTokenView(JWTGenericViewMixin):
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
response = Response(serializer.data, status=status.HTTP_201_CREATED) 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: except utils_exceptions.LocaleNotExisted:
raise utils_exceptions.LocaleNotExisted(locale=_locale) raise utils_exceptions.LocaleNotExisted(locale=_locale)
else: else:
@ -244,13 +245,23 @@ class RefreshTokenView(JWTGenericViewMixin):
# Logout # Logout
class LogoutView(generics.CreateAPIView): class LogoutView(JWTAuthViewMixin):
"""Logout user""" """Logout user"""
serializer_class = serializers.LogoutSerializer serializer_class = serializers.LogoutSerializer
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
"""Override create method""" """Override create method"""
serializer = self.get_serializer(data=request.data) _locale = request.COOKIES.get('locale')
serializer.is_valid(raise_exception=True) try:
serializer.save() locale = self._check_locale(locale=_locale)
return Response(status=status.HTTP_200_OK)
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() raise exceptions.LocaleNotExisted()
return locale 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. CHECK locale in cookies and PUT access and refresh tokens there.
cookies it is list that contain namedtuples cookies it is list that contain namedtuples
@ -40,15 +42,17 @@ class JWTGenericViewMixin(generics.GenericAPIView):
secure=False) secure=False)
# Write to cookie access and refresh token with secure flag # Write to cookie access and refresh token with secure flag
_access_token = self.COOKIE(key='access_token', if access_token and refresh_token:
value=access_token, _access_token = self.COOKIE(key='access_token',
http_only=self.ACCESS_TOKEN_HTTP_ONLY, value=access_token,
secure=self.ACCESS_TOKEN_SECURE) http_only=self.ACCESS_TOKEN_HTTP_ONLY,
_refresh_token = self.COOKIE(key='refresh_token', secure=self.ACCESS_TOKEN_SECURE)
value=refresh_token, _refresh_token = self.COOKIE(key='refresh_token',
http_only=self.REFRESH_TOKEN_HTTP_ONLY, value=refresh_token,
secure=self.REFRESH_TOKEN_SECURE) http_only=self.REFRESH_TOKEN_HTTP_ONLY,
COOKIES.extend((_locale, _access_token, _refresh_token)) secure=self.REFRESH_TOKEN_SECURE)
COOKIES.extend((_access_token, _refresh_token))
COOKIES.append(_locale)
return COOKIES return COOKIES
def _put_cookies_in_response(self, cookies: list, response: Response): def _put_cookies_in_response(self, cookies: list, response: Response):
@ -93,8 +97,8 @@ class JWTCreateAPIView(JWTGenericViewMixin, generics.CreateAPIView):
else: else:
return self._put_cookies_in_response( return self._put_cookies_in_response(
cookies=self._put_data_in_cookies(locale=locale, cookies=self._put_data_in_cookies(locale=locale,
access_token=access_token, access_token=access_token.value,
refresh_token=refresh_token), refresh_token=refresh_token.value),
response=response) response=response)