コード例 #1
0
ファイル: views.py プロジェクト: rasouliali1379/backend
    def post(self, request):
        try:
            serialized_data = self.serializer_class(data=request.data)
            if serialized_data.is_valid(raise_exception=True):
                email = serialized_data.data['email'].lower()
                try:
                    user = User.objects.get(profile__email=email,
                                            profile__email_confirmed=True)
                except User.DoesNotExist:
                    raise authnz_exceptions.CustomException(
                        detail=_('Email is invalid or not confirmed'))

                if user.check_password(serialized_data.data['password']):
                    if user.is_active:
                        payload = jwt_payload_handler(
                            user)  # todo: Is deprecated
                        jwt_token = utilities.jwt_response_payload_handler(
                            jwt_encode_handler(payload), user=user)
                        return responses.SuccessResponse(jwt_token).send()
                    else:
                        raise authnz_exceptions.CustomException(
                            detail=_('This user is inactive, contact us.'))
                else:
                    raise authnz_exceptions.CustomException(
                        detail=_('Email or Password is invalid.'))
        except authnz_exceptions.CustomException as e:
            return responses.ErrorResponse(message=e.detail,
                                           status=e.status_code).send()
        except exceptions.ValidationError as e:
            return responses.ErrorResponse(message=e.detail,
                                           status=e.status_code).send()
コード例 #2
0
ファイル: views.py プロジェクト: rasouliali1379/backend
 def post(self, request):
     try:
         serialized_data = self.serializer_class(data=request.data)
         if serialized_data.is_valid(raise_exception=True):
             if request.user.check_password(
                     serialized_data.data['old_password']):
                 if request.user.is_active:
                     transactions.change_user_password(
                         request.user, serialized_data.data['password'])
                     payload = jwt_payload_handler(
                         request.user)  # todo: Is deprecated
                     jwt_token = utilities.jwt_response_payload_handler(
                         jwt_encode_handler(payload), user=request.user)
                     return responses.SuccessResponse(jwt_token).send()
                 else:
                     raise authnz_exceptions.CustomException(
                         detail=_('This user is deactivated, contact us.'))
             else:
                 raise authnz_exceptions.CustomException(
                     detail=_('Old Password is invalid.'))
     except authnz_exceptions.CustomException as e:
         return responses.ErrorResponse(message=e.detail,
                                        status=e.status_code).send()
     except exceptions.ValidationError as e:
         return responses.ErrorResponse(message=e.detail,
                                        status=e.status_code).send()
