def test_26_is_previous_otp(self): # check if the OTP was used previously serial = "previous" db_token = Token(serial, tokentype="totp") db_token.save() token = TotpTokenClass(db_token) token.set_otpkey(self.otpkey) token.set_hashlib("sha1") token.set_otplen(6) # Authenticate with the current OTP value counter = token._time2counter(time.time(), timeStepping=30) otp_now = token._calc_otp(counter) r = token.check_otp(otp_now, window=180) self.assertEqual(r, counter) # Now we try several is_previous_otp and the timeShift must stay the same! ts0 = float(token.get_tokeninfo("timeShift")) self.assertTrue(-181 < ts0 < 181) # Too old r = token.is_previous_otp(token._calc_otp(counter - 3)) self.assertEqual(r, False) ts = float(token.get_tokeninfo("timeShift")) self.assertEqual(ts, ts0) # The same OTP value r = token.is_previous_otp(otp_now) self.assertEqual(r, True) ts = float(token.get_tokeninfo("timeShift")) self.assertEqual(ts, ts0) # Future value r = token.is_previous_otp(token._calc_otp(counter + 8)) self.assertEqual(r, False) ts = float(token.get_tokeninfo("timeShift")) self.assertEqual(ts, ts0)