Beispiel #1
0
class BackendUserHelper():
    def __init__(self):
        self.firestore_adapter = FirestoreAdapter()
        self.cache_adapter = CacheAdapter()

    def create_user(self):
        """
        1. Creates a default backend user
        in backend
        2. Creates the same in Firestore
        """

        user = User.objects.create_user(
            username='******',
            avatar=
            'https://cdn4.iconfinder.com/data/icons/helpdesk-support-business/128/2_Support_helpdesk_avatar_business-20_Support_helpdesk_avatar_business-18-512.png',
            mobile_number='9999999999',
            email='*****@*****.**')

        self.firestore_adapter.create_user(user.id,
                                           user.username,
                                           mobile_number=user.mobile_number,
                                           avatar=user.avatar)

        self.cache_adapter.set(BACKEND_USER_KEY, user.id)
Beispiel #2
0
    def validate(self, attrs):
        """
        Validates if the OTP is correct for a given
        mobile number
        """
        mobile_number = attrs.get('mobile_number')
        try:
            get_user_model().objects.get(mobile_number=mobile_number)
        except ObjectDoesNotExist:
            raise Http404

        key = OTP_PREFIX + mobile_number
        param_otp = attrs.get('otp')
        cache_adapter_obj = CacheAdapter()
        cached_otp_value = cache_adapter_obj.get(key)
        user = None
        if param_otp == cached_otp_value:
            user = get_user_model().objects.get(mobile_number=mobile_number)

        if not user:
            raise serializers.ValidationError(
                {
                    'auth_failure':
                    'Unable to authenticate with provided credential'
                },
                code='authentication')

        return attrs
Beispiel #3
0
    def get_backend_user(self):
        """
        Returns the backend user created for
        support chat
        """

        cache_adapter = CacheAdapter()
        user_id = cache_adapter.get(BACKEND_USER_KEY)

        return self.get(id=user_id, is_deleted=False)
    def post(self, request):
        """
        POST API -> /api/v1/user/generate/otp
        """

        serializer = OTPGenerateSerializer(data=request.data)
        if serializer.is_valid():
            key = OTP_PREFIX + serializer.data['mobile_number']
            cache_adapter_obj = CacheAdapter()
            cache_adapter_obj.set(key, get_random_number(),
                                  OTP_EXPIRY_IN_SECONDS)

            return Response({'response': 'OTP sent'},
                            status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    def post(self, request, uid, *arg, **kwargs):
        form = self.form_class(request.POST)
        key = 'password_reset:' + uid
        cache_adapter_obj = CacheAdapter()
        email = cache_adapter_obj.get(key)
        if email is None:
            messages.error(request,
                           'The reset password link is no longer valid.')

        if form.is_valid():
            new_password = form.cleaned_data['password']
            user = get_user_model().objects.get(email=email)
            user.set_password(new_password)
            user.save()
            cache_adapter_obj.delete(key)
            messages.success(request, 'Password has been reset.')
        else:
            messages.error(request, 'Password reset has been unsuccessful.')

        return render(request, self.template_name, context={'form': form})
    def post(self, request):
        """
        POST API -> /api/v1/user/password_reset/mail
        """

        serializer = PasswordResetMailSerializer(data=request.data)
        if serializer.is_valid():
            email = serializer.data['email']
            random_string = get_random_string(30)
            key = 'password_reset:' + random_string
            cache_adapter_obj = CacheAdapter()
            cache_adapter_obj.set(key, email, EMAIL_EXPIRY_IN_SECONDS)
            link = get_domain_url(request) + reverse('user:password-reset',
                                                     args=(random_string, ))
            print(link)
            send_reset_password_email.delay(email, link)

            return Response({'response': 'Reset email sent!'},
                            status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #7
0
class OTPService():
    def __init__(self, mobile_number):
        self.mobile_number = mobile_number
        self.OTP_PREFIX = 'OTP_'
        self.OTP_EXPIRY = 600  # in seconds
        self.cache_adapter = CacheAdapter()

    def get_otp(self):
        """
        Returns the OTP stored in cache for
        the given number
        """

        key = self.OTP_PREFIX + self.mobile_number

        return self.cache_adapter.get(key)

    def clear_otp(self):
        """
        Clears the OTP from the cache
        """

        key = self.OTP_PREFIX + self.mobile_number
        self.cache_adapter.delete(key)

    def generate_otp(self):
        """
        Generates OTP for the given mobile number
        """

        key = self.OTP_PREFIX + self.mobile_number

        # Gets a new OTP if not present in cache,
        # and if it's already present, returns the same
        # OTP
        one_time_password = self.cache_adapter.get(key)
        if one_time_password is None:
            one_time_password = get_random_number()

        # setting the OTP in cache
        self.cache_adapter.set(key, one_time_password, self.OTP_EXPIRY)

        return one_time_password
def get_otp(mobile_number='1234567890'):
    obj = CacheAdapter()
    return obj.get(OTP_PREFIX + mobile_number)
def set_otp(mobile_number='1234567890', otp='123456'):
    obj = CacheAdapter()
    obj.set(OTP_PREFIX + mobile_number, otp, 120)
Beispiel #10
0
 def __init__(self):
     self.firestore_adapter = FirestoreAdapter()
     self.cache_adapter = CacheAdapter()
Beispiel #11
0
 def __init__(self, mobile_number):
     self.mobile_number = mobile_number
     self.OTP_PREFIX = 'OTP_'
     self.OTP_EXPIRY = 600  # in seconds
     self.cache_adapter = CacheAdapter()