Repository: netbox-community/netbox
Issue: REMOTE_AUTH_DEFAULT_GROUPS not adding new users to group

### NetBox version

v3.1.9

### Python version

3.8

### Steps to Reproduce

1. Create new group called 'Users' in Netbox admin
2. Add settings to configuration.py for remote auth and default group:
```
# Remote authentication support
REMOTE_AUTH_ENABLED = True

REMOTE_AUTH_BACKEND = 'social_core.backends.azuread_tenant.AzureADTenantOAuth2'

REMOTE_AUTH_DEFAULT_GROUPS = ['Users']

REMOTE_AUTH_GROUP_SYNC_ENABLED = False

SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_RESOURCE  = '[redacted]'
SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY       = '[redacted]'
SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET    = '[redacted]'
SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID = '[redacted]'
```

3. New user authenticates using the Azure auth provider into Netbox
4. User is added to Netbox, but user is not added to 'Users' group

### Expected Behavior

New users should be added to the group(s) indicted by the REMOTE_AUTH_DEFAULT_GROUPS setting.

### Observed Behavior

User is not added to group on sign-on.

Changed files:
netbox/netbox/settings.py, netbox/netbox/authentication.py

diff --git a/netbox/netbox/authentication.py b/netbox/netbox/authentication.py
index a13e8d192..00fb3ee66 100644
--- a/netbox/netbox/authentication.py
+++ b/netbox/netbox/authentication.py
@@ -348,3 +348,26 @@ class LDAPBackend:
             ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
 
         return obj
+
+
+# Custom Social Auth Pipeline Handlers
+def user_default_groups_handler(backend, user, response, *args, **kwargs):
+    """
+    Custom pipeline handler which adds remote auth users to the default group specified in the
+    configuration file.
+    """
+    logger = logging.getLogger('netbox.auth.user_default_groups_handler')
+    if settings.REMOTE_AUTH_DEFAULT_GROUPS:
+        # Assign default groups to the user
+        group_list = []
+        for name in settings.REMOTE_AUTH_DEFAULT_GROUPS:
+            try:
+                group_list.append(Group.objects.get(name=name))
+            except Group.DoesNotExist:
+                logging.error(
+                    f"Could not assign group {name} to remotely-authenticated user {user}: Group not found")
+        if group_list:
+            user.groups.add(*group_list)
+        else:
+            user.groups.clear()
+            logger.debug(f"Stripping user {user} from Groups")
diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py
index b2e1eca6c..c0df42a2b 100644
--- a/netbox/netbox/settings.py
+++ b/netbox/netbox/settings.py
@@ -483,6 +483,19 @@ for param in dir(configuration):
 
 SOCIAL_AUTH_JSONFIELD_ENABLED = True
 
+SOCIAL_AUTH_PIPELINE = (
+    'social_core.pipeline.social_auth.social_details',
+    'social_core.pipeline.social_auth.social_uid',
+    'social_core.pipeline.social_auth.social_user',
+    'social_core.pipeline.user.get_username',
+    'social_core.pipeline.social_auth.associate_by_email',
+    'social_core.pipeline.user.create_user',
+    'social_core.pipeline.social_auth.associate_user',
+    'netbox.authentication.user_default_groups_handler',
+    'social_core.pipeline.social_auth.load_extra_data',
+    'social_core.pipeline.user.user_details',
+)
+
 
 #
 # Django Prometheus