from pwgen import pwgen
from re import findall

import vccs_client


def generate_password(settings, credential_id, email):
    """
    Generate a new password credential and add it to the VCCS authentication backend.

    The salt returned needs to be saved for use in subsequent authentications using
    this password. The password is returned so that it can be conveyed to the user.

    :param settings: settings dict
    :param credential_id: VCCS credential_id as string
    :param email: user e-mail address as string
    :return: (password, salt) both strings
    """
    password = pwgen(settings.get('password_length'), no_symbols = True)
    factor = vccs_client.VCCSPasswordFactor(password,
                                            credential_id = credential_id)
    vccs = vccs_client.VCCSClient(base_url = settings.get('vccs_url'))
    vccs.add_credentials(email, [factor])

    return (_human_readable(password), factor.salt)


def _human_readable(password):
    """
    Format a random password more readable to humans (groups of four characters).

    :param password: string
    :return: readable password as string
    :rtype: string
    """
    regexp = '.{,4}'
    parts = findall(regexp, password)
    return ' '.join(parts)

from pwgen import pwgen
from re import findall

import vccs_client


def generate_password(settings, credential_id, email):
    """
    Generate a new password credential and add it to the VCCS authentication backend.

    The salt returned needs to be saved for use in subsequent authentications using
    this password. The password is returned so that it can be conveyed to the user.

    :param settings: settings dict
    :param credential_id: VCCS credential_id as string
    :param email: user e-mail address as string
    :return: (password, salt) both strings
    """
    password = pwgen(settings.get('password_length'), no_capitalize = True, no_symbols = True)
    factor = vccs_client.VCCSPasswordFactor(password,
                                            credential_id = credential_id)
    vccs = vccs_client.VCCSClient(base_url = settings.get('vccs_url'))
    vccs.add_credentials(email, [factor])

    return (_human_readable(password), factor.salt)


def _human_readable(password):
    """
    Format a random password more readable to humans (groups of four characters).

    :param password: string
    :return: readable password as string
    :rtype: string
    """
    regexp = '.{,4}'
    parts = findall(regexp, password)
    return ' '.join(parts)

Exclude upper case letters from generated passwords.
Exclude upper case letters from generated passwords.

12 character passwords from the set a-z0-9 have more bits of
entropy (62) than 10 character passwords from the set
a-zA-Z0-9 (60), and are probably perceived as nicer by the
users too (less ambiguity, easier to type on smartphones and
the like).
