コード例 #1
0
ファイル: test_oath.py プロジェクト: uswitch/yubikey-manager
 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())
コード例 #2
0
 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
コード例 #3
0
 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)
コード例 #4
0
 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()
コード例 #5
0
 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
コード例 #6
0
 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
コード例 #7
0
 def calculate(self, credential, timestamp, password_key):
     try:
         dev = self._descriptor.open_device(TRANSPORT.CCID)
         controller = OathController(dev.driver)
         if controller.locked and password_key is not None:
             controller.validate(a2b_hex(password_key))
     except:
         return None
     return controller.calculate(Credential.from_dict(credential),
                                 timestamp).to_dict()
コード例 #8
0
    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)
コード例 #9
0
 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
コード例 #10
0
 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
コード例 #11
0
 def test_credential_parse_period_and_issuer_and_name(self):
     issuer, name, period = Credential.parse_key(b'20/Issuer:name')
     self.assertEqual(20, period)
     self.assertEqual('Issuer', issuer)
     self.assertEqual('name', name)
コード例 #12
0
 def test_credential_parse_only_name(self):
     issuer, name, period = Credential.parse_key(b'name')
     self.assertEqual(30, period)
     self.assertIsNone(issuer)
     self.assertEqual('name', name)
コード例 #13
0
 def test_credential_parse_period_and_name(self):
     issuer, name, period = Credential.parse_key(b'20/name')
     self.assertEqual(20, period)
     self.assertIsNone(issuer)
     self.assertEqual('name', name)
コード例 #14
0
 def test_credential_parse_weird_issuer_and_name(self):
     issuer, name, period = Credential.parse_key(b'weird/Issuer:name')
     self.assertEqual(30, period)
     self.assertEqual('weird/Issuer', issuer)
     self.assertEqual('name', name)
コード例 #15
0
 def calculate(self, credential, timestamp, password_key):
     return self._calculate(Credential.from_dict(credential), timestamp,
                            password_key).to_dict()
コード例 #16
0
 def test_credential_parse_only_name(self):
     period, issuer, name = Credential.parse_long_name('name')
     self.assertEqual(30, period)
     self.assertEqual(None, issuer)
     self.assertEqual('name', name)
コード例 #17
0
 def test_credential_parse_period_and_issuer_and_name(self):
     period, issuer, name = Credential.parse_long_name('20/Issuer:name')
     self.assertEqual(20, period)
     self.assertEqual('Issuer', issuer)
     self.assertEqual('name', name)
コード例 #18
0
ファイル: yubikey.py プロジェクト: hanazuki/yubioath-desktop
def cred_from_dict(data):
    return Credential(data['key'].encode('utf-8'),
                      OATH_TYPE[data['oath_type']], data['touch'])
コード例 #19
0
 def delete_credential(self, credential, password_key):
     dev = self._descriptor.open_device(TRANSPORT.CCID)
     controller = OathController(dev.driver)
     if controller.locked and password_key is not None:
         controller.validate(a2b_hex(password_key))
     controller.delete(Credential.from_dict(credential))