Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code structure refactor #5582

Merged
merged 105 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
2684b7b
moved docker files to /contrib/container
matmair Sep 21, 2023
b82c638
changed code owners to make more precise
matmair Sep 21, 2023
590cc4a
updated CI to use new subdirs
matmair Sep 21, 2023
f3a67a7
added manual trigger for testing
matmair Sep 21, 2023
36dee59
moved ci files
matmair Sep 21, 2023
c204e74
moved assets into subdir
matmair Sep 21, 2023
3039cc5
moved deploy template file to contrib
matmair Sep 21, 2023
e603f06
moved django files to src/backend
matmair Sep 21, 2023
92e9270
updated paths in scripts etc
matmair Sep 21, 2023
08f110a
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Sep 27, 2023
b948110
updated reqs path
matmair Sep 27, 2023
c41bd25
fixed version file path
matmair Sep 27, 2023
8932a19
fixed flake8 path
matmair Sep 27, 2023
a38caa8
fixed path to node ressources
matmair Sep 28, 2023
ecb9711
fixed task paths
matmair Sep 28, 2023
6417f01
added dep path for node
matmair Sep 28, 2023
9eddc6f
removed unused yarn lockfile
matmair Sep 28, 2023
8ff63c4
removed unused ci script
matmair Sep 28, 2023
c523a3c
updated internal backend paths for tasks
matmair Sep 28, 2023
b2c069c
updated translation stats path
matmair Sep 28, 2023
5a77615
fixed source path for coverage
matmair Sep 28, 2023
d527443
fixed main commit repo path
matmair Sep 28, 2023
211b644
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Sep 28, 2023
62de738
[FR] Restructure code
matmair Oct 3, 2023
6df2aec
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Oct 4, 2023
25c718a
[FR] Restructure code
matmair Oct 11, 2023
0cb0f03
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Oct 16, 2023
f5592b3
fit in changes from testing
matmair Oct 16, 2023
40a6e7e
gather packager improvements (#149)
matmair Oct 16, 2023
66ea021
Added docs for file structure
matmair Oct 16, 2023
c406875
Merge branch 'matmair/issue5578' of https://github.com/matmair/InvenT…
matmair Oct 16, 2023
968fb94
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Oct 26, 2023
f492e8e
Fixed style errors
matmair Oct 26, 2023
23fad6d
updated deepsource paths
matmair Oct 26, 2023
23be254
fix deepsource paths
matmair Oct 26, 2023
3006d3e
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Nov 4, 2023
35f31cd
fixed reqs
matmair Nov 4, 2023
ef9b45a
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Nov 29, 2023
f6d8588
[FR] Restructure code
matmair Dec 16, 2023
a004fdd
merge fixes
matmair Dec 16, 2023
27d8606
move newly added dirs too
matmair Dec 16, 2023
53f2c10
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Dec 16, 2023
e8131ad
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Jan 1, 2024
c1ba7a8
fix reqs files
matmair Jan 1, 2024
9d0bfd5
another dep fix
matmair Jan 1, 2024
584aa36
merge upstream/master
matmair Jan 10, 2024
ccbb3ca
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Jan 10, 2024
4dc8309
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Jan 11, 2024
fdb72bd
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Jan 14, 2024
23cd6be
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Jan 15, 2024
af8b86a
revert removal of tags
matmair Jan 15, 2024
fb918cf
merge upstream
matmair Jan 17, 2024
53d7ba0
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Jan 17, 2024
4859d43
enabled detection of old config files
matmair Jan 17, 2024
897c73e
adapt coverage src
matmair Jan 17, 2024
773431f
also detect and support old location for plugins.txt
matmair Jan 17, 2024
1e52c07
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Jan 17, 2024
5f58030
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Jan 24, 2024
9aeb641
style fix
matmair Jan 24, 2024
c48995f
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Jan 31, 2024
a2dc63a
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Feb 9, 2024
b4c1ef4
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Feb 13, 2024
da7c161
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Feb 28, 2024
59db188
fix ~/init.sh location
matmair Mar 15, 2024
21f7e34
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Mar 16, 2024
e3c9423
Merge branch 'matmair/issue5578' of https://github.com/matmair/InvenT…
matmair Mar 16, 2024
a3f8eca
fix requirements path
matmair Mar 16, 2024
91910c5
fix config to current master
matmair Mar 16, 2024
fc4e0e4
move new folders
matmair Mar 16, 2024
328f20b
fix import order
matmair Mar 16, 2024
2e02556
fix paths for qc_check
matmair Mar 16, 2024
66504f7
fix docs build
matmair Mar 16, 2024
2251f33
fix fix path
matmair Mar 16, 2024
b6d4044
set docker project dir
matmair Mar 16, 2024
4f41fa1
just use a cd
matmair Mar 16, 2024
be67a14
set image path?
matmair Mar 16, 2024
c35f621
set file correct
matmair Mar 16, 2024
1c22e03
fix copy path
matmair Mar 16, 2024
e02e446
fix tasks dir
matmair Mar 16, 2024
65c3a37
fix init path
matmair Mar 16, 2024
ad0d65d
fix copy path
matmair Mar 16, 2024
488581e
set prject dir
matmair Mar 16, 2024
c5fc567
fix paths
matmair Mar 16, 2024
2f3a969
remove old prod files
matmair Mar 16, 2024
a9c1dfe
fix dev env path
matmair Mar 16, 2024
affda78
set docker file
matmair Mar 16, 2024
f65f68e
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Mar 26, 2024
f1b9a2e
Merge branch 'master' of https://github.com/inventree/InvenTree into …
matmair Mar 27, 2024
fa68360
Merge branch 'master' into code-structure
SchrodingersGat Mar 27, 2024
ded7558
Fix devcontainer docker compose file
SchrodingersGat Mar 28, 2024
90c2779
fix login attempt values
matmair Mar 28, 2024
532ef4f
fix init.sh path
matmair Mar 28, 2024
c2d5172
Merge remote-tracking branch 'matmair/matmair/issue5578' into code-st…
SchrodingersGat Mar 28, 2024
386c252
Fix pathing for Docker
SchrodingersGat Mar 28, 2024
5328406
Docker build fix
SchrodingersGat Mar 28, 2024
120b9b7
Update init.sh
SchrodingersGat Mar 28, 2024
6614373
Merge branch 'master' into code-refactor
SchrodingersGat Apr 2, 2024
8e6f572
Fix path
SchrodingersGat Apr 2, 2024
99f7462
Merge remote-tracking branch 'origin/master' into code-refactor
SchrodingersGat Apr 2, 2024
caada8e
Update requirements.txt
SchrodingersGat Apr 2, 2024
58d79f8
Merge remote-tracking branch 'origin/master' into code-refactor
SchrodingersGat Apr 2, 2024
b8c4aab
merge
matmair Apr 2, 2024
32da46c
Merge branch 'matmair/issue5578' of https://github.com/matmair/InvenT…
matmair Apr 2, 2024
1c7d6ce
fix rq merge
matmair Apr 2, 2024
1e320d0
fix docker compose usage
matmair Apr 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' of https://github.com/inventree/InvenTree into …
…matmair/issue5578
  • Loading branch information
matmair committed Jan 1, 2024
commit e8131ad791ce57e2369595ce319be5609d40ed25
2 changes: 1 addition & 1 deletion contrib/packager.io/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function update_or_install() {
# Run update as app user
echo "# Updating InvenTree"
sudo -u ${APP_USER} --preserve-env=$SETUP_ENVS bash -c "cd ${APP_HOME} && pip install wheel"
sudo -u ${APP_USER} --preserve-env=$SETUP_ENVS bash -c "cd ${APP_HOME} && invoke update --no-frontend | sed -e 's/^/# inv update| /;'"
sudo -u ${APP_USER} --preserve-env=$SETUP_ENVS bash -c "cd ${APP_HOME} && invoke update | sed -e 's/^/# inv update| /;'"

# Make sure permissions are correct again
echo "# Set permissions for data dir and media: ${DATA_DIR}"
Expand Down
7 changes: 7 additions & 0 deletions docs/docs/settings/SSO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ InvenTree provides the possibility to use 3rd party services to authenticate use
!!! tip "Provider Documentation"
There are a lot of technical considerations when configuring a particular SSO provider. A good starting point is the [django-allauth documentation](https://django-allauth.readthedocs.io/en/latest/socialaccount/providers/index.html)

!!! warning "Advanced Users"
The SSO functionality provided by django-allauth is powerful, but can prove challenging to configure. Please ensure that you understand the implications of enabling SSO for your InvenTree instance. Specific technical details of each available SSO provider are beyond the scope of this documentation - please refer to the [django-allauth documentation](https://django-allauth.readthedocs.io/en/latest/socialaccount/providers/index.html) for more information.

## SSO Configuration

The basic requirements for configuring SSO are outlined below:
Expand Down Expand Up @@ -131,3 +134,7 @@ Make sure all users with admin privileges have sufficient passwords - they can r

!!! warning "It's a secret!"
Never share the secret key associated with your InvenTree install!

## Error Handling

If you encounter an error during the SSO process, the error should be logged in the InvenTree database. You can view the [error log](./logs.md) in the [admin interface](./admin.md) to see the details of the error.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defusedxml==0.7.1
# python3-openid
diff-match-patch==20230430
# via django-import-export
dj-rest-auth==5.0.1
dj-rest-auth==5.0.2
# via -r requirements.in
django==3.2.23
# via
Expand Down Expand Up @@ -83,7 +83,7 @@ django==3.2.23
# djangorestframework
# djangorestframework-simplejwt
# drf-spectacular
django-allauth==0.54.0
django-allauth==0.59.0
# via
# -r requirements.in
# django-allauth-2fa
Expand Down
12 changes: 10 additions & 2 deletions src/backend/InvenTree/InvenTree/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from allauth.account.adapter import DefaultAccountAdapter
from allauth.account.forms import LoginForm, SignupForm, set_form_field_order
from allauth.exceptions import ImmediateHttpResponse
from allauth.core.exceptions import ImmediateHttpResponse
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth_2fa.adapter import OTPAdapter
from allauth_2fa.utils import user_has_valid_totp_device
Expand All @@ -24,6 +24,7 @@
from dj_rest_auth.registration.serializers import RegisterSerializer
from rest_framework import serializers

import InvenTree.sso
from common.models import InvenTreeSetting
from InvenTree.exceptions import log_error

Expand Down Expand Up @@ -228,7 +229,7 @@ def clean(self):

def registration_enabled():
"""Determine whether user registration is enabled."""
if InvenTreeSetting.get_setting('LOGIN_ENABLE_REG') or InvenTreeSetting.get_setting('LOGIN_ENABLE_SSO_REG'):
if InvenTreeSetting.get_setting('LOGIN_ENABLE_REG') or InvenTree.sso.registration_enabled():
if settings.EMAIL_HOST:
return True
else:
Expand Down Expand Up @@ -358,6 +359,13 @@ def login(self, request, user):
# Otherwise defer to the original allauth adapter.
return super().login(request, user)

def authentication_error(self, request, provider_id, error=None, exception=None, extra_context=None):
"""Callback method for authentication errors."""

# Log the error to the database
log_error(request.path if request else 'sso')
logger.error("SSO error for provider '%s' - check admin error log", provider_id)


# override dj-rest-auth
class CustomRegisterSerializer(RegisterSerializer):
Expand Down
8 changes: 8 additions & 0 deletions src/backend/InvenTree/InvenTree/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@
'InvenTree.middleware.InvenTreeRemoteUserMiddleware', # Remote / proxy auth
'django_otp.middleware.OTPMiddleware', # MFA support
'InvenTree.middleware.CustomAllauthTwoFactorMiddleware', # Flow control for allauth
'allauth.account.middleware.AccountMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'InvenTree.middleware.AuthRequiredMiddleware',
Expand Down Expand Up @@ -956,6 +957,13 @@
SOCIAL_BACKENDS = get_setting('INVENTREE_SOCIAL_BACKENDS', 'social_backends', [], typecast=list)

for app in SOCIAL_BACKENDS:

# Ensure that the app starts with 'allauth.socialaccount.providers'
social_prefix = 'allauth.socialaccount.providers.'

if not app.startswith(social_prefix): # pragma: no cover
app = social_prefix + app

INSTALLED_APPS.append(app) # pragma: no cover

SOCIALACCOUNT_PROVIDERS = get_setting('INVENTREE_SOCIAL_PROVIDERS', 'social_providers', None, typecast=dict)
Expand Down
46 changes: 21 additions & 25 deletions src/backend/InvenTree/InvenTree/social_auth_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
import logging
from importlib import import_module

from django.urls import include, path, reverse
from django.urls import NoReverseMatch, include, path, reverse

from allauth.account.models import EmailAddress
from allauth.socialaccount import providers
from allauth.socialaccount.models import SocialApp
from allauth.socialaccount.providers.keycloak.views import \
KeycloakOAuth2Adapter
from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter,
OAuth2LoginView)
from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework.exceptions import NotFound
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response

import InvenTree.sso
from common.models import InvenTreeSetting
from InvenTree.mixins import CreateAPI, ListAPI, ListCreateAPI
from InvenTree.serializers import InvenTreeModelSerializer
Expand Down Expand Up @@ -51,14 +49,6 @@ def handle_oauth2(adapter: OAuth2Adapter):
]


def handle_keycloak():
"""Define urls for keycloak."""
return [
path('login/', GenericOAuth2ApiLoginView.adapter_view(KeycloakOAuth2Adapter), name='keycloak_api_login'),
path('connect/', GenericOAuth2ApiConnectView.adapter_view(KeycloakOAuth2Adapter), name='keycloak_api_connet'),
]


legacy = {
'twitter': 'twitter_oauth2',
'bitbucket': 'bitbucket_oauth2',
Expand All @@ -72,10 +62,13 @@ def handle_keycloak():
social_auth_urlpatterns = []

provider_urlpatterns = []
for provider in providers.registry.get_list():

for name, provider in providers.registry.provider_map.items():

try:
prov_mod = import_module(provider.get_package() + ".views")
except ImportError:
logger.exception("Could not import authentication provider %s", name)
continue

# Try to extract the adapter class
Expand All @@ -89,8 +82,6 @@ def handle_keycloak():
if provider.id in legacy:
logger.warning('`%s` is not supported on platform UI. Use `%s` instead.', provider.id, legacy[provider.id])
continue
elif provider.id == 'keycloak':
urls = handle_keycloak()
else:
logger.error('Found handler that is not yet ready for platform UI: `%s`. Open an feature request on GitHub if you need it implemented.', provider.id)
continue
Expand All @@ -107,26 +98,31 @@ class SocialProviderListView(ListAPI):
def get(self, request, *args, **kwargs):
"""Get the list of providers."""
provider_list = []
for provider in providers.registry.get_list():
for provider in providers.registry.provider_map.values():
provider_data = {
'id': provider.id,
'name': provider.name,
'login': request.build_absolute_uri(reverse(f'{provider.id}_api_login')),
'connect': request.build_absolute_uri(reverse(f'{provider.id}_api_connect')),
'configured': False
}

try:
provider_app = provider.get_app(request)
provider_data['display_name'] = provider_app.name
provider_data['configured'] = True
except SocialApp.DoesNotExist:
provider_data['display_name'] = provider.name
provider_data['login'] = request.build_absolute_uri(reverse(f'{provider.id}_api_login'))
except NoReverseMatch:
provider_data['login'] = None

try:
provider_data['connect'] = request.build_absolute_uri(reverse(f'{provider.id}_api_connect'))
except NoReverseMatch:
provider_data['connect'] = None

provider_data['configured'] = InvenTree.sso.check_provider(provider)
provider_data['display_name'] = InvenTree.sso.provider_display_name(provider)

provider_list.append(provider_data)

data = {
'sso_enabled': InvenTreeSetting.get_setting('LOGIN_ENABLE_SSO'),
'sso_registration': InvenTreeSetting.get_setting('LOGIN_ENABLE_SSO_REG'),
'sso_enabled': InvenTree.sso.login_enabled(),
'sso_registration': InvenTree.sso.registration_enabled(),
'mfa_required': InvenTreeSetting.get_setting('LOGIN_ENFORCE_MFA'),
'providers': provider_list
}
Expand Down
81 changes: 81 additions & 0 deletions src/backend/InvenTree/InvenTree/sso.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""Helper functions for Single Sign On functionality"""


import logging

from common.models import InvenTreeSetting
from InvenTree.helpers import str2bool

logger = logging.getLogger('inventree')


def get_provider_app(provider):
"""Return the SocialApp object for the given provider"""

from allauth.socialaccount.models import SocialApp

try:
apps = SocialApp.objects.filter(provider__iexact=provider.id)
except SocialApp.DoesNotExist:
logger.warning("SSO SocialApp not found for provider '%s'", provider.id)
return None

if apps.count() > 1:
logger.warning("Multiple SocialApps found for provider '%s'", provider.id)

if apps.count() == 0:
logger.warning("SSO SocialApp not found for provider '%s'", provider.id)

return apps.first()


def check_provider(provider, raise_error=False):
"""Check if the given provider is correctly configured.

To be correctly configured, the following must be true:

- Provider must either have a registered SocialApp
- Must have at least one site enabled
"""

import allauth.app_settings

# First, check that the provider is enabled
app = get_provider_app(provider)

if not app:
return False

if allauth.app_settings.SITES_ENABLED:
# At least one matching site must be specified
if not app.sites.exists():
logger.error("SocialApp %s has no sites configured", app)
return False

# At this point, we assume that the provider is correctly configured
return True


def provider_display_name(provider):
"""Return the 'display name' for the given provider"""

if app := get_provider_app(provider):
return app.name

# Fallback value if app not found
return provider.name


def login_enabled() -> bool:
"""Return True if SSO login is enabled"""
return str2bool(InvenTreeSetting.get_setting('LOGIN_ENABLE_SSO'))


def registration_enabled() -> bool:
"""Return True if SSO registration is enabled"""
return str2bool(InvenTreeSetting.get_setting('LOGIN_ENABLE_SSO_REG'))


def auto_registration_enabled() -> bool:
"""Return True if SSO auto-registration is enabled"""
return str2bool(InvenTreeSetting.get_setting('LOGIN_SIGNUP_SSO_AUTO'))
8 changes: 4 additions & 4 deletions src/backend/InvenTree/config_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ remote_login_header: HTTP_REMOTE_USER
# social_backends:
# - 'allauth.socialaccount.providers.google'
# - 'allauth.socialaccount.providers.github'
# - 'allauth.socialaccount.providers.keycloak'

# Add specific settings for social account providers (if required)
# Refer to the djngo-allauth documentation for more details:
# https://docs.allauth.org/en/latest/socialaccount/provider_configuration.html
# social_providers:
# keycloak:
# KEYCLOAK_URL: 'https://keycloak.custom/auth'
# KEYCLOAK_REALM: 'master'
# github:
# VERIFIED_EMAIL: true

# Add LDAP support
# ldap:
Expand Down
Loading
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.