def code(ctx, show_hidden, query, single):
    """
    Generate codes.

    Generate codes from credentials stored on your YubiKey.
    Provide a query string to match one or more specific credentials.
    Touch and HOTP credentials require a single match to be triggered.
    """

    ensure_validated(ctx)

    controller = ctx.obj['controller']
    creds = [(cr, c)
             for (cr, c) in controller.calculate_all()
             if show_hidden or not cr.is_hidden
             ]

    creds = _search(creds, query)

    if len(creds) == 1:
        cred, code = creds[0]
        if cred.touch:
            prompt_for_touch()
        try:
            if cred.oath_type == OATH_TYPE.HOTP:
                # HOTP might require touch, we don't know.
                # Assume yes after 500ms.
                hotp_touch_timer = Timer(0.500, prompt_for_touch)
                hotp_touch_timer.start()
                creds = [(cred, controller.calculate(cred))]
                hotp_touch_timer.cancel()
            elif code is None:
                creds = [(cred, controller.calculate(cred))]
        except APDUError as e:
            if e.sw == SW.SECURITY_CONDITION_NOT_SATISFIED:
                ctx.fail('Touch credential timed out!')

    elif single:
        _error_multiple_hits(ctx, [cr for cr, c in creds])

    if single:
        click.echo(creds[0][1].value)
    else:
        creds.sort()

        outputs = [
            (
                cr.printable_key,
                c.value if c
                else '[Touch Credential]' if cr.touch
                else '[HOTP Credential]' if cr.oath_type == OATH_TYPE.HOTP
                else ''
            ) for (cr, c) in creds
        ]

        longest_name = max(len(n) for (n, c) in outputs) if outputs else 0
        longest_code = max(len(c) for (n, c) in outputs) if outputs else 0
        format_str = u'{:<%d}  {:>%d}' % (longest_name, longest_code)

        for name, result in outputs:
            click.echo(format_str.format(name, result))
def code ( ctx , show_hidden , query , single ) : ensure_validated ( ctx ) controller = ctx . obj [ 'controller' ] creds = [ ( cr , c ) for ( cr , c ) in controller . calculate_all ( ) if show_hidden or not cr . is_hidden ] creds = _search ( creds , query ) if len ( creds ) == 1 : cred , code = creds [ 0 ] if cred . touch : prompt_for_touch ( ) try : if cred . oath_type == OATH_TYPE . HOTP : hotp_touch_timer = Timer ( 0.500 , prompt_for_touch ) hotp_touch_timer . start ( ) creds = [ ( cred , controller . calculate ( cred ) ) ] hotp_touch_timer . cancel ( ) elif code is None : creds = [ ( cred , controller . calculate ( cred ) ) ] except APDUError as e : if e . sw == SW . SECURITY_CONDITION_NOT_SATISFIED : ctx . fail ( 'Touch credential timed out!' ) elif single : _error_multiple_hits ( ctx , [ cr for cr , c in creds ] ) if single : click . echo ( creds [ 0 ] [ 1 ] . value ) else : creds . sort ( ) outputs = [ ( cr . printable_key , c . value if c else '[Touch Credential]' if cr . touch else '[HOTP Credential]' if cr . oath_type == OATH_TYPE . HOTP else '' ) for ( cr , c ) in creds ] longest_name = max ( len ( n ) for ( n , c ) in outputs ) if outputs else 0 longest_code = max ( len ( c ) for ( n , c ) in outputs ) if outputs else 0 format_str = u'{:<%d}  {:>%d}' % ( longest_name , longest_code ) for name , result in outputs : click . echo ( format_str . format ( name , result ) )
Generate codes.

    Generate codes from credentials stored on your YubiKey.
    Provide a query string to match one or more specific credentials.
    Touch and HOTP credentials require a single match to be triggered.