Thanks to the answer below, I have a before_request function which redirects a user to /login if they have not yet logged in:
flask before request - add exception for specific route
Here is a copy of my before_request:
#app.before_request
def before_request():
if 'logged_in' not in session and request.endpoint != 'login':
return redirect(url_for('login'))
Files in my static directory are not being served however unless the user is logged in.
On my /login page I am sourcing a css file from the /static directory but it can not be loaded because of this before_request.
I have deployed this application using apache mod_wsgi and in my apache configuration file I have even included the /static directory as the site's DocumentRoot.
How can I add an exception to serve my application's /static files without the user logging in, but still use this before_request for the routes defined by my flask application?
You'll need to add an Alias or AliasMatch directive to your Apache config file (or .htaccess file, should you not have access to the .conf files) to ensure that Apache serves your static files, rather than Flask. Make sure that you have provided a Directory to allow the Apache web server to access your static path. (Also, don't forget to restart Apache if you are editing the .conf files so your changes will be picked up).
As a temporary stop-gap measure (or to make it easy to work with in development) you could also check to make sure that the string /static/ is not in request.path:
if 'logged_in' not in session \
and request.endpoint != 'login' \
and '/static/' not in request.path:
I think that there is a solution cleaner than checking request.path.
if 'logged_in' not in session and request.endpoint not in ('login', 'static'):
return redirect(url_for('login'))
I do agree with the Apache approach, but for a quick fix I the following logic at the start of the before_request() function:
if flask.request.script_root == "/static":
return
Related
I would like to serve user uploaded media files using nginx on the same host as the Django application rather than a CDN or S3 or similar.
The django-private-storage library can be used to protect media files behind a login: https://github.com/edoburu/django-private-storage
I am deploying my Django application with Dokku.
Dokku says that dokku persistant storage plugin should be used to allow for user uploads to be persisted on the host. https://dokku.com/docs~v0.9.2/advanced-usage/persistent-storage/
My confusion is that django-private-storage requires you to edit the config for nginx.
Specifically, it requires you to set the location of the private media being served to be internal. So that the URL cannot be accessed from the outside by a user who isn't logged in.
The dokku docs don't explain how to use persistant storage behind an application login.
Do I actually need django-persistant-storage to be able to write user uploaded media?
How can I combine these solutions so that my application, which is inside a container, can read and write media files, which media files are served by nginx, and served at an internal location that can only be accessed by a user who is logged into the application?
Updates (Oct 2021)
I am able to deploy my app, upload files and access them at the appropriate URL. But I haven't been able to protect them against unauthenticated access.
I haven't yet used django-private-storage or dokku persistant storage. Once the files are inaccessible I plan to follow these steps to allow authenticated access: https://b0uh.github.io/protect-django-media-files-per-user-basis-with-nginx.html
I created a file my_conf.conf saved to /home/dokku/backend/nginx.conf.d
which contains
location /protected/ {
internal;
alias /home/dokku/backend/;
}
and then rebooted Nginx
I can't actually see the images anywhere on host, but if I run dokku enter backend then my files are there in the container under '/mediafiles/testuploads/'
Here is settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
MEDIA_URL = '/media/'
and models.py
class User(AbstractUser):
profile_image = models.ImageField(upload_to='testuploads/', null=True)
Let's do it by yourself!
You can serve your profile images with your custom view that checks auth!
You may implement the needed feature without additional dependencies. As a bonus, you will understand the whole process.
So, you need to:
Add a path, something like /profiles/<int:user_id>/image/ in your urls.py
Use this link with a proper user_id in your front-end (change a needed template)
Write a class-based or function-based view for this endpoint, as usual, check the user_id parameter, check the auth in request, compare user in request with the user in the user instance and maybe something else.
Response with 401 Not Authorized when you have an unauthorized request.
Response with FileResponse in OK.
from django.http import FileResponse
response = FileResponse(open('myfile.png', 'rb'))
use your user.profile_image property
Theoretically, if the user doesn't know the old path to /media/testuploads/filename.ext this file is "not shared".
But if you want to be sure - don't serve /media/ folder with NGINX or exactly /media/testuploads/ path if you want to serve another media files (return 401 https://$host$request_uri; in NGINX config in the needed block). Such changes need NGINX to be reloaded.
Watch view caching in the next seasons 😀 to improve the performance. But the browser will cache the image if it works in default settings.
I have project in django 1.0.4 - yes I know it is old.
I want to use the lack of access to media (audio) files for users who are not logged in.
After making changes to nginx, logged in users also have no access.
I tried with view and url function - no result
my nginx settings:
location /media/content/audio/ {
deny all;
}
my function and url
#login_required
def protected_serve(request, path, document_root=None, show_indexes=False):
if not request.user.is_authenticated:
raise Http404()
else:
return serve(request, path, document_root, show_indexes)
urlpatterns += patterns('',
(r'^media/content/audio/(?P<path>.*)$', protected_serve),
)
You're very close to having the whole puzzle put together. There are two things you need to do:
Configure NGINX that you do want to be able to serve data from a particular folder, but that said folder isn't public. The authorization to send files from a folder will come from the application behind NGINX, not from external requests to NGINX.
Have your django app send the kind of response to NGINX that NGINX understands to mean "serve this file from the protected area in 1"
The way you achieve the first goal is to use the config directive "internal"
Achieving the second goal is to use the HTTP response header "X-Accel-Redirect" as #ralf states in the comments above.
Here is a blog post on the subject: https://clubhouse.io/developer-how-to/how-to-use-internal-redirects-in-nginx/
A Python project to help you achieve the same goal: https://pypi.org/project/django-transfer/
NGINX Docs: https://www.nginx.com/resources/wiki/start/topics/examples/xsendfile/
in the urls i have this url that leads to the serve_protected_file on each time someone is trying to access a media file
url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], views.serve_protected_file, {'document_root': settings.MEDIA_ROOT})
the serve_protected_file looks like this
def serve_protected_file(request, path, document_root=None, show_indexes=False):
if request.user.is_authenticated:
return serve(request, path, document_root, show_indexes)
raise Http404()
This only works in the development environment
When i deploy then the files are served with the nginx and it gives me the file trough the url which looks like that
https://staging.mywebsite.com/media/img/531126758844.jpg
how to restrict it everything in my media folder is private and external access to a file should be restricted unless user is authenticated
my staging.nginx.conf looks like
location ~ ^/media.*?/(.*)$ {
alias /data/www/staging/mywesite/media/$1;
access_log off;
}
okay Eurica after 2 hours of head banging...
Since the media files are all confidential, I just deleted the media configuration in staging.nginx.conf and it works like a charm
so now is executing the django view..., nginx was intercepting the django view
This is a continuation for my previous question How to get hostname or IP in settings.py so that i can use it to decide which app's urls to use
I am making a django project that have 2 apps. When you open www.webName.co.id it will use urls.py from app A, but when you open webName.co.uk it will use urls.py from app B.
Basically my project will have 1 backend, multiple frontend urls and views, and each apps have their own models.
But i am having problem with how Django decide which static folder and media root it is using. I want to change which static folder and media it is using depending on the www. Basically i want to use static and media folder in app A when you enter webName.co.uk, and another static and media folder in app B when you enter webName.co.uk
The way i am going to do this is adding codes in middleware to change the settings for MEDIA_ROOT and STATICFILES_DIRS, but the documentation said i should not do this. How do i achieve what i wanted? thanks
class SimpleMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before the view (and later middleware) are called.
# sets to show Taiwan or Indo version
# sets the timezone too
the_host = request.get_host()
http_host = request.META['HTTP_HOST']
if(the_host == 'http://www.webName.com.tw' or the_host == 'http://webName.com.tw'):
#translation.activate('zh_TW')
# i am planning to change the SETTINGS in runtime here
request.urlconf = 'webName.urls_taiwan'
elif (the_host == 'http://www.webName.id' or the_host == 'http://webName.id'):
#translation.activate('in_ID')
# i am planning to change the SETTINGS in runtime here
request.urlconf = 'webName.urls_indonesia'
response = self.get_response(request)
# Code to be executed for each request/response after the view is called.
return response
When creating static folders name them like this:
appA/static/appA/*
appB/static/appB/*
When you use your static files in your html template you can:
{% static 'appA/something.js' %}
and
{% static 'appB/something.js' %}
I have not worked with 'media' files, but I assume you can do basically the same thing.
This is not the right approach.
Django includes support for collecting static files from different apps into a central location, via the collectstatic management command. You should run that on deploy.
For the media files, you should simply upload them to a central location.
I have a Flask-Admin project set up with Flask-Security as well. It is pretty much https://pythonhosted.org/Flask-Security/quickstart.html#id1 but just more advanced. I can access the login page at localhost/login and logout and localhost/logout. The logging in and logging out works.
The templates for Flask-Admin works and everything is displayed as I'd expect. However, there are no templates on my machine or docker container where the Flask-Admin app is run. I installed Flask by running pip install Flask-Admin. I know I can over ride the security log in by adding something like
SECURITY_LOGIN_USER_TEMPLATE = 'security/login_user.html'
to the config file and uploading to /templates/security/login_user.html. There is also using
{%extends base.html}
to have a common theme. Should I have template files already in my project?
Flask Security have a default login template, if you want to use your own template for login or register follow these steps:
Create in template folder the a subfolder named security
Add your html documents to this folder
Go to your flask configuration and add the following settings:
If your want the register functionality
SECURITY_REGISTERABLE = True
Add the name of your templates:
SECURITY_LOGIN_USER_TEMPLATE = 'security/login.html'
SECURITY_REGISTER_USER_TEMPLATE = 'security/register.html'
Remember to use the appropriate form in login.html and in register.html, usually causes doubts but is simple:
register.html: register_user_form.field
login.html: login_user_form.field
These are the configurations for this work correctly.
this repository can you to see and understand better doubt: