<commit_before>from zope.interface import implements
from repoze.who.interfaces import IAuthenticator
from paste.httpheaders import AUTHORIZATION

from openspending.model import Account
from openspending.ui.lib.security import check_password_hash


class UsernamePasswordAuthenticator(object):
    implements(IAuthenticator)

    def authenticate(self, environ, identity):
        if not 'login' in identity or not 'password' in identity:
            return None
        account = Account.by_name(identity['login'])
        if account is None:
            return None
        if check_password_hash(account.password, identity['password']):
            return account.name
        return None


class ApiKeyAuthenticator(object):
    implements(IAuthenticator)

    def authenticate(self, environ, identity):
        authorization = AUTHORIZATION(environ)
        try:
            authmeth, auth = authorization.split(' ', 1)
        except ValueError:
            # not enough values to unpack
            return None
        if authmeth.lower() == 'apikey':
            acc = Account.by_api_key(auth.strip())
            if acc is not None:
                return acc.name
<commit_msg>Prevent exception if login try with system user<commit_after>from zope.interface import implements
from repoze.who.interfaces import IAuthenticator
from paste.httpheaders import AUTHORIZATION

from openspending.model import Account
from openspending.ui.lib.security import check_password_hash


class UsernamePasswordAuthenticator(object):
    implements(IAuthenticator)

    def authenticate(self, environ, identity):
        if not 'login' in identity or not 'password' in identity:
            return None
        account = Account.by_name(identity['login'])
        if account is None:
            return None
        if account.password is None:
            return None
        if check_password_hash(account.password, identity['password']):
            return account.name
        return None


class ApiKeyAuthenticator(object):
    implements(IAuthenticator)

    def authenticate(self, environ, identity):
        authorization = AUTHORIZATION(environ)
        try:
            authmeth, auth = authorization.split(' ', 1)
        except ValueError:
            # not enough values to unpack
            return None
        if authmeth.lower() == 'apikey':
            acc = Account.by_api_key(auth.strip())
            if acc is not None:
                return acc.name

{'fraction_non_alphanumeric': 0.07053100932306446, 'fraction_numerical': 0.0008107012565869477, 'mean_word_length': 2.6671619613670132, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4921750', 'n_tokens_mistral': 636, 'n_tokens_neox': 625, 'n_words': 191}