This commit is contained in:
Christian Cueni 2022-04-04 16:33:42 +02:00
parent 796db1e83b
commit c6adf1ca41
4 changed files with 16 additions and 12 deletions

View File

@ -88,6 +88,7 @@ THIRD_PARTY_APPS = [
LOCAL_APPS = [ LOCAL_APPS = [
"vbv_lernwelt.core", "vbv_lernwelt.core",
"vbv_lernwelt.simpletodo", "vbv_lernwelt.simpletodo",
"vbv_lernwelt.sso",
# Your stuff: custom apps go here # Your stuff: custom apps go here
] ]
# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps # https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
@ -452,14 +453,16 @@ if "django_redis.cache.RedisCache" in env("VBV_DJANGO_CACHE_BACKEND", default=""
OAUTH = { OAUTH = {
"client_name": env("OAUTH_CLIENT_NAME", default="lernetz"), "client_name": env("OAUTH_CLIENT_NAME", default="lernetz"),
"client_id": env("OAUTH_CLIENT_ID", default=""), "client_id": env("OAUTH_CLIENT_ID", default="iterativ"),
"client_secret": env("OAUTH_CLIENT_SECRET", default=""), "client_secret": env("OAUTH_CLIENT_SECRET", default=""),
"access_token_url": env("OAUTH_ACCESS_TOKEN_URL", default="https://sso.test.b.lernetz.host/auth/realms/vbv/protocol/openid-connect/token"), "access_token_url": env("OAUTH_ACCESS_TOKEN_URL", default="https://sso.test.b.lernetz.host/auth/realms/vbv/protocol/openid-connect/token"),
"authorize_url": env("OAUTH_AUTHORIZE_URL", default="https://sso.test.b.lernetz.host/auth/realms/vbv/protocol/openid-connect/tokenk"), "authorize_url": env("OAUTH_AUTHORIZE_URL", default="https://sso.test.b.lernetz.host/auth/realms/vbv/protocol/openid-connect/auth"),
"api_base_url": env("OAUTH_API_BASE_URL", default="https://sso.test.b.lernetz.host/auth/realms/vbv/protocol/openid-connect/"), "api_base_url": env("OAUTH_API_BASE_URL", default="https://sso.test.b.lernetz.host/auth/realms/vbv/protocol/openid-connect/"),
"local_redirect_uri": env("OAUTH_LOCAL_DIRECT_URI", default="http://localhost:8000/api/oauth/callback/"), "local_redirect_uri": env("OAUTH_LOCAL_DIRECT_URI", default="http://localhost:8000/sso/callback/"),
'client_kwargs': { "client_kwargs": {
'scope': '', 'scope': '',
'token_endpoint_auth_method': 'client_secret_post',
'token_placement': 'header',
} }
} }

View File

@ -36,6 +36,7 @@ urlpatterns = [
path("login/", django_view_authentication_exempt(auth_views.LoginView.as_view(template_name="core/login.html"))), path("login/", django_view_authentication_exempt(auth_views.LoginView.as_view(template_name="core/login.html"))),
path("checkratelimit/", check_rate_limit), path("checkratelimit/", check_rate_limit),
path("todo/", include("vbv_lernwelt.simpletodo.urls")), path("todo/", include("vbv_lernwelt.simpletodo.urls")),
path("sso/", include("vbv_lernwelt.sso.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG: if settings.DEBUG:
# Static file serving when using Gunicorn + Uvicorn for local web socket development # Static file serving when using Gunicorn + Uvicorn for local web socket development

View File

@ -3,12 +3,10 @@ from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from . import views from . import views
from ..core.middleware.auth import django_view_authentication_exempt
router = DefaultRouter()
app_name = 'sso' app_name = 'sso'
urlpatterns = [ urlpatterns = [
path(r'^login/', views.login, name='login'), path(r'login/', django_view_authentication_exempt(views.login), name='login'),
path(r'^callback/', views.authorize, name='authorize'), path(r'callback/', django_view_authentication_exempt(views.authorize), name='authorize'),
url(r"^sso/", include(router.urls)),
] ]

View File

@ -13,14 +13,16 @@ OAUTH_REDIRECT = 'oauth-redirect'
def login(request): def login(request):
hep_oauth_client = oauth.create_client('hep') oauth_client = oauth.create_client(settings.OAUTH["client_name"])
redirect_uri = settings.OAUTH_LOCAL_REDIRECT_URI redirect_uri = settings.OAUTH["local_redirect_uri"]
return hep_oauth_client.authorize_redirect(request, redirect_uri) return oauth_client.authorize_redirect(request, redirect_uri)
def authorize(request): def authorize(request):
try: try:
logger.debug(request) logger.debug(request)
token = oauth.lernetz.authorize_access_token(request)
print(token)
except OAuthError as e: except OAuthError as e:
logger.warning(f'OAuth error: {e}') logger.warning(f'OAuth error: {e}')
if not settings.DEBUG: if not settings.DEBUG: