Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Set unusable password on OIDC users (#12103)

Rafał Pitońbf69b32860e2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 44ab11d11d..7cff48bd2f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -65,6 +65,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Added native support for gzip compression - #11833 by @patrys
- Set flat rates as the default tax calculation strategy - #12069 by @maarcingebala
- Enables flat rates for channels in which no tax calculation method was set.
+- Users created by the OIDC plugin now have unusable password set instead of empty string - #12103 by @rafalp
# 3.11.0
diff --git a/saleor/account/migrations/0076_fill_empty_passwords.py b/saleor/account/migrations/0076_fill_empty_passwords.py
new file mode 100644
index 0000000000..731cab44e5
--- /dev/null
+++ b/saleor/account/migrations/0076_fill_empty_passwords.py
@@ -0,0 +1,22 @@
+# Generated by Django 3.2.18 on 2023-02-22 16:40
+
+from django.contrib.auth.hashers import make_password
+from django.db import migrations
+
+
+def fill_empty_passwords_on_users(apps, _schema_editor):
+ User = apps.get_model("account", "User")
+ User.objects.filter(password="").update(password=make_password(None))
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("account", "0075_add_address_metadata"),
+ ]
+
+ operations = [
+ migrations.RunPython(
+ fill_empty_passwords_on_users,
+ reverse_code=migrations.RunPython.noop,
+ ),
+ ]
diff --git a/saleor/plugins/openid_connect/tests/test_utils.py b/saleor/plugins/openid_connect/tests/test_utils.py
index fe829b7562..ca194ecdb3 100644
--- a/saleor/plugins/openid_connect/tests/test_utils.py
+++ b/saleor/plugins/openid_connect/tests/test_utils.py
@@ -267,6 +267,7 @@ def test_get_or_create_user_from_payload_creates_user_with_sub():
assert user_from_payload.email == customer_email
assert user_from_payload.private_metadata[f"oidc-{oauth_url}"] == sub_id
+ assert not user_from_payload.has_usable_password()
def test_get_or_create_user_from_payload_multiple_subs(customer_user, admin_user):
diff --git a/saleor/plugins/openid_connect/utils.py b/saleor/plugins/openid_connect/utils.py
index 513e9b4501..bf11de2684 100644
--- a/saleor/plugins/openid_connect/utils.py
+++ b/saleor/plugins/openid_connect/utils.py
@@ -7,6 +7,7 @@ import requests
from authlib.jose import JWTClaims, jwt
from authlib.jose.errors import DecodeError, JoseError
from authlib.oidc.core import CodeIDToken
+from django.contrib.auth.hashers import make_password
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
@@ -332,6 +333,7 @@ def get_or_create_user_from_payload(
"first_name": payload.get("given_name", ""),
"last_name": payload.get("family_name", ""),
"private_metadata": {oidc_metadata_key: sub},
+ "password": make_password(None),
}
try:
user = User.objects.get(**get_kwargs)