Esempio n. 1
0
def init_journalist(is_admin=False):
    """Initialize a journalist into the database. Return their
    :class:`db.Journalist` object and password string.

    :param bool is_admin: Whether the user is an admin.

    :returns: A 2-tuple. The first entry, an :obj:`db.Journalist`
              corresponding to the row just added to the database. The
              second, their password string.
    """
    username = crypto_util.genrandomid()
    user_pw = crypto_util.genrandomid()
    user = db.Journalist(username, user_pw, is_admin)
    db.db_session.add(user)
    db.db_session.commit()
    return user, user_pw
Esempio n. 2
0
def init_journalist(is_admin=False):
    """Initialize a journalist into the database. Return their
    :class:`db.Journalist` object and password string.

    :param bool is_admin: Whether the user is an admin.

    :returns: A 2-tuple. The first entry, an :obj:`db.Journalist`
              corresponding to the row just added to the database. The
              second, their password string.
    """
    username = crypto_util.genrandomid()
    user_pw = crypto_util.genrandomid()
    user = db.Journalist(username, user_pw, is_admin)
    db.db_session.add(user)
    db.db_session.commit()
    return user, user_pw
Esempio n. 3
0
def generate_unique_codename(num_words):
    """Generate random codenames until we get an unused one"""
    while True:
        codename = crypto_util.genrandomid(num_words)
        sid = crypto_util.hash_codename(codename)  # scrypt (slow)
        matching_sources = Source.query.filter(Source.filesystem_id == sid).all()
        if len(matching_sources) == 0:
            return codename
Esempio n. 4
0
def _make_password():
    while True:
        password = crypto_util.genrandomid(7)
        try:
            Journalist.check_password_acceptable(password)
            return password
        except PasswordError:
            continue
Esempio n. 5
0
def generate():
    number_words = 8
    if request.method == 'POST':
        number_words = int(request.form['number-words'])
        if number_words not in range(7, 11):
            abort(403)
    session['codename'] = crypto_util.genrandomid(number_words)
    return render_template('generate.html', codename=session['codename'])
Esempio n. 6
0
def _make_password():
    while True:
        password = crypto_util.genrandomid(7)
        try:
            Journalist.check_password_acceptable(password)
            return password
        except PasswordError:
            continue
Esempio n. 7
0
def generate():
    number_words = 8
    if request.method == "POST":
        number_words = int(request.form["number-words"])
        if number_words not in range(7, 11):
            abort(403)
    session["codename"] = crypto_util.genrandomid(number_words)
    return render_template("generate.html", codename=session["codename"])
Esempio n. 8
0
    def test_genrandomid(self):
        id = crypto_util.genrandomid()
        id_words = id.split()

        self.assertEqual(id, crypto_util.clean(id))
        self.assertEqual(len(id_words), crypto_util.DEFAULT_WORDS_IN_RANDOM_ID)
        for word in id_words:
            self.assertIn(word, crypto_util.words)
Esempio n. 9
0
def generate():
    number_words = 8
    if request.method == 'POST':
        number_words = int(request.form['number-words'])
        if number_words not in range(7, 11):
            abort(403)
    session['codename'] = crypto_util.genrandomid(number_words)
    return render_template('generate.html', codename=session['codename'])
Esempio n. 10
0
    def verify_genrandomid(self, locale):
        id = crypto_util.genrandomid(locale=locale)
        id_words = id.split()

        self.assertEqual(id, crypto_util.clean(id))
        self.assertEqual(len(id_words), crypto_util.DEFAULT_WORDS_IN_RANDOM_ID)
        for word in id_words:
            self.assertIn(word, crypto_util._get_wordlist(locale))
Esempio n. 11
0
def make_password(config):
    while True:
        password = crypto_util.genrandomid(7, i18n.get_language(config))
        try:
            Journalist.check_password_acceptable(password)
            return password
        except PasswordError:
            continue
Esempio n. 12
0
def make_password(config):
    while True:
        password = crypto_util.genrandomid(7, i18n.get_language(config))
        try:
            Journalist.check_password_acceptable(password)
            return password
        except PasswordError:
            continue
Esempio n. 13
0
    def test_genkeypair(self):
        codename = crypto_util.genrandomid()
        filesystem_id = crypto_util.hash_codename(codename)
        journalist_filename = crypto_util.display_id()
        source = db.Source(filesystem_id, journalist_filename)
        db.db_session.add(source)
        db.db_session.commit()
        crypto_util.genkeypair(source.filesystem_id, codename)

        self.assertIsNotNone(crypto_util.getkey(filesystem_id))
Esempio n. 14
0
def init_source_without_keypair():
    """Initialize a source: create their database record and the
    filesystem directory that stores their submissions & replies.
    Return a source object and their codename string.

    :returns: A 2-tuple. The first entry, the :class:`db.Source`
    initialized. The second, their codename string.
    """
    # Create source identity and database record
    codename = crypto_util.genrandomid()
    filesystem_id = crypto_util.hash_codename(codename)
    journalist_filename = crypto_util.display_id()
    source = db.Source(filesystem_id, journalist_filename)
    db.db_session.add(source)
    db.db_session.commit()
    # Create the directory to store their submissions and replies
    os.mkdir(store.path(source.filesystem_id))

    return source, codename
Esempio n. 15
0
def init_source_without_keypair():
    """Initialize a source: create their database record and the
    filesystem directory that stores their submissions & replies.
    Return a source object and their codename string.

    :returns: A 2-tuple. The first entry, the :class:`db.Source`
    initialized. The second, their codename string.
    """
    # Create source identity and database record
    codename = crypto_util.genrandomid()
    filesystem_id = crypto_util.hash_codename(codename)
    journalist_filename = crypto_util.display_id()
    source = db.Source(filesystem_id, journalist_filename)
    db.db_session.add(source)
    db.db_session.commit()
    # Create the directory to store their submissions and replies
    os.mkdir(store.path(source.filesystem_id))

    return source, codename
Esempio n. 16
0
def generate_unique_codename():
    """Generate random codenames until we get an unused one"""
    while True:
        codename = crypto_util.genrandomid(Source.NUM_WORDS)

        # The maximum length of a word in the wordlist is 9 letters and the
        # codename length is 7 words, so it is currently impossible to
        # generate a codename that is longer than the maximum codename length
        # (currently 128 characters). This code is meant to be defense in depth
        # to guard against potential future changes, such as modifications to
        # the word list or the maximum codename length.
        if len(codename) > Source.MAX_CODENAME_LEN:
            app.logger.warning(
                "Generated a source codename that was too long, "
                "skipping it. This should not happen. "
                "(Codename='{}')".format(codename))
            continue

        filesystem_id = crypto_util.hash_codename(codename)  # scrypt (slow)
        matching_sources = Source.query.filter(
            Source.filesystem_id == filesystem_id).all()
        if len(matching_sources) == 0:
            return codename
Esempio n. 17
0
    def test_hash_codename(self):
        codename = crypto_util.genrandomid()
        hashed_codename = crypto_util.hash_codename(codename)

        self.assertRegexpMatches(hashed_codename, '^[2-7A-Z]{103}=$')