core icon indicating copy to clipboard operation
core copied to clipboard

Fix for command_line authentication provider

Open mogorman opened this issue 1 year ago • 5 comments
trafficstars

Breaking change

Proposed change

Current implementation does not ever match the users that exist in the system. my changes fix that. It also changes new user creation. I mirrored creating a user the way to the frontend does so that you get a person associated with the new user.

Type of change

  • [ ] Dependency upgrade
  • [X] Bugfix (non-breaking change which fixes an issue)
  • [ ] New integration (thank you!)
  • [X] New feature (which adds functionality to an existing integration)
  • [ ] Deprecation (breaking change to happen in the future)
  • [ ] Breaking change (fix/feature causing existing functionality to break)
  • [ ] Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #105817 , #100184
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • [x] The code change is tested and works locally.
  • [ ] Local tests pass. Your PR cannot be merged unless tests pass
  • [ ] There is no commented out code in this PR.
  • [ ] I have followed the development checklist
  • [ ] I have followed the perfect PR recommendations
  • [ ] The code has been formatted using Ruff (ruff format homeassistant tests)
  • [ ] Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • [ ] The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • [ ] New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • [ ] Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

mogorman avatar Jan 06 '24 23:01 mogorman

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:

Learn more about our pull request process.

home-assistant[bot] avatar Jan 06 '24 23:01 home-assistant[bot]

With the following patch existing users are created as a person, too.

--- a/homeassistant/auth/providers/command_line.py	2024-02-04 01:41:34.460181490 +0100
+++ b/homeassistant/auth/providers/command_line.py	2024-02-04 01:46:55.952650748 +0100
@@ -118,6 +118,13 @@
         username = flow_result["username"].strip().casefold()

         users = await self.store.async_get_users()
+        hass = async_get_hass()
+        meta = self._user_meta.get(flow_result["username"], {})
+
+        pretty_name = meta.get("fullname")
+        if not pretty_name:
+            pretty_name = flow_result["username"]
+
         for user in users:
             if user.name and user.name.strip().casefold() != username:
                 continue
@@ -127,28 +134,34 @@

             for credential in await self.async_credentials():
                 if credential.data["username"] and credential.data["username"].strip().casefold() == username:
+                    coll: person.PersonStorageCollection = hass.data[person.DOMAIN][1]
+                    found = False
+                    for pers in coll.async_items():
+                        if pers.get(person.ATTR_USER_ID) == user.id:
+                            found = True
+                            break
+
+                    if "person" in hass.config.components and not found:
+                        await person.async_create_person(hass, pretty_name, user_id=user.id)
+
                     return credential

             cred = self.async_create_credentials({"username": username})
             await self.store.async_link_user(user, cred)
             return cred

-        hass = async_get_hass()
-        meta = self._user_meta.get(flow_result["username"], {})
-
         provider = _async_get_hass_provider(hass)
         await provider.async_initialize()

         user = await hass.auth.async_create_user(flow_result["username"], group_ids=[meta.get("group")])
         cred = await provider.async_get_or_create_credentials({"username": flow_result["username"]})

-        pretty_name = meta.get("fullname")
-        if not pretty_name:
-            pretty_name = flow_result["username"]
         await provider.data.async_save()
         await hass.auth.async_link_user(user, cred)
+
         if "person" in hass.config.components:
             await person.async_create_person(hass, pretty_name, user_id=user.id)
+
         # Create new credentials.
         return cred

SuperSandro2000 avatar Feb 04 '24 02:02 SuperSandro2000

I suggest adding additional functionality, such as automatically creating a user photo. I have my own LDAP script in Python that authenticates a user and uploads their photo to the ./image/ directory. It would be convenient to create a user immediately with a photo.

Losenmann avatar Mar 24 '24 03:03 Losenmann

Does command_line auth provider still work?

Daniel-dev22 avatar Apr 30 '24 16:04 Daniel-dev22

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes. Thank you for your contribution!

github-actions[bot] avatar Jun 29 '24 17:06 github-actions[bot]

.

SuperSandro2000 avatar Jul 01 '24 08:07 SuperSandro2000

Because there hasn't been any activity on this PR for quite some time now, I've decided to close it for being stale.

Feel free to re-open this PR when you are ready to pick up work on it again 👍

../Frenck

frenck avatar Jul 07 '24 15:07 frenck