Ejemplo n.º 1
0
    def verify(self, otp, for_time=None):
        """
        Verifies the OTP passed in against the current time OTP
        @param [String/Integer] otp the OTP to check against
        """
        if for_time is None:
            for_time = datetime.datetime.now()

        return utils.strings_equal(unicode(otp), unicode(self.at(for_time))) + \
               utils.strings_equal(unicode(otp), unicode(self.at(for_time+datetime.timedelta(0,30)))) \
               >= 1
Ejemplo n.º 2
0
    def verify(self, otp, for_time=None, valid_window=0):
        """
        Verifies the OTP passed in against the current time OTP
        @param [String/Integer] otp the OTP to check against
        @param [Integer] valid_window extends the validity to this many counter ticks before and after the current one
        """
        if for_time is None:
            for_time = datetime.datetime.now()
        
        if valid_window:
            for i in range(-valid_window, valid_window + 1):
                if utils.strings_equal(str(otp), str(self.at(for_time, i))):
                    return True
            return False

        return utils.strings_equal(str(otp), str(self.at(for_time)))
Ejemplo n.º 3
0
 def verify(self, otp, counter):
     """
     Verifies the OTP passed in against the current time OTP
     @param [String/Integer] otp the OTP to check against
     @param [Integer] counter the counter of the OTP
     """
     return utils.strings_equal(str(otp), str(self.at(counter)))
Ejemplo n.º 4
0
    def verify(self, otp, for_time=None, valid_window=0):
        """
        Verifies the OTP passed in against the current time OTP
        @param [String/Integer] otp the OTP to check against
        @param [Integer] valid_window extends the validity to this many counter ticks before and after the current one
        """
        if for_time is None:
            for_time = datetime.datetime.now()

        if valid_window:
            for i in range(-valid_window, valid_window + 1):
                if utils.strings_equal(str(otp), str(self.at(for_time, i))):
                    return True
            return False

        return utils.strings_equal(str(otp), str(self.at(for_time)))
Ejemplo n.º 5
0
Archivo: hotp.py Proyecto: Dubz/pyotp
 def verify(self, otp, counter):
     """
     Verifies the OTP passed in against the current time OTP
     @param [String/Integer] otp the OTP to check against
     @param [Integer] counter the counter of the OTP
     """
     return utils.strings_equal(unicode(otp), unicode(self.at(counter)))
Ejemplo n.º 6
0
    def verify(self, otp, for_time=None):
        """
        Verifies the OTP passed in against the current time OTP
        @param [String/Integer] otp the OTP to check against
        """
        if for_time is None:
            for_time = datetime.datetime.now()

        return utils.strings_equal(str(otp), str(self.at(for_time)))
Ejemplo n.º 7
0
 def check_totp_code(self, code):
     totp = pyotp.TOTP(self.totp_key)
     now_timecode = totp.timecode(timezone.now())
     min_timecode = max(self.last_totp_timecode + 1, now_timecode - settings.DMOJ_TOTP_TOLERANCE_HALF_MINUTES)
     for timecode in range(min_timecode, now_timecode + settings.DMOJ_TOTP_TOLERANCE_HALF_MINUTES + 1):
         if strings_equal(code, totp.generate_otp(timecode)):
             self.last_totp_timecode = timecode
             self.save(update_fields=['last_totp_timecode'])
             return True
     return False
Ejemplo n.º 8
0
    def verify_former(self, otp, for_time=None, valid_window=0):
        """
        Verifies the OTP passed in against the current and former time OTP
        @return [Integer] the OTP as an integer
        """
        if for_time is None:
            for_time = datetime.datetime.now()
        for_time_former = (for_time - datetime.timedelta(seconds=30))
        for_time_former = time.mktime(for_time_former.timetuple())

        if valid_window:
            for i in range(-valid_window, valid_window + 1):
                if utils.strings_equal(str(otp), str(self.at(for_time, i))) or \
                        utils.strings_equal(str(otp), str(self.at(for_time_former, i))):
                    return True
            return False

        return utils.strings_equal(str(otp), str(self.at(for_time))) or \
               utils.strings_equal(str(otp), str(self.at(for_time_former)))
Ejemplo n.º 9
0
    def verify_former(self, otp, for_time=None, valid_window=0):
        """
        Verifies the OTP passed in against the current and former time OTP
        @return [Integer] the OTP as an integer
        """
        if for_time is None:
            for_time = datetime.datetime.now()
        for_time_former = (for_time - datetime.timedelta(seconds=30))
        for_time_former = time.mktime(for_time_former.timetuple())

        if valid_window:
            for i in range(-valid_window, valid_window + 1):
                if utils.strings_equal(str(otp), str(self.at(for_time, i))) or \
                        utils.strings_equal(str(otp), str(self.at(for_time_former, i))):
                    return True
            return False

        return utils.strings_equal(str(otp), str(self.at(for_time))) or \
               utils.strings_equal(str(otp), str(self.at(for_time_former)))
Ejemplo n.º 10
0
def verify_token(secret_key, token):
    """Validates token considering past tokens"""
    if not token:
        return False

    totp = pyotp.totp.TOTP(secret_key)

    for_time = datetime.datetime.now()

    for i in range(-config.two_factor_valid_past_tokens_count, 1):
        if strings_equal(str(token), str(totp.at(for_time, i))):
            return True
    return False