-
-
Save Mabuchin/e4034b97dc2fa54d70e595859af42a73 to your computer and use it in GitHub Desktop.
Netbox OAuth Login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Override upstream urls.py for OAuth login | |
""" | |
from netbox.urls import * | |
urlpatterns = urlpatterns + [ | |
url(r'^oauth/', include('social_django.urls', namespace='social')), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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