Skip to content
Snippets Groups Projects
Commit 27b6c719 authored by jan.bednarik's avatar jan.bednarik
Browse files

Signal for post_login event

parent f8b38154
No related branches found
No related tags found
1 merge request!1Signal for post_login event
...@@ -4,6 +4,8 @@ Django app na uživatele, týmy a skupiny, s napojením na LDAP a SSO. ...@@ -4,6 +4,8 @@ Django app na uživatele, týmy a skupiny, s napojením na LDAP a SSO.
[![code style: Black](https://img.shields.io/badge/code%20style-Black-000000)](https://github.com/psf/black) [![code style: Black](https://img.shields.io/badge/code%20style-Black-000000)](https://github.com/psf/black)
[![license MIT](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE) [![license MIT](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE)
![Python Version](https://img.shields.io/pypi/pyversions/pirates)
![Django Version](https://img.shields.io/pypi/djversions/pirates?color=0C4B33)
## Použití ## Použití
...@@ -73,3 +75,13 @@ OIDC_OP_USER_ENDPOINT = join(OIDC_RP_REALM_URL, "protocol/openid-connect/userinf ...@@ -73,3 +75,13 @@ OIDC_OP_USER_ENDPOINT = join(OIDC_RP_REALM_URL, "protocol/openid-connect/userinf
``` ```
URL patterns pro OpenID Connect už jsou součástí `pirates.urls` (viz výše). URL patterns pro OpenID Connect už jsou součástí `pirates.urls` (viz výše).
#### Signál po přihlášení
Po přihlášení uživatele je poslán signál `pirates.signals.post_login` s
parametry:
* `sender` - `PiratesOIDCAuthenticationBackend`
* `user` - přihlášený uživatel (instance `AUTH_USER_MODEL`)
* `created` - `True`/`False` zda-li byl vytvořen nový uživatel
* `request` - instance `HttpRequest`
from mozilla_django_oidc.auth import OIDCAuthenticationBackend from mozilla_django_oidc.auth import OIDCAuthenticationBackend
from .signals import post_login
class PiratesOIDCAuthenticationBackend(OIDCAuthenticationBackend): class PiratesOIDCAuthenticationBackend(OIDCAuthenticationBackend):
""" """
...@@ -23,13 +25,25 @@ class PiratesOIDCAuthenticationBackend(OIDCAuthenticationBackend): ...@@ -23,13 +25,25 @@ class PiratesOIDCAuthenticationBackend(OIDCAuthenticationBackend):
first_name = claims.get("given_name", "") first_name = claims.get("given_name", "")
last_name = claims.get("family_name", "") last_name = claims.get("family_name", "")
email = claims.get("email", "") email = claims.get("email", "")
return self.UserModel.objects.create( user = self.UserModel.objects.create(
sso_id=sso_id, first_name=first_name, last_name=last_name, email=email sso_id=sso_id, first_name=first_name, last_name=last_name, email=email
) )
self.send_post_login_signal(user, True, claims)
return user
def update_user(self, user, claims): def update_user(self, user, claims):
user.first_name = claims.get("given_name", "") user.first_name = claims.get("given_name", "")
user.last_name = claims.get("family_name", "") user.last_name = claims.get("family_name", "")
user.email = claims.get("email", "") user.email = claims.get("email", "")
user.save() user.save()
self.send_post_login_signal(user, False, claims)
return user return user
def send_post_login_signal(self, user, created, claims):
post_login.send(
sender=self.__class__,
user=user,
created=created,
claims=claims,
request=self.request,
)
from django.dispatch import Signal
post_login = Signal()
...@@ -13,7 +13,7 @@ def read(fname): ...@@ -13,7 +13,7 @@ def read(fname):
setup( setup(
name="pirates", name="pirates",
version="0.3.1", version="0.4.0",
license="MIT", license="MIT",
description="Django app for users, teamds and groups.", description="Django app for users, teamds and groups.",
long_description=read("README.md"), long_description=read("README.md"),
...@@ -34,7 +34,12 @@ setup( ...@@ -34,7 +34,12 @@ setup(
"Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: CPython",
"Framework :: Django",
"Framework :: Django :: 2.2",
"Framework :: Django :: 3.0",
"Framework :: Django :: 3.1",
"Topic :: Utilities", "Topic :: Utilities",
], ],
project_urls={ project_urls={
...@@ -44,5 +49,5 @@ setup( ...@@ -44,5 +49,5 @@ setup(
}, },
keywords=["django", "openid", "sso"], keywords=["django", "openid", "sso"],
python_requires=">=3.6", python_requires=">=3.6",
install_requires=["mozilla-django-oidc>=1.2.3,<2", "python-ldap>=3.2.0,<4"], install_requires=["mozilla-django-oidc>=1.2.4,<2", "python-ldap>=3.2.0,<4"],
) )
...@@ -2,3 +2,4 @@ pytest ...@@ -2,3 +2,4 @@ pytest
pytest-cov pytest-cov
pytest-factoryboy pytest-factoryboy
pytest-django pytest-django
pytest-mock
...@@ -10,8 +10,10 @@ fake = Faker() ...@@ -10,8 +10,10 @@ fake = Faker()
@pytest.fixture @pytest.fixture
def backend(): def backend(mocker):
return PiratesOIDCAuthenticationBackend() instance = PiratesOIDCAuthenticationBackend()
instance.request = mocker.Mock()
return instance
def test_auth_backend__get_sso_id(backend): def test_auth_backend__get_sso_id(backend):
...@@ -69,3 +71,39 @@ def test_auth_backend__update_user(backend, user): ...@@ -69,3 +71,39 @@ def test_auth_backend__update_user(backend, user):
assert updated_user.last_name == claims["family_name"] assert updated_user.last_name == claims["family_name"]
assert updated_user.email == claims["email"] assert updated_user.email == claims["email"]
assert get_user_model().objects.get() == updated_user assert get_user_model().objects.get() == updated_user
def test_auth_backend__create_user__send_post_login(backend, mocker):
m_post_login = mocker.patch("pirates.auth.post_login")
claims = {
"sub": fake.random_letters(),
"given_name": fake.first_name(),
"family_name": fake.last_name(),
"email": fake.email(),
}
user = backend.create_user(claims)
m_post_login.send.assert_called_once_with(
sender=backend.__class__,
user=user,
created=True,
claims=claims,
request=backend.request,
)
def test_auth_backend__update_user__send_post_login(backend, user, mocker):
m_post_login = mocker.patch("pirates.auth.post_login")
claims = {
"sub": fake.random_letters(),
"given_name": fake.first_name(),
"family_name": fake.last_name(),
"email": fake.email(),
}
updated_user = backend.update_user(user, claims)
m_post_login.send.assert_called_once_with(
sender=backend.__class__,
user=updated_user,
created=False,
claims=claims,
request=backend.request,
)
[tox] [tox]
envlist = envlist =
py{36,37,38}-django{30,22} py{36,37,38,39}-django{31,30,22}
[testenv] [testenv]
deps = deps =
-r{toxinidir}/tests/requirements.txt -r{toxinidir}/tests/requirements.txt
django31: Django>=3.1,<3.2
django30: Django>=3.0,<3.1 django30: Django>=3.0,<3.1
django22: Django>=2.2,<3 django22: Django>=2.2,<3
setenv = setenv =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment