Login

Tag "middleware"

Snippet List

Django Standard API Response Middleware for DRF

As you can see, if you using django-rest-framework, you will found many different response format. This middleware to solve all of these problems with Standard API Response. All HTTP Response status stored into json response, not in HTTP Status (because mobile application, like android can't fetch the response body when HTTP Status >= 400).

  • middleware
  • django
  • api
  • django-rest-framework
Read More

Geoip middleware to restrict users to a set of allowed countries

This middleware uses Django's Geoip support (https://docs.djangoproject.com/fr/2.2/ref/contrib/gis/geoip2/), as well as axes's package helper to retrieve IP address (since Django's REMOTE_ADDR might be wrong when behind a reverse proxy). Ensure your geolite DB files are up to date (eg. with https://djangosnippets.org/snippets/10674/). The checker is optional, but ensures that security is not broken due to a misspelled/missing GEOIP_COUNTRY_WHITELIST.

  • middleware
  • ip
  • country
  • geoip
  • restriction
  • blocker
Read More

LoginRequiredMiddleware

### settings.py MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'utils.LoginRequiredMiddleware', ] LOGIN_REQUIRED_URLS = [ r'^panel/(.*)$' ] this will help any url under `panel/` require login.

  • middleware
  • authentication
  • login_required
Read More

Django Online Now Users - Middleware

django online users, usage: `{{ request.online_now }}` or `{{ request.online_now_ids }}`, complete tutorial: https://python.web.id/blog/django-count-online-users/, this snippet forked from: https://gist.github.com/dfalk/1472104

  • middleware
  • django
  • session
  • cache
Read More

JSON Web Token authentication middleware

This hasn't been thoroughly tested yet but so far it works great. We had no use for sessions or the built in authentication middleware for django as this was built to be a microservice for authentication. Unfortunately if you just use the django rest framework-jwt package the authentication occurs at the view level meaning request.user.is_authenticated() will always return False. We have a few internal non-api views that needed @login_required. We have a stripped down version of django that is very performant that we are using for microservices with built-in authorization using JSON Web Tokens. This service is authentication which has access to a `users` table. Any questions or curious how well lightweight django is working for microservices, or we he are doing the the authorization on the other services, or just improvements please drop a line - thanks.

  • middleware
  • authentication
  • json web token
  • django-rest-framework
  • JWT
Read More

Middleware for fixing Microsoft Office (Word, Excel, Powerpoint) hyperlinks to views requiring authentication

Hyperlinks to views requiring authentication in Microsoft Office (Word, Excel, Powerpoint) can fail based on how Office handles rendering. This middleware sends a refresh return to the client, which will allow the page to be opened as normal, instead of the "Unable to open ... Cannot download the information you requested." This is a port from the ruby middleware project [fix microsoft links](https://github.com/spilliton/fix_microsoft_links). To enable, add to your [middleware stack](https://docs.djangoproject.com/en/dev/topics/http/middleware/) in the django project settings.

  • middleware
  • office
  • word
  • excel
  • powerpoint
Read More

LocaleMiddleware without browser language discovery

This snippet holds your Django project from automatically changing language of the page to the best fitting one by discovering the client browser language. I personally needed to show the page to the user for the first time in the default language (English), although there were some translations. User can still change the language (via session cookies). Insert this middleware BEFORE the Django's `django.middleware.locale.LocaleMiddleware` in settings.

  • middleware
  • i18n
  • locale
Read More

SSL Force Middleware

A simple way to force SSL on all pages. It's very simple at this point - the only issue I can see right now and I will address this later is if someone sends http:// in another portion of your url.

  • middleware
  • django
  • ssl
  • secure
Read More

181 snippets posted so far.