Skip to content

Commit

Permalink
refactor: only load oidc module if OIDC is configured
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Sep 15, 2023
1 parent 7e3cb6b commit 46c795b
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 277 deletions.
15 changes: 11 additions & 4 deletions canaille/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ def setup_jinja(app):

def setup_blueprints(app):
import canaille.core.blueprints
import canaille.oidc.blueprints

app.url_map.strict_slashes = False

app.register_blueprint(canaille.core.blueprints.bp)
app.register_blueprint(canaille.oidc.blueprints.bp)

if "OIDC" in app.config:
import canaille.oidc.blueprints

app.register_blueprint(canaille.oidc.blueprints.bp)


def setup_flask(app):
Expand All @@ -88,6 +91,7 @@ def global_processor():
return {
"debug": app.debug or app.config.get("TESTING", False),
"has_smtp": "SMTP" in app.config,
"has_oidc": "OIDC" in app.config,
"has_password_recovery": app.config.get("ENABLE_PASSWORD_RECOVERY", True),
"has_registration": app.config.get("ENABLE_REGISTRATION", False),
"has_account_lockability": app.backend.get().has_account_lockability(),
Expand Down Expand Up @@ -135,7 +139,6 @@ def setup_flask_converters(app):


def create_app(config=None, validate=True, backend=None):
from .oidc.oauth import setup_oauth
from .app.i18n import setup_i18n
from .app.configuration import setup_config
from .app.themes import setup_themer
Expand All @@ -149,14 +152,18 @@ def create_app(config=None, validate=True, backend=None):
try:
setup_logging(app)
setup_backend(app, backend)
setup_oauth(app)
setup_flask_converters(app)
setup_blueprints(app)
setup_jinja(app)
setup_i18n(app)
setup_themer(app)
setup_flask(app)

if "OIDC" in app.config:
from .oidc.oauth import setup_oauth

setup_oauth(app)

except Exception as exc: # pragma: no cover
if sentry_sdk:
sentry_sdk.capture_exception(exc)
Expand Down
2 changes: 1 addition & 1 deletion canaille/core/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def index():
if user.can_edit_self or user.can_manage_users:
return redirect(url_for("core.account.profile_edition", edited_user=user))

if user.can_use_oidc:
if "OIDC" in current_app.config and user.can_use_oidc:
return redirect(url_for("oidc.consents.consents"))

return redirect(url_for("core.account.about"))
Expand Down
34 changes: 18 additions & 16 deletions canaille/core/templates/mails/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@
<i class="user mail icon"></i>
{% trans %}Emails{% endtrans %}
</a>
<a class="item" href="{{ url_for('oidc.clients.index') }}">
<i class="th list icon"></i>
{% trans %}Clients{% endtrans %}
</a>
<a class="item" href="{{ url_for('oidc.clients.add') }}">
<i class="plus icon"></i>
{% trans %}Add a client{% endtrans %}
</a>
<a class="item" href="{{ url_for('oidc.tokens.index') }}">
<i class="key icon"></i>
{% trans %}Tokens{% endtrans %}
</a>
<a class="item" href="{{ url_for('oidc.authorizations.index') }}">
<i class="user secret icon"></i>
{% trans %}Codes{% endtrans %}
</a>
{% if has_oidc %}
<a class="item" href="{{ url_for('oidc.clients.index') }}">
<i class="th list icon"></i>
{% trans %}Clients{% endtrans %}
</a>
<a class="item" href="{{ url_for('oidc.clients.add') }}">
<i class="plus icon"></i>
{% trans %}Add a client{% endtrans %}
</a>
<a class="item" href="{{ url_for('oidc.tokens.index') }}">
<i class="key icon"></i>
{% trans %}Tokens{% endtrans %}
</a>
<a class="item" href="{{ url_for('oidc.authorizations.index') }}">
<i class="user secret icon"></i>
{% trans %}Codes{% endtrans %}
</a>
{% endif %}
{% endblock %}

{% block content %}
Expand Down
2 changes: 1 addition & 1 deletion canaille/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{% trans %}Profile{% endtrans %}
</a>
{% endif %}
{% if user.can_use_oidc %}
{% if has_oidc and user.can_use_oidc %}
<a class="item {% if menuitem is defined and menuitem == "consents" %}active{% endif %}"
href="{{ url_for('oidc.consents.consents') }}">
<i class="handshake icon"></i>
Expand Down
14 changes: 8 additions & 6 deletions tests/app/fixtures/themes/test/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
<i class="id card icon"></i>
{% trans %}My profile{% endtrans %}
</a>
<a class="item {% if menuitem == "consents" %}active{% endif %}"
href="{{ url_for('consents.consents') }}">
<i class="handshake icon"></i>
{% trans %}My consents{% endtrans %}
</a>
{% if has_oidc and user.can_use_oidc %}
<a class="item {% if menuitem == "consents" %}active{% endif %}"
href="{{ url_for('consents.consents') }}">
<i class="handshake icon"></i>
{% trans %}My consents{% endtrans %}
</a>
{% endif %}
{% if user.can_manage_users %}
<a class="item {% if menuitem == "users" %}active{% endif %}"
href="{{ url_for('core.account.users') }}">
Expand All @@ -51,7 +53,7 @@
{% trans %}Groups{% endtrans %}
</a>
{% endif %}
{% if user.can_manage_oidc %}
{% if has_oidc and user.can_manage_oidc %}
<div class="ui dropdown item {% if menuitem == "admin" %}active{% endif %}">
<i class="settings icon"></i>
Admin
Expand Down
Loading

0 comments on commit 46c795b

Please sign in to comment.