Beispiel #1
0
    def select_entry(self, entries, format, attrs, display_count=True):
        """
        Display a list of lines in with formatting defined in ``format``.
        ``attrs`` is a list of attributes in the format.

        Prompt user for a selection and return the value (index of
        ``entries`` -1).

        If only one entry is provided then always return 0.

        Return: 0..n for the index of the selected entry
                -1 if all entries should be displayed
                -2 to quit, no entries to be displayed
        """
        if not self.env.interactive or not sys.stdout.isatty():
            return -1

        counter = len(entries)
        if counter == 0:
            raise NotFound(reason=_("No matching entries found"))

        i = 1
        for e in entries:
            # There is no guarantee that all attrs are in any given
            # entry
            d = {}
            for a in attrs:
                d[a] = e.get(a, '')
            self.print_line("%d: %s" % (i, format % d))
            i = i + 1

        if display_count:
            self.print_count(entries, 'Found %d match', 'Found %d matches')

        while True:
            try:
                resp = self.prompt(
                    "Choose one: (1 - %s), a for all, q to quit" % counter)
            except EOFError:
                return -2

            if resp.lower() == "q":  #pylint: disable=E1103
                return -2
            if resp.lower() == "a":  #pylint: disable=E1103
                return -1
            try:
                selection = int(resp) - 1
                if (selection >= 0 and selection < counter):
                    break
            except Exception:
                # fall through to the error msg
                pass

            self.print_line("Please enter a number between 1 and %s" % counter)

        self.print_line('')
        return selection
Beispiel #2
0
 def test_get(self):
     certreq = self.mkinstance(
         'custodia/[email protected]', 'store:certreq')
     self.m_api.Command.vault_retrieve.side_effect = NotFound(reason=u'')
     certreq.get('keys/HTTP/client1.ipa.example')
Beispiel #3
0
    def forward(self, *args, **kwargs):
        # Open the YubiKey
        try:
            yk = yubico.find_yubikey()
        except usb.core.USBError as e:
            raise NotFound(reason="No YubiKey found: %s" % e.strerror)
        except yubico.yubikey.YubiKeyError as e:
            raise NotFound(reason=e.reason)

        assert yk.version_num() >= (2, 1)

        # If no slot is specified, find the first free slot.
        if kwargs.get('slot', None) is None:
            try:
                used = yk.status().valid_configs()
                kwargs['slot'] = sorted({1, 2}.difference(used))[0]
            except IndexError:
                raise NotFound(reason=_('No free YubiKey slot!'))

        # Create the key (NOTE: the length is fixed).
        key = os.urandom(20)

        # Write the config.
        cfg = yk.init_config()
        cfg.mode_oath_hotp(key, kwargs['ipatokenotpdigits'])
        cfg.extended_flag('SERIAL_API_VISIBLE', True)
        yk.write_config(cfg, slot=kwargs['slot'])

        # Filter the options we want to pass.
        options = {
            k: v
            for k, v in kwargs.items() if k in (
                'version',
                'description',
                'ipatokenowner',
                'ipatokendisabled',
                'ipatokennotbefore',
                'ipatokennotafter',
                'ipatokenotpdigits',
            )
        }

        # Run the command.
        answer = self.Backend.rpcclient.forward(
            'otptoken_add',
            *args,
            type=u'hotp',
            ipatokenvendor=u'YubiCo',
            ipatokenmodel=unicode(yk.model),
            ipatokenserial=unicode(yk.serial()),
            ipatokenotpalgorithm=u'sha1',
            ipatokenhotpcounter=0,
            ipatokenotpkey=key,
            no_qrcode=True,
            **options)

        # Suppress values we don't want to return.
        for k in (u'uri', u'ipatokenotpkey'):
            if k in answer.get('result', {}):
                del answer['result'][k]

        # Return which slot was used for writing.
        answer.get('result', {})['slot'] = kwargs['slot']

        return answer