def parse_public_key(user_pubkey):
    """ Return paramiko key class instance of a user's public key text. """
    import paramiko
    if len(user_pubkey.split()) == 3:
        key_msg, key_data, _ = user_pubkey.split()
    elif len(user_pubkey.split()) == 2:
        key_msg, key_data = user_pubkey.split()
    elif len(user_pubkey.split()) == 1:
        key_msg, key_data = 'ssh-rsa', user_pubkey
    else:
        raise ValueError('Malformed public key format: {0!r}'.format(
            user_pubkey))
    try:
        key_bytes = key_data.decode('ascii')
    except UnicodeDecodeError:
        raise ValueError('Malformed public key encoding: {0!r}'.format(
            key_data))
    decoded_keybytes = paramiko.py3compat.decodebytes(key_bytes)
    try:
        return {'ssh-rsa': paramiko.RSAKey, 'ssh-dss': paramiko.DSSKey,
            'ecdsa-sha2-nistp256': paramiko.ECDSAKey}.get(key_msg)(data=
            decoded_keybytes)
    except KeyError:
        raise ValueError('Malformed public key_msg: {0!r}'.format(key_msg))