コード例 #3
0
ファイル: views.py プロジェクト: sirramin/backend
 def post(self, request, backend, *args, **kwargs):
     try:
         serialized_data = self.serializer_class(data=request.data)
         if serialized_data.is_valid(raise_exception=True):
             token = serialized_data.data['token']
             if backend.lower() == 'google':
                 try:
                     resp_user = id_token.verify_oauth2_token(token, google_requests.Request(),
                                                              settings.GOOGLE_OAUTH_ID)
                 except Exception as e:
                     return responses.ErrorResponse(message='Error in google open auth',
                                                    dev_error=str(e), status=400).send()
                 if resp_user['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
                     raise authnz_exceptions.CustomException(detail=_('Google Wrong issuer.'))
                 if not resp_user.get('email') or not resp_user.get('given_name') or \
                         not resp_user.get('family_name') or not resp_user.get('picture'):
                     raise authnz_exceptions.CustomException(
                         detail=_('Scope need to have email, given name, family, picture'))
                 email = resp_user['email'].lower()
                 try:
                     user = User.objects.get(profile__email=email)
                 except User.DoesNotExist as e:
                     user = transactions.open_auth_user_creator(email, resp_user['given_name'],
                                                                resp_user['family_name'], resp_user['picture'])
             else:
                 raise authnz_exceptions.CustomException(detail=_('Wrong backend'))
         if user.is_active:
             payload = jwt_payload_handler(user)  # todo: Is deprecated
             jwt_token = utilities.jwt_response_payload_handler(jwt_encode_handler(payload), user=user)
         else:
             raise authnz_exceptions.CustomException(
                         detail=_('Your user account is deactivated, contact us for more information.'))
         return responses.SuccessResponse(jwt_token).send()
     except authnz_exceptions.CustomException as e:
         return responses.ErrorResponse(message=e.detail, status=e.status_code).send()
コード例 #4
0
ファイル: views.py プロジェクト: sirramin/backend
    def post(self, request):
        try:
            serialize_data = self.serializer_class(data=request.data)
            if serialize_data.is_valid(raise_exception=True):
                email = serialize_data.data['email'].lower()
                try:
                    user = User.objects.get(profile__email=email)
                except User.DoesNotExist:
                    raise authnz_exceptions.CustomException(detail=_('Email does not exist.'))

                if user.is_active:
                    forgot_password_token = cache.get('{}{}'.format(user.username,
                                                                    settings.CACHE_FORGOT_PASSWORD_TOKEN))
                    if forgot_password_token == serialize_data.data['token']:
                        transactions.change_user_password(user, serialize_data.data['password'])
                        cache.delete('{}{}'.format(user.username, settings.CACHE_FORGOT_PASSWORD_TOKEN))
                        payload = jwt_payload_handler(user)  # todo: Is deprecated
                        jwt_token = utilities.jwt_response_payload_handler(jwt_encode_handler(payload),
                                                                           user=user)
                        return responses.SuccessResponse(jwt_token).send()
                    elif not forgot_password_token:
                        raise authnz_exceptions.CustomException(detail=_('Token timeout.'))
                    else:
                        raise authnz_exceptions.CustomException(detail=_('We sent a new token recently please try it.'))
                else:
                    raise authnz_exceptions.CustomException(detail=_('Your account is inactive.'))
        except exceptions.ValidationError as e:
            return responses.ErrorResponse(message=e.detail, status=e.status_code).send()
コード例 #5
0
ファイル: views.py プロジェクト: sirramin/backend
    def post(self, request):
        try:
            serialize_data = self.serializer_class(data=request.data)
            if serialize_data.is_valid(raise_exception=True):
                email = serialize_data.data['email'].lower()
                try:
                    user = User.objects.get(profile__email=email)
                except User.DoesNotExist:
                    raise authnz_exceptions.CustomException(detail=_('Email does not exist.'))
                if user.is_active and user.email:
                    forgot_password_token = cache.get('{}{}'.format(user.username,
                                                                    settings.CACHE_FORGOT_PASSWORD_TOKEN))
                    if not forgot_password_token:
                        permissions.check_send_email_permission(email)
                        forgot_password_token = utilities.forgot_password_delete_account_token_generator()
                        utilities.send_password_forget_token_email(user, request, forgot_password_token)
                        cache.set('{}{}'.format(user.username, settings.CACHE_FORGOT_PASSWORD_TOKEN),
                                  forgot_password_token, timeout=settings.TIMEOUT_FORGOT_PASSWORD_TOKEN)
                        return responses.SuccessResponse(message=_('Check Your email for token.')).send()
                    else:
                        raise authnz_exceptions.CustomException(detail=_('We sent an token recently please try later'))
                elif not user.is_active:
                    raise authnz_exceptions.CustomException(detail=_('This account is inactive.'))
                else:
                    raise authnz_exceptions.CustomException(detail=_('This account has no email.'))

        except exceptions.ValidationError as e:
            return responses.ErrorResponse(message=e.detail, status=e.status_code).send()
        except Exception as e:
            return responses.ErrorResponse(message=str(e)).send()
コード例 #6
0
ファイル: views.py プロジェクト: rasouliali1379/backend
 def post(self, request, backend, *args, **kwargs):
     try:
         if request.user.profile.email and request.user.profile.email_confirmed:
             return responses.ErrorResponse(
                 message=_('This email used before.')).send()
         serialized_data = self.serializer_class(data=request.data)
         if serialized_data.is_valid(raise_exception=True):
             token = serialized_data.data['token']
             if backend.lower() == 'google':
                 try:
                     resp_user = id_token.verify_oauth2_token(
                         token, google_requests.Request(),
                         settings.GOOGLE_OAUTH_ID)
                 except Exception as e:
                     return responses.ErrorResponse(
                         message='Error in google open auth',
                         dev_error=str(e),
                         status=400).send()
                 if resp_user['iss'] not in [
                         'accounts.google.com',
                         'https://accounts.google.com'
                 ]:
                     raise authnz_exceptions.CustomException(
                         detail=_('Google Wrong issuer.'))
                 if not resp_user.get('email') or not resp_user.get('given_name') or \
                         not resp_user.get('family_name') or not resp_user.get('picture'):
                     raise authnz_exceptions.CustomException(detail=_(
                         'Scope need to have email, given name, family, picture'
                     ))
                 email = resp_user['email'].lower()
                 try:
                     user = User.objects.get(profile__email=email)
                 except User.DoesNotExist as e:
                     user = None
                 if user:
                     raise authnz_exceptions.CustomException(
                         detail=_('This email was used before.'))
                 else:
                     request.user.profile.email = email
                     request.user.profile.email_confirmed = True
                     request.user.save()
             else:
                 raise authnz_exceptions.CustomException(
                     detail=_('Wrong backend'))
         return responses.SuccessResponse().send()
     except authnz_exceptions.CustomException as e:
         return responses.ErrorResponse(message=e.detail,
                                        status=e.status_code).send()
コード例 #7
0
ファイル: views.py プロジェクト: rasouliali1379/backend
    def post(self, request):
        try:
            serialized_data = self.serializer_class(data=request.data)
            if serialized_data.is_valid(raise_exception=True):
                email = serialized_data.data['email'].lower()
                try:
                    user = User.objects.get(profile__email=email)
                except User.DoesNotExist as e:
                    user = None

                if user and user.profile.email_confirmed:
                    raise authnz_exceptions.CustomException(
                        detail=_('This email is registered before.'))
                elif user:
                    permissions.check_send_email_permission(email)
                    user.set_password(serialized_data.data['password'])
                    user.save()
                    utilities.send_email_confirm(user, request)
                    return responses.SuccessResponse().send()
                else:
                    password = serialized_data.data['password']
                    user = transactions.register_user_with_email_and_password(
                        email, password)
                    utilities.send_email_confirm(user, request)
                    return responses.SuccessResponse(
                        message=_('Check your email box.')).send()
        except authnz_exceptions.CustomException as e:
            return responses.ErrorResponse(message=e.detail,
                                           status=e.status_code).send()
        except exceptions.ValidationError as e:
            return responses.ErrorResponse(message=e.detail,
                                           status=e.status_code).send()
コード例 #8
0
    def get(self, request):
        try:
            if request.user.is_active:
                payload = jwt_payload_handler(request.user)  # todo: Is deprecated
                jwt_token = utilities.jwt_response_payload_handler(jwt_encode_handler(payload),
                                                                   user=request.user)
                return responses.SuccessResponse(jwt_token).send()
            else:
                raise authnz_exceptions.CustomException(detail=_('This user is inactive, contact us.'))

        except authnz_exceptions.CustomException as e:
            return responses.ErrorResponse(message=e.detail, status=e.status_code).send()
コード例 #9
0
ファイル: views.py プロジェクト: sirramin/backend
 def post(self, request):
     try:
         serialized_data = self.serializer_class(data=request.data)
         if serialized_data.is_valid(raise_exception=True):
             nick_name = serialized_data.data['nick_name']
             if request.user.profile.nick_name == nick_name:
                 return responses.SuccessResponse().send()
             if Profile.objects.filter(nick_name__iexact=nick_name):
                 raise authnz_exceptions.CustomException(detail=_('This nick name exists'))
             else:
                 return responses.SuccessResponse().send()
     except authnz_exceptions.CustomException as e:
         return responses.ErrorResponse(message=e.detail, status=e.status_code).send()
     except exceptions.ValidationError as e:
         return responses.ErrorResponse(message=e.detail, status=e.status_code).send()
コード例 #10
0
    def post(self, request):
        try:
            serialized_data = self.serializer_class(data=request.data)
            if serialized_data.is_valid(raise_exception=True):
                email = serialized_data.data['email'].lower()
                try:
                    user = User.objects.get(email=email)
                except User.DoesNotExist as e:
                    user = None

                if user:
                    raise authnz_exceptions.CustomException(detail=_('This email is registered before.'))
                else:
                    password = serialized_data.data['password']
                    user = transactions.register_user_with_email_and_password(email, password)
                    return responses.SuccessResponse().send()
        except authnz_exceptions.CustomException as e:
            return responses.ErrorResponse(message=e.detail, status=e.status_code).send()
        except exceptions.ValidationError as e:
            return responses.ErrorResponse(message=e.detail, status=e.status_code).send()