Аутентификация Django SAML2 с помощью AZURE

У меня есть приложение Django в продакшене на сервере Ubuntu 20.04, который использует python3.8.10, и я пытаюсь реализовать SSO-соединение с помощью pysaml2 с Azure. Я решил использовать пакет django-saml2-auth-ai.

Так что я настроил все правильно в соответствии с документом. Однако, когда я пробую свой url /accounts/login/, я получаю ошибку /saml/deny/. то же самое для /saml/acs/.

Я изменил 'DEFAULT_NEXT_URL' на /admin и теперь перенаправление работает с url /accounts/login/ НО у меня есть KeyError с атрибутами отображения :

    Traceback (most recent call last):
  File "/var/www/html/api/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/var/www/html/api/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/var/www/html/api/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/var/www/html/api/venv/lib/python3.8/site-packages/django_saml2_auth/views.py", line 219, in acs
    user_name = user_identity[

Exception Type: KeyError at /saml/acs/
Exception Value: 'user.displayname'

Вот мои атрибуты конфигурации на Azure : enter image description here

urls.py

import django_saml2_auth.views


urlpatterns = [
    path('saml/', include('django_saml2_auth.urls')),
    path('accounts/login/', django_saml2_auth.views.signin),
    path('', login_, name="login"),
    path('admin/', admin.site.urls),
    path('accounts/', include('accounts.urls')),
]

settings.py

# more code...

LOGIN_URL = "/accounts/login"
LOGIN_REDIRECT_URL = 'accounts:index'

# more code...

SAML2_AUTH = {
'SAML_CLIENT_SETTINGS': {  # Pysaml2 Saml client settings
    'entityid': 'https://mysiteweb.com/',

    'metadata': {
        'local': [
            os.path.join(BASE_DIR, 'mysiteweb.xml'),
        ],
    },
    'service': {
        'sp': {
            'logout_requests_signed': True,
            'idp': 'https://sts.windows.net/83d4d*****-*****-***/'
        }
    }
},
'debug': 1,
'DEFAULT_NEXT_URL': 'https://mysiteweb.com/',
'NEW_USER_PROFILE': {
    'USER_GROUPS': [],  # The default group name when a new user logs in
    'ACTIVE_STATUS': True,  # The default active status for new users
    'STAFF_STATUS': False,  # The staff status for new users
    'SUPERUSER_STATUS': False,  # The superuser status for new users
},
'ATTRIBUTES_MAP': {  # Change Email/UserName/FirstName/LastName to corresponding SAML2 userprofile attributes.
    'email': 'user.userprincipalname',
    'username': 'user.displayname',
    'first_name': 'user.givenname',
    'last_name': 'user.surname',
},

'ASSERTION_URL': 'https://mysiteweb.com',
}

Я использую SAML-TRACER на chrome и et Django LOGGERS вот так :

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/var/www/html/api/api/debug.log',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'saml2': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}
Вернуться на верх