Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorators not applied to Static Files #1953

Closed
digitalkaoz opened this issue Oct 22, 2020 · 2 comments · Fixed by #1954
Closed

Decorators not applied to Static Files #1953

digitalkaoz opened this issue Oct 22, 2020 · 2 comments · Fixed by #1954

Comments

@digitalkaoz
Copy link

Describe the bug

decorators cannot be applied to static files serving. My use-case here is serving auto-generated docs through the application itself while requesting a valid Basic-Authentication for the static files.

Code snippet

import os

from sanic import Blueprint, Sanic
from sanic.request import Request
from sanic_httpauth import HTTPBasicAuth

auth = HTTPBasicAuth()

users = {
    "docs": "docs",
}


@auth.verify_password
def verify_password(username: str, password: str) -> bool:
    """
    basic auth credentials validation

    :param username:
    :param password:
    :return:
    """
    if username in users:
        return users.get(username) == "docs"
    return False


def ApiDoc(app: Sanic):
    """
    add autogenerated Api Docs to the Sanic App
    :param app: the Sanic App
    :return:
    """

    directory = './api_doc/')
    path = '/api_doc'

    bp = Blueprint('api_doc', url_prefix=path)
    bp.static('/', directory)

    @bp.middleware('request')
    @auth.login_required
    def ba_middleware(request: Request):
        """
        inject Basic Authentication for the whole blueprint
        :param request:
        :return:
        """
        pass

    app.blueprint(bp)

this works perfectly fine for e.g. the sanic-swagger blueprint.

Expected behavior
the blueprint middleware is also applied when serving the static files

Environment (please complete the following information):

  • OS: osx
  • Python 3.8.6
  • Sanic 20.9.0
@ashleysommer
Copy link
Member

Hi @digitalkaoz
I was about to reply with a comment like "this is expected, because request middlewares do not apply to static file routes".
However I just did a test, and app.middleware('request') decorators do apply to app.static() routes.
So this must be a bug in the way blueprints register static routes. Its likely related to the relatively new namespaced middlewares used in blueprints (ie, named_middleware) looks like it the applicable_middlewares algorithm does not detect static routes within a blueprint as a candidate to apply the middleware to.

This might be intentional, but I doubt it. I'll add a test for it in our test suite, and try to see if there is a clean way to fix it without introducing any breakage or performance decrease here.

ashleysommer added a commit to ashleysommer/sanic that referenced this issue Oct 22, 2020
This allows blueprint registration to add the bp's static routes to its list of own routes. So now blueprint middlewares will apply to a blueprint's static file routes.
Fixes sanic-org#1953
@digitalkaoz
Copy link
Author

@ashleysommer lovely!

ahopkins pushed a commit that referenced this issue Oct 24, 2020
This allows blueprint registration to add the bp's static routes to its list of own routes. So now blueprint middlewares will apply to a blueprint's static file routes.
Fixes #1953
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants