def test_credential_serialize_name(self): self.assertEqual('name', Credential('name').long_name()) self.assertEqual('Issuer:name', Credential('name', issuer='Issuer').long_name()) self.assertEqual( '20/Issuer:name', Credential('name', issuer='Issuer', period=20).long_name()) self.assertEqual( 'Issuer:name', Credential('name', issuer='Issuer', period=30).long_name()) self.assertEqual('20/name', Credential('name', period=20).long_name())
def _read_slot_cred(self, slot, digits, timestamp): try: code = self._read_slot_code( slot, digits, timestamp, wait_for_touch=False) return (Credential(self._slot_name(slot), OATH_TYPE.TOTP, False), code) except YkpersError as e: if e.errno == 11: return (Credential(self._slot_name(slot), OATH_TYPE.TOTP, True ), None) except Exception as e: return (Credential(str(e).encode(), OATH_TYPE.TOTP, True), None) return None
def test_credential_expiration(self): cred = Credential('name') cred.update_expiration(0) self.assertEqual(30, cred.expiration) cred.update_expiration(30) self.assertEqual(60, cred.expiration) cred.update_expiration(60) self.assertEqual(90, cred.expiration)
def calculate_slot_mode(self, slot, digits, timestamp): dev = self._descriptor.open_device(TRANSPORT.OTP) code = dev.driver.calculate( slot, challenge=timestamp, totp=True, digits=int(digits), wait_for_touch=True) return Credential( self._slot_name(slot), code=code, oath_type='totp', touch=True, algo='SHA1', expiration=self._expiration(timestamp)).to_dict()
def _read_slot_cred(self, slot, digits, timestamp): try: dev = self._descriptor.open_device(TRANSPORT.OTP) code = dev.driver.calculate( slot, challenge=timestamp, totp=True, digits=int(digits), wait_for_touch=False) valid_from = timestamp - (timestamp % 30) valid_to = valid_from + 30 code = Code(code, valid_from, valid_to) return (Credential(self._slot_name(slot), OATH_TYPE.TOTP, False), code) except YkpersError as e: if e.errno == 11: return (Credential(self._slot_name(slot), OATH_TYPE.TOTP, True ), None) except Exception as e: return (Credential(str(e).encode(), OATH_TYPE.TOTP, True), None) return None
def _read_slot_cred(self, slot, digits, timestamp): try: dev = self._descriptor.open_device(TRANSPORT.OTP) code = dev.driver.calculate( slot, challenge=timestamp, totp=True, digits=int(digits), wait_for_touch=False) return Credential( self._slot_name(slot), code=code, oath_type='totp', touch=False, algo='SHA1', expiration=self._expiration(timestamp)) except YkpersError as e: if e.errno == 11: return Credential( self._slot_name(slot), oath_type='totp', touch=True, algo='SHA1') except: pass return None
def calc(slot, digits, label): try: code, touch = self._otp_get_code_or_touch( slot, digits, timestamp) entries.append({ 'credential': cred_to_dict( Credential(label.encode(), OATH_TYPE.TOTP, touch)), 'code': code_to_dict( Code(code, valid_from, valid_to)) if code else None }) except YkpersError as e: if e.errno == 4: pass else: raise
def calculate_slot_mode(self, slot, digits, timestamp): try: dev = self._descriptor.open_device(TRANSPORT.OTP) code = dev.driver.calculate( slot, challenge=timestamp, totp=True, digits=int(digits), wait_for_touch=True) except Exception as e: logger.error('Failed to calculate code in slot mode', exc_info=e) return None valid_from = timestamp - (timestamp % 30) valid_to = valid_from + 30 code = Code(code, valid_from, valid_to) return pair_to_dict(Credential(self._slot_name(slot), OATH_TYPE.TOTP, True), code)
def calculate_slot_mode(self, slot, digits, timestamp): try: code = self._read_slot_code( slot, digits, timestamp, wait_for_touch=True) return pair_to_dict(Credential(self._slot_name(slot), OATH_TYPE.TOTP, True), code) except YkpersError as e: if e.errno == 4: logger.debug( 'Time out error, user probably did not touch the device.') else: logger.error( 'Failed to calculate code in slot mode', exc_info=e) except Exception as e: logger.error('Failed to calculate code in slot mode', exc_info=e) return None
def cred_from_dict(data): return Credential(data['key'].encode('utf-8'), OATH_TYPE[data['oath_type']], data['touch'])