def test_from_source(self): from otp.ai.passlib.totp import TOTP from_source = TOTP.from_source otp = from_source( u('otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example' )) self.assertEqual(otp.key, KEY4_RAW) otp = from_source( 'otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example' ) self.assertEqual(otp.key, KEY4_RAW) otp = from_source(dict(v=1, type='totp', key=KEY4)) self.assertEqual(otp.key, KEY4_RAW) otp = from_source( u('{"v": 1, "type": "totp", "key": "JBSWY3DPEHPK3PXP"}')) self.assertEqual(otp.key, KEY4_RAW) otp = from_source( '{"v": 1, "type": "totp", "key": "JBSWY3DPEHPK3PXP"}') self.assertEqual(otp.key, KEY4_RAW) self.assertIs(from_source(otp), otp) wallet1 = AppWallet() otp1 = TOTP.using(wallet=wallet1).from_source(otp) self.assertIsNot(otp1, otp) self.assertEqual(otp1.to_dict(), otp.to_dict()) otp2 = TOTP.using(wallet=wallet1).from_source(otp1) self.assertIs(otp2, otp1) self.assertRaises(ValueError, from_source, u('foo')) self.assertRaises(ValueError, from_source, 'foo')
def test_totp_match_w_older_token(self): from otp.ai.passlib.totp import TotpMatch time = 141230981 token = '781501' otp = TOTP.using(now=lambda: time + 86400)(KEY3) result = otp.match(token, time - 30) self.assertTotpMatch(result, time=time - 30, skipped=1)
def test_using_w_period(self): self.assertEqual(TOTP(KEY1).period, 30) self.assertEqual(TOTP.using(period=63)(KEY1).period, 63) self.assertRaises(TypeError, TOTP.using, period=1.5) self.assertRaises(TypeError, TOTP.using, period='abc') self.assertRaises(ValueError, TOTP.using, period=0) self.assertRaises(ValueError, TOTP.using, period=-1)
def test_generate(self): from otp.ai.passlib.totp import TOTP otp = TOTP(new=True) time = self.randtime() result = otp.generate(time) token = result.token self.assertIsInstance(token, unicode) start_time = result.counter * 30 self.assertEqual(otp.generate(start_time + 29).token, token) self.assertNotEqual(otp.generate(start_time + 30).token, token) dt = datetime.datetime.utcfromtimestamp(time) self.assertEqual(int(otp.normalize_time(dt)), int(time)) self.assertEqual(otp.generate(dt).token, token) otp2 = TOTP.using(now=lambda: time)(key=otp.base32_key) self.assertEqual(otp2.generate().token, token) self.assertRaises(ValueError, otp.generate, -1)
def test_verify(self): from otp.ai.passlib.totp import TOTP time = 1412889861 TotpFactory = TOTP.using(now=lambda: time) source1 = dict(v=1, type='totp', key='otxl2f5cctbprpzx') match = TotpFactory.verify('332136', source1) self.assertTotpMatch(match, time=time) source1 = dict(v=1, type='totp', key='otxl2f5cctbprpzx') self.assertRaises(exc.InvalidTokenError, TotpFactory.verify, '332155', source1) source1 = dict(v=1, type='totp') self.assertRaises(ValueError, TotpFactory.verify, '332155', source1) source1json = '{"v": 1, "type": "totp", "key": "otxl2f5cctbprpzx"}' match = TotpFactory.verify('332136', source1json) self.assertTotpMatch(match, time=time) source1uri = 'otpauth://totp/Label?secret=otxl2f5cctbprpzx' match = TotpFactory.verify('332136', source1uri) self.assertTotpMatch(match, time=time)
def test_to_dict(self): otp = TOTP(KEY4, alg='sha1', digits=6, period=30) self.assertEqual(otp.to_dict(), dict(v=1, type='totp', key=KEY4)) otp = TOTP(KEY4, alg='sha1', digits=6, period=30, label='*****@*****.**', issuer='Example Org') self.assertEqual( otp.to_dict(), dict(v=1, type='totp', key=KEY4, label='*****@*****.**', issuer='Example Org')) otp = TOTP(KEY4, alg='sha1', digits=6, period=30, label='*****@*****.**') self.assertEqual( otp.to_dict(), dict(v=1, type='totp', key=KEY4, label='*****@*****.**')) otp = TOTP(KEY4, alg='sha1', digits=6, period=30, issuer='Example Org') self.assertEqual( otp.to_dict(), dict(v=1, type='totp', key=KEY4, issuer='Example Org')) TotpFactory = TOTP.using(issuer='Example Org') otp = TotpFactory(KEY4) self.assertEqual(otp.to_dict(), dict(v=1, type='totp', key=KEY4)) otp = TotpFactory(KEY4, issuer='Example Org') self.assertEqual(otp.to_dict(), dict(v=1, type='totp', key=KEY4)) self.assertEqual( TOTP(KEY4, alg='sha256').to_dict(), dict(v=1, type='totp', key=KEY4, alg='sha256')) self.assertEqual( TOTP(KEY4, digits=8).to_dict(), dict(v=1, type='totp', key=KEY4, digits=8)) self.assertEqual( TOTP(KEY4, period=63).to_dict(), dict(v=1, type='totp', key=KEY4, period=63))
def test_normalize_time(self): TotpFactory = TOTP.using() otp = self.randotp(TotpFactory) for _ in range(10): time = self.randtime() tint = int(time) self.assertEqual(otp.normalize_time(time), tint) self.assertEqual(otp.normalize_time(tint + 0.5), tint) self.assertEqual(otp.normalize_time(tint), tint) dt = datetime.datetime.utcfromtimestamp(time) self.assertEqual(otp.normalize_time(dt), tint) orig = TotpFactory.now try: TotpFactory.now = staticmethod(lambda: time) self.assertEqual(otp.normalize_time(None), tint) finally: TotpFactory.now = orig self.assertRaises(TypeError, otp.normalize_time, '1234') return
def test_using_w_now(self): otp = self.randotp() self.assertIs(otp.now, _time.time) self.assertAlmostEqual(otp.normalize_time(None), int(_time.time())) counter = [123.12] def now(): counter[0] += 1 return counter[0] otp = self.randotp(cls=TOTP.using(now=now)) self.assertEqual(otp.normalize_time(None), 126) self.assertEqual(otp.normalize_time(None), 127) self.assertRaises(TypeError, TOTP.using, now=123) msg_re = 'now\\(\\) function must return non-negative' self.assertRaisesRegex(AssertionError, msg_re, TOTP.using, now=lambda: 'abc') self.assertRaisesRegex(AssertionError, msg_re, TOTP.using, now=lambda: -1) return
def test_totp_match_w_invalid_token(self): time = 141230981 token = '781501' otp = TOTP.using(now=lambda: time + 86400)(KEY3) self.assertRaises(exc.InvalidTokenError, otp.match, token, time + 60)
def test_totp_match_w_new_token(self): time = 141230981 token = '781501' otp = TOTP.using(now=lambda: time + 86400)(KEY3) result = otp.match(token, time + 30) self.assertTotpMatch(result, time=time + 30, skipped=-1)
def test_normalize_token_class(self): self.test_normalize_token_instance(otp=TOTP.using(digits=7))