Skip to content

Instantly share code, notes, and snippets.

@Mabuchin
Forked from leoluk/custom_middleware.py
Created February 20, 2019 10:32
Show Gist options
  • Save Mabuchin/e4034b97dc2fa54d70e595859af42a73 to your computer and use it in GitHub Desktop.
Save Mabuchin/e4034b97dc2fa54d70e595859af42a73 to your computer and use it in GitHub Desktop.
Netbox OAuth Login
"""
Custom LOGIN_REQUIRED middleware which allows OAuth URLs.
"""
import utilities.middleware
from django.conf import settings
class CustomLoginRequiredMiddleware(utilities.middleware.LoginRequiredMiddleware):
def __call__(self, request):
if settings.LOGIN_REQUIRED and not request.user.is_authenticated:
if request.path_info.startswith('/oauth/'):
return self.get_response(request)
return super(CustomLoginRequiredMiddleware, self).__call__(request)
"""
Override upstream urls.py for OAuth login
"""
from netbox.urls import *
urlpatterns = urlpatterns + [
url(r'^oauth/', include('social_django.urls', namespace='social')),
]
"""
Override the Netbox settings.py for customizations outside of configuration.py.
"""
import os
from netbox.upstream_settings import *
# OAuth monkey patching
MIDDLEWARE = [
'netbox.custom_middleware.CustomLoginRequiredMiddleware' if
x == 'utilities.middleware.LoginRequiredMiddleware' else x
for x in MIDDLEWARE]
AUTHENTICATION_BACKENDS = (
'foo.CustomOAuthProvider',
)
SOCIAL_AUTH_FOO_ADMIN_KEY = os.getenv('FOO_OAUTH_CLIENT', '')
SOCIAL_AUTH_FOO_ADMIN_SECRET = os.getenv('FOO_OAUTH_SECRET', '')
LOGIN_URL = '/oauth/login/foo-admin/'
ROOT_URLCONF = 'netbox.custom_urls'
INSTALLED_APPS = INSTALLED_APPS + [
'social_django',
]
SOCIAL_AUTH_POSTGRES_JSONFIELD = True
# Custom Netbox modifications
mv netbox/netbox/settings.py netbox/netbox/upstream_settings.py
cp /opt/app-root/etc/settings.py netbox/netbox/settings.py
cp /opt/app-root/etc/custom_urls.py netbox/netbox/custom_urls.py
cp /opt/app-root/etc/custom_middleware.py netbox/netbox/custom_middleware.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment