示例#1
0
def initialize(
    app,
    authenticate,
    class_views=None,
    store_refresh_token=None,
    retrieve_refresh_token=None,
    revoke_refresh_token=None,
    retrieve_user=None,
):
    # Add settings
    utils.load_settings(app, settings)

    if class_views is not None:
        for route, view in class_views:
            try:
                if issubclass(view, HTTPMethodView) and isinstance(route, str):
                    sanic_jwt_auth_bp.add_route(
                        view.as_view(),
                        route,
                        strict_slashes=app.config.SANIC_JWT_STRICT_SLASHES)
                else:
                    raise exceptions.InvalidClassViewsFormat()
            except TypeError:
                raise exceptions.InvalidClassViewsFormat()

    # Add blueprint
    # sanic_jwt_auth_bp.strict_slashes = app.strict_slashes
    app.blueprint(sanic_jwt_auth_bp,
                  url_prefix=app.config.SANIC_JWT_URL_PREFIX)

    # Setup authentication module
    app.auth = SanicJWTAuthentication(app, authenticate)
    if store_refresh_token:
        setattr(app.auth, 'store_refresh_token', store_refresh_token)
    if retrieve_refresh_token:
        setattr(app.auth, 'retrieve_refresh_token', retrieve_refresh_token)
    if revoke_refresh_token:
        setattr(app.auth, 'revoke_refresh_token', revoke_refresh_token)
    if retrieve_user:
        setattr(app.auth, 'retrieve_user', retrieve_user)
    if app.config.SANIC_JWT_REFRESH_TOKEN_ENABLED and not all(
            store_refresh_token, retrieve_refresh_token, revoke_refresh_token):
        raise exceptions.RefreshTokenNotImplemented()

    @app.exception(exceptions.SanicJWTException)
    def exception_response(request, exception):
        return text(str(exception), status=exception.status_code)
示例#2
0
def initialize(
    app,
    authenticate,
    class_views=None,
    store_refresh_token=None,
    retrieve_refresh_token=None,
    retrieve_user=None,
):
    # Add settings
    utils.load_settings(app, settings)

    if class_views is not None:
        for route, view in class_views:
            try:
                if issubclass(view, HTTPMethodView) and isinstance(route, str):
                    sanic_jwt_auth_bp.add_route(view.as_view(), route)
                else:
                    raise exceptions.InvalidClassViewsFormat()
            except TypeError:
                raise exceptions.InvalidClassViewsFormat()

    # Add blueprint
    app.blueprint(sanic_jwt_auth_bp, url_prefix=app.config.SANIC_JWT_URL_PREFIX)

    # Setup authentication module
    app.auth = SanicJWTAuthentication(app, authenticate)
    if store_refresh_token:
        setattr(app.auth, 'store_refresh_token', store_refresh_token)
    if retrieve_refresh_token:
        setattr(app.auth, 'retrieve_refresh_token', retrieve_refresh_token)
    if retrieve_user:
        setattr(app.auth, 'retrieve_user', retrieve_user)

    if app.config.SANIC_JWT_REFRESH_TOKEN_ENABLED and (
        not store_refresh_token or
        not retrieve_refresh_token
    ):
        raise exceptions.RefreshTokenNotImplemented()
示例#3
0
    def __add_class_views(self):
        """
        Include any custom class views on the Sanic JWT Blueprint
        """
        config = self.config
        if "class_views" in self.kwargs:
            class_views = self.kwargs.pop("class_views")

            for route, view in class_views:
                if issubclass(view, endpoints.BaseEndpoint) and isinstance(
                        route, str):
                    self.bp.add_route(
                        view.as_view(
                            self.responses,
                            config=self.config,
                            instance=self.instance,
                        ),
                        route,
                        strict_slashes=config.strict_slashes(),
                    )
                else:
                    raise exceptions.InvalidClassViewsFormat()