* Feature/7137/gitea sso (#7940) * added gitea auth to admin panel with configs , added api calls * added gitea to oauth root (for signup and signin) * removed log * replace github oauth with gitea ouath error messages * added gitea to auth root * fix: update token expiration handling and remove unused variable in Gitea callback * fix: include Gitea in OAuth enabled checks * fix: improve error handling when fetching emails from Gitea * chore : remove logs and add semicolons * refactor: update Gitea authentication components and imports for consistency * fix: enhance Gitea authentication form to auto-populate host value and improve OAuth checks * refactor: enhance Gitea OAuth provider with improved error handling and URL validation * fix: update authentication success messages to check for string value "1" --------- Co-authored-by: Shivam Jain <shivam.clgstash@gmail.com> Co-authored-by: Prateek Shourya <prateekshourya29@gmail.com>
100 lines
4.1 KiB
Python
100 lines
4.1 KiB
Python
# Python imports
|
|
import uuid
|
|
from urllib.parse import urlencode
|
|
|
|
# Django import
|
|
from django.http import HttpResponseRedirect
|
|
from django.views import View
|
|
|
|
# Module imports
|
|
from plane.authentication.provider.oauth.gitea import GiteaOAuthProvider
|
|
from plane.authentication.utils.login import user_login
|
|
from plane.license.models import Instance
|
|
from plane.authentication.utils.host import base_host
|
|
from plane.authentication.adapter.error import (
|
|
AUTHENTICATION_ERROR_CODES,
|
|
AuthenticationException,
|
|
)
|
|
from plane.utils.path_validator import validate_next_path
|
|
|
|
|
|
class GiteaOauthInitiateSpaceEndpoint(View):
|
|
def get(self, request):
|
|
# Get host and next path
|
|
request.session["host"] = base_host(request=request, is_space=True)
|
|
next_path = request.GET.get("next_path")
|
|
if next_path:
|
|
request.session["next_path"] = str(validate_next_path(next_path))
|
|
|
|
# Check instance configuration
|
|
instance = Instance.objects.first()
|
|
if instance is None or not instance.is_setup_done:
|
|
exc = AuthenticationException(
|
|
error_code=AUTHENTICATION_ERROR_CODES["INSTANCE_NOT_CONFIGURED"],
|
|
error_message="INSTANCE_NOT_CONFIGURED",
|
|
)
|
|
params = exc.get_error_dict()
|
|
if next_path:
|
|
params["next_path"] = str(validate_next_path(next_path))
|
|
url = f"{base_host(request=request, is_space=True)}?{urlencode(params)}"
|
|
return HttpResponseRedirect(url)
|
|
|
|
try:
|
|
state = uuid.uuid4().hex
|
|
provider = GiteaOAuthProvider(request=request, state=state)
|
|
request.session["state"] = state
|
|
auth_url = provider.get_auth_url()
|
|
return HttpResponseRedirect(auth_url)
|
|
except AuthenticationException as e:
|
|
params = e.get_error_dict()
|
|
if next_path:
|
|
params["next_path"] = str(next_path)
|
|
url = f"{base_host(request=request, is_space=True)}?{urlencode(params)}"
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
class GiteaCallbackSpaceEndpoint(View):
|
|
def get(self, request):
|
|
code = request.GET.get("code")
|
|
state = request.GET.get("state")
|
|
next_path = request.session.get("next_path")
|
|
|
|
if state != request.session.get("state", ""):
|
|
exc = AuthenticationException(
|
|
error_code=AUTHENTICATION_ERROR_CODES["GITEA_OAUTH_PROVIDER_ERROR"],
|
|
error_message="GITEA_OAUTH_PROVIDER_ERROR",
|
|
)
|
|
params = exc.get_error_dict()
|
|
if next_path:
|
|
params["next_path"] = str(validate_next_path(next_path))
|
|
url = f"{base_host(request=request, is_space=True)}?{urlencode(params)}"
|
|
return HttpResponseRedirect(url)
|
|
|
|
if not code:
|
|
exc = AuthenticationException(
|
|
error_code=AUTHENTICATION_ERROR_CODES["GITEA_OAUTH_PROVIDER_ERROR"],
|
|
error_message="GITEA_OAUTH_PROVIDER_ERROR",
|
|
)
|
|
params = exc.get_error_dict()
|
|
if next_path:
|
|
params["next_path"] = str(validate_next_path(next_path))
|
|
url = f"{base_host(request=request, is_space=True)}?{urlencode(params)}"
|
|
return HttpResponseRedirect(url)
|
|
|
|
try:
|
|
provider = GiteaOAuthProvider(request=request, code=code)
|
|
user = provider.authenticate()
|
|
# Login the user and record his device info
|
|
user_login(request=request, user=user, is_space=True)
|
|
# Process workspace and project invitations
|
|
# redirect to referer path
|
|
url = (
|
|
f"{base_host(request=request, is_space=True)}{str(validate_next_path(next_path)) if next_path else ''}"
|
|
)
|
|
return HttpResponseRedirect(url)
|
|
except AuthenticationException as e:
|
|
params = e.get_error_dict()
|
|
if next_path:
|
|
params["next_path"] = str(validate_next_path(next_path))
|
|
url = f"{base_host(request=request, is_space=True)}?{urlencode(params)}"
|
|
return HttpResponseRedirect(url)
|