Skip to content

Commit

Permalink
chore: ensure otterdog can run in local mode without authentication e…
Browse files Browse the repository at this point in the history
…nabled
  • Loading branch information
Thomas Neidhart committed Feb 27, 2025
1 parent 769a287 commit 004883f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
35 changes: 27 additions & 8 deletions otterdog/webapp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# *******************************************************************************
# Copyright (c) 2023-2024 Eclipse Foundation and others.
# Copyright (c) 2023-2025 Eclipse Foundation and others.
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-v20.html
Expand All @@ -13,7 +13,7 @@
from datetime import datetime
from importlib import import_module
from importlib.util import find_spec
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import quart_flask_patch # type: ignore # noqa: F401
from flask_github import GitHub # type: ignore
Expand All @@ -35,20 +35,34 @@

mongo = Mongo()
redis_handler = RedisHandler()
auth_manager = QuartAuth(cookie_secure=False) # type: ignore
oauth_github = GitHub()
auth_manager: QuartAuth | None = None
oauth_github: GitHub | None = None


def register_extensions(app):
mongo.init_app(app)
redis_handler.init_app(app)

from otterdog.webapp.auth import User
if app.config["GITHUB_CLIENT_ID"] is not None:
from otterdog.webapp.auth import User

auth_manager.user_class = User
auth_manager.init_app(app)
global auth_manager
global oauth_github

oauth_github.init_app(app)
auth_manager = QuartAuth(cookie_secure=False) # type: ignore
oauth_github = GitHub()

auth_manager.user_class = User
auth_manager.init_app(app)

oauth_github.init_app(app)
else:

@app.context_processor
async def dummy_user() -> dict[str, Any]:
return {
"current_user": None,
}


def register_github_webhook(app) -> None:
Expand All @@ -60,7 +74,12 @@ def register_github_webhook(app) -> None:


def register_blueprints(app):
global auth_manager

for module_name in _BLUEPRINT_MODULES:
if module_name == "auth" and auth_manager is None:
continue

routes_fqn = f"otterdog.webapp.{module_name}.routes"
spec = find_spec(routes_fqn)
if spec is not None:
Expand Down
4 changes: 2 additions & 2 deletions otterdog/webapp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class AppConfig:
GITHUB_WEBHOOK_SYNC_CONTEXT = config("GITHUB_WEBHOOK_SYNC_CONTEXT", default="otterdog-sync")

# GitHub OAuth config
GITHUB_CLIENT_ID = config("GITHUB_OAUTH_CLIENT_ID")
GITHUB_CLIENT_SECRET = config("GITHUB_OAUTH_CLIENT_SECRET")
GITHUB_CLIENT_ID = config("GITHUB_OAUTH_CLIENT_ID", default=None)
GITHUB_CLIENT_SECRET = config("GITHUB_OAUTH_CLIENT_SECRET", default=None)

# GitHub App config
GITHUB_APP_ID = config("GITHUB_APP_ID")
Expand Down
18 changes: 11 additions & 7 deletions otterdog/webapp/home/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# *******************************************************************************
# Copyright (c) 2024 Eclipse Foundation and others.
# Copyright (c) 2024-2025 Eclipse Foundation and others.
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-v20.html
Expand All @@ -17,11 +17,15 @@
@blueprint.after_request
async def after_request_func(response: Response):
if is_cache_control_enabled() and response.status_code == 200:
if await current_user.is_authenticated:
response.cache_control.no_cache = True
else:
response.cache_control.max_age = 60
response.cache_control.public = True
response.vary = "Cookie"
try:
if await current_user.is_authenticated:
response.cache_control.no_cache = True
return response
except KeyError:
pass

response.cache_control.max_age = 60
response.cache_control.public = True
response.vary = "Cookie"

return response
2 changes: 1 addition & 1 deletion otterdog/webapp/templates/includes/navigation.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{{ current_user.username }} - Logout
</a>
</div>
{% else %}
{% elif current_user %}
<a href="{{ url_for('auth_blueprint.login_view') }}" class="dropdown-item">
<i class="fas fa-user mr-2"></i> Login
</a>
Expand Down

0 comments on commit 004883f

Please sign in to comment.