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.


Discussion:
Can confirm this is also happening for me, also using social auth. Users are created but not added to group. Netbox ccd14dc6bbd5 (v3.1.9) via netbox-docker.

```
LOGIN_REQUIRED=True
REMOTE_AUTH_ENABLED=True
REMOTE_AUTH_AUTO_CREATE_USER=True
REMOTE_AUTH_BACKEND='social_core.backends.emf.EmfOAuth2'
SOCIAL_AUTH_EMF_KEY='xxx'
SOCIAL_AUTH_EMF_SECRET='xxx'
REMOTE_AUTH_DEFAULT_GROUPS=['noc']
```
Same thing here when using Azure AD. I'm using Netbox v3.1.11

# Remote authentication support
REMOTE_AUTH_ENABLED = True
REMOTE_AUTH_BACKEND = 'social_core.backends.azuread.AzureADOAuth2'
SOCIAL_AUTH_AZUREAD_OAUTH2_KEY = 'xxx'   
SOCIAL_AUTH_AZUREAD_OAUTH2_SECRET = 'xxx'
REMOTE_AUTH_DEFAULT_GROUPS=['AAD-NRE-Apps']
REMOTE_AUTH_HEADER = 'HTTP_REMOTE_USER'
REMOTE_AUTH_AUTO_CREATE_USER = True
REMOTE_AUTH_DEFAULT_PERMISSIONS = {}

Having same issue on Netbox-Docker v3.2-1.6.1


```
REMOTE_AUTH_ENABLED = True
REMOTE_AUTH_BACKEND = 'social_core.backends.azuread.AzureADOAuth2'
SOCIAL_AUTH_AZUREAD_OAUTH2_KEY = 'xxx'
SOCIAL_AUTH_AZUREAD_OAUTH2_SECRET = 'xxx'
SOCIAL_AUTH_REDIRECT_IS_HTTPS = True
REMOTE_AUTH_AUTO_CREATE_USER = True
REMOTE_AUTH_DEFAULT_GROUPS=['superduperadmins']
```
We have the same issue via Okta. We are sending the groups via the token and just the actual user is created as default.
We have the same issue via Okta.
We are sending the groups via the token and just the actual user is created as default in Netbox
Same issue on NetBox 3.2.3
I've tried assigning the role through the App registration side as well didn't work, Users are created with no role/group.
Instead of having a default group it would be good to use the AAD role assignment. Then you can assign users their permissions based off AAD groups/roles not just lump all SSO users together.

I have a working solution for this, at least for setting the default groups. Need to do a little cleanup/refactoring and I'll submit a PR.

Resolving patch:
diff --git a/netbox/netbox/authentication.py b/netbox/netbox/authentication.py
--- a/netbox/netbox/authentication.py
+++ b/netbox/netbox/authentication.py
@@ -348,3 +348,26 @@ def __new__(cls, *args, **kwargs):
             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
--- a/netbox/netbox/settings.py
+++ b/netbox/netbox/settings.py
@@ -483,6 +483,19 @@ def _setting(name, default=None):
 
 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


Test patch: