Exemplo n.º 1
0
    def __init__(
        self,
        *args,
        client_name,
        instance_host_backend=None,
        instance_credentials_backend=None,
        session_class=None,
        **kwargs
    ):
        if session_class is None:
            session_class = MastodonSession

        OAuth2ConsumerBlueprint.__init__(self, *args, session_class=session_class, **kwargs)
        self.client_name = client_name

        if instance_host_backend is None:
            self.instance_host_backend = SessionBackend(key="{bp.name}_instance_host")
        elif callable(instance_host_backend):
            self.instance_host_backend = instance_host_backend()
        else:
            self.instance_host_backend = instance_host_backend

        if instance_credentials_backend is None:
            self.instance_credentials_backend = InMemoryBackend()
        elif callable(instance_credentials_backend):
            self.instance_credentials_backend = instance_credentials_backend()
        else:
            self.instance_credentials_backend = instance_credentials_backend
Exemplo n.º 2
0
    def __init__(self,
                 name,
                 import_name,
                 static_folder=None,
                 static_url_path=None,
                 template_folder=None,
                 url_prefix=None,
                 subdomain=None,
                 url_defaults=None,
                 root_path=None,
                 login_url=None,
                 authorized_url=None,
                 backend=None):

        bp_kwargs = dict(
            name=name,
            import_name=import_name,
            static_folder=static_folder,
            static_url_path=static_url_path,
            template_folder=template_folder,
            url_prefix=url_prefix,
            subdomain=subdomain,
            url_defaults=url_defaults,
            root_path=root_path,
        )
        # `root_path` didn't exist in 0.10, and will cause an error if it's
        # passed in that version. Only pass `root_path` if it's set.
        if bp_kwargs["root_path"] is None:
            del bp_kwargs["root_path"]
        flask.Blueprint.__init__(self, **bp_kwargs)

        login_url = login_url or "/{bp.name}"
        authorized_url = authorized_url or "/{bp.name}/authorized"

        self.add_url_rule(
            rule=login_url.format(bp=self),
            endpoint="login",
            view_func=self.login,
        )
        self.add_url_rule(
            rule=authorized_url.format(bp=self),
            endpoint="authorized",
            view_func=self.authorized,
        )

        if backend is None:
            self.backend = SessionBackend()
        elif callable(backend):
            self.backend = backend()
        else:
            self.backend = backend

        self.logged_in_funcs = []
        self.from_config = {}
        invalidate_token = lambda d: lazy.invalidate(self.session, "token")
        self.config = CallbackDict(on_update=invalidate_token)
        self.before_app_request(self.load_config)
Exemplo n.º 3
0
    def __init__(self,
                 name,
                 import_name,
                 static_folder=None,
                 static_url_path=None,
                 template_folder=None,
                 url_prefix=None,
                 subdomain=None,
                 url_defaults=None,
                 root_path=None,
                 login_url=None,
                 authorized_url=None,
                 backend=None):

        bp_kwargs = dict(
            name=name,
            import_name=import_name,
            static_folder=static_folder,
            static_url_path=static_url_path,
            template_folder=template_folder,
            url_prefix=url_prefix,
            subdomain=subdomain,
            url_defaults=url_defaults,
            root_path=root_path,
        )
        # `root_path` didn't exist until 1.0
        if StrictVersion(flask.__version__) < StrictVersion('1.0'):
            del bp_kwargs["root_path"]
        flask.Blueprint.__init__(self, **bp_kwargs)

        login_url = login_url or "/{bp.name}"
        authorized_url = authorized_url or "/{bp.name}/authorized"

        self.add_url_rule(
            rule=login_url.format(bp=self),
            endpoint="login",
            view_func=self.login,
        )
        self.add_url_rule(
            rule=authorized_url.format(bp=self),
            endpoint="authorized",
            view_func=self.authorized,
        )

        if backend is None:
            self.backend = SessionBackend()
        elif callable(backend):
            self.backend = backend()
        else:
            self.backend = backend

        self.logged_in_funcs = []
        self.from_config = {}
        self.config = Dictective(
            lambda d: lazy.invalidate(self.session, "token"))
        self.before_app_request(self.load_config)
Exemplo n.º 4
0
def make_mastodon_blueprint(
    client_name,
    backend=None,
    instance_host_backend=None,
    instance_credentials_backend=None,
    scope="read",
):
    if backend is None:
        backend = SessionBackend(key="{bp.name}_{bp.instance_host}_oauth_token")

    mastodon_bp = MastodonConsumerBlueprint(
        "mastodon", __name__, client_name=client_name, scope=scope, backend=backend
    )

    @mastodon_bp.before_app_request
    def set_applocal_session():
        ctx = stack.top
        ctx.mastodon_oauth = mastodon_bp.session

    return mastodon_bp