Exemple #1
0
def handler_from_provider(provider_name):
    """
    Return an authentication handler for the given provider.

    Params:
        provider_name (str): Name of the email provider ("custom", "gmail" or
            "outlook").

    Returns:
        An object that implements the AuthHandler interface.
    """
    if provider_name == "custom":
        from .generic import GenericAuthHandler

        return GenericAuthHandler()
    elif provider_name == "gmail":
        from .google import GoogleAuthHandler

        return GoogleAuthHandler()
    elif provider_name == "microsoft":
        from .microsoft import MicrosoftAuthHandler

        return MicrosoftAuthHandler()

    raise NotSupportedError(
        'Nylas does not support the email provider "{}".'.format(provider_name)
    )
Exemple #2
0
def handler_from_provider(provider_name):
    """Return an authentication handler for the given provider.

    Parameters
    ----------
    provider_name : str
        Name of the email provider (e.g. inbox/providers.py) OR of the provider
        module's PROVIDER constant

        (XXX: interface terribleness!)

    Returns
    -------
    An object that implements the AuthHandler interface.
    """
    from inbox.auth import module_registry
    auth_mod = module_registry.get(provider_name)

    if auth_mod is None:
        # Try to get a generic provider
        info = providers.get(provider_name, None)
        if info:
            provider_type = info.get('type', None)
            if provider_type:
                auth_mod = module_registry.get('generic')

    if auth_mod is None:
        raise NotSupportedError('Inbox does not support the email provider.')

    auth_handler_class = getattr(auth_mod, auth_mod.AUTH_HANDLER_CLS)
    auth_handler = auth_handler_class(provider_name=provider_name)
    return auth_handler
Exemple #3
0
def provider_info(provider_name):
    """Like providers[provider_name] except raises
    inbox.basicauth.NotSupportedError instead of KeyError when the provider is
    not found.
    """
    if provider_name not in providers:
        raise NotSupportedError("Provider: {} not supported.".format(
            provider_name))

    return providers[provider_name]
Exemple #4
0
def get_sendmail_client(account):
    from inbox.sendmail import module_registry

    sendmail_mod = module_registry.get(account.provider)
    if not sendmail_mod:
        raise NotSupportedError('Inbox does not support the email provider.')

    sendmail_cls = getattr(sendmail_mod, sendmail_mod.SENDMAIL_CLS)
    sendmail_client = sendmail_cls(account.id, account.namespace)
    return sendmail_client
Exemple #5
0
def provider_info(provider_name, email_address=None):
    """Like providers[provider_name] except raises
    inbox.basicauth.NotSupportedError instead of KeyError when the provider is
    not found.

    Parameters
    ----------
    email_address : str or None
        Allows further customization of the return value on a per-account
        basis.
    """
    if provider_name not in providers:
        raise NotSupportedError(
            "Provider: {} not supported.".format(provider_name))

    return providers.lookup_info(provider_name, email_address)
Exemple #6
0
def create_account(db_session, email_address, response):
    provider_name = provider_from_address(email_address)
    if provider_name == "unknown":
        raise NotSupportedError('Inbox does not support the email provider.')

    try:
        account = db_session.query(GenericAccount).filter_by(
            email_address=email_address).one()
    except sqlalchemy.orm.exc.NoResultFound:
        namespace = Namespace()
        account = GenericAccount(namespace=namespace)

    account.email_address = response['email']
    account.password = response['password']
    account.date = datetime.datetime.utcnow()
    account.provider = provider_name

    return account
Exemple #7
0
def handler_from_provider(provider_name):
    # TODO: Console auth doesn't have support for handling unknown providers
    # and just trying eas first with a fallback, so just assume EAS for now.
    # -cg3
    if provider_name == 'unknown':
        provider_name = 'eas'
    auth_mod = module_registry.get(provider_name)

    if auth_mod is not None:
        return auth_mod

    # Try to get a generic provider
    info = providers.get(provider_name, None)
    if info:
        provider_type = info.get('type', None)
        if provider_type:
            auth_mod = module_registry.get('generic')

            if auth_mod is not None:
                return auth_mod

    raise NotSupportedError('Inbox does not support the email provider.')
Exemple #8
0
def provider_info(provider_name):
    if provider_name not in providers:
        raise NotSupportedError(
            "Provider: {} not supported.".format(provider_name))

    return providers[provider_name]
Exemple #9
0
def handler_from_provider(provider_name):
    """Return an authentication handler for the given provider.

    Parameters
    ----------
    provider_name : str
        Name of the email provider (e.g. inbox/providers.py) OR of the provider
        module's PROVIDER constant

        (XXX: interface terribleness!)

    Returns
    -------
    An object that implements the AuthHandler interface.

    Note that if a plugin provides mixin classes, the type of the object may be
    generated dynamically, and may not necessarily be directly importable.
    """

    # TODO: Console auth doesn't have support for handling unknown providers
    # and just trying eas first with a fallback, so just assume EAS for now.
    # -cg3
    if provider_name == 'unknown':
        provider_name = 'eas'
    auth_mod = module_registry.get(provider_name)

    if auth_mod is None:
        # Try to get a generic provider
        info = providers.get(provider_name, None)
        if info:
            provider_type = info.get('type', None)
            if provider_type:
                auth_mod = module_registry.get('generic')

    if auth_mod is None:
        raise NotSupportedError('Inbox does not support the email provider.')

    # The name in the entry_point must match module's PROVIDER constant.
    # Known values include: 'generic', 'gmail', 'outlook', 'eas' (if installed)
    mod_name = auth_mod.PROVIDER

    # Now get the AuthHandler corresponding to the auth_mod.
    # XXX: We should replace register_backends with entry_points.
    auth_handler_class = None
    group = 'inbox.auth'
    for entry_point in pkg_resources.iter_entry_points(group, mod_name):
        if auth_handler_class is not None:
            raise RuntimeError(
                "Duplicate definition of inbox.auth handler: {0!r}".format(
                    mod_name))
        auth_handler_class = entry_point.load()

    assert auth_handler_class is not None, \
        "auth module {} not registered as an entry point".format(mod_name)

    # Allow plugins to specify mixin classes
    # Example usage:
    #
    #   # setup.py snippet
    #   entry_points = {
    #       'inbox.auth.mixins': [
    #           'yahoo = example.auth:YAuthMixin',
    #           '* = example.auth:AllAuthMixin',
    #       ],
    #   },
    #
    #   # example/auth.py
    #
    #   class YAuthMixin(object):
    #       def verify_account(self, account):
    #           if account.email == '*****@*****.**':
    #               return False
    #           return super(YAuthMixin, self).verify_account(account)
    #
    mixins = []
    group = 'inbox.auth.mixins'
    for entry_point in pkg_resources.iter_entry_points(group, '*'):
        mixins.append(entry_point.load())
    for entry_point in pkg_resources.iter_entry_points(group, mod_name):
        mixins.append(entry_point.load())

    if mixins:
        bases = tuple(mixins) + (auth_handler_class, )

        # type(name, bases, dict)
        auth_handler_class = type('<wrapped %s>' % auth_handler_class.__name__,
                                  bases, {})

    auth_handler = auth_handler_class()
    return auth_handler