dev: auth callback for runing user post authentication workflows (#4498)

This commit is contained in:
Nikhil 2024-05-17 15:27:34 +05:30 committed by GitHub
parent 61e83ed808
commit 8d860396bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 58 additions and 31 deletions

View File

@ -21,9 +21,10 @@ from .error import AuthenticationException, AUTHENTICATION_ERROR_CODES
class Adapter:
"""Common interface for all auth providers"""
def __init__(self, request, provider):
def __init__(self, request, provider, callback=None):
self.request = request
self.provider = provider
self.callback = callback
self.token_data = None
self.user_data = None
@ -48,7 +49,8 @@ class Adapter:
def complete_login_or_signup(self):
email = self.user_data.get("email")
user = User.objects.filter(email=email).first()
# Check if sign up case or login
is_signup = bool(user)
if not user:
# New user
(ENABLE_SIGNUP,) = get_configuration_value(
@ -115,6 +117,13 @@ class Adapter:
user.token_updated_at = timezone.now()
user.save()
if self.callback:
self.callback(
user,
is_signup,
self.request,
)
if self.token_data:
self.create_update_account(user=user)

View File

@ -4,8 +4,8 @@ from plane.authentication.adapter.base import Adapter
class CredentialAdapter(Adapter):
"""Common interface for all credential providers"""
def __init__(self, request, provider):
super().__init__(request, provider)
def __init__(self, request, provider, callback=None):
super().__init__(request, provider, callback)
self.request = request
self.provider = provider

View File

@ -23,8 +23,9 @@ class OauthAdapter(Adapter):
userinfo_url,
client_secret=None,
code=None,
callback=None,
):
super().__init__(request, provider)
super().__init__(request, provider, callback=callback)
self.client_id = client_id
self.scope = scope
self.redirect_uri = redirect_uri

View File

@ -21,6 +21,7 @@ class EmailProvider(CredentialAdapter):
key=None,
code=None,
is_signup=False,
callback=None,
):
super().__init__(request, self.provider)
self.key = key

View File

@ -24,6 +24,7 @@ class MagicCodeProvider(CredentialAdapter):
request,
key,
code=None,
callback=None,
):
(

View File

@ -22,7 +22,7 @@ class GitHubOAuthProvider(OauthAdapter):
provider = "github"
scope = "read:user user:email"
def __init__(self, request, code=None, state=None):
def __init__(self, request, code=None, state=None, callback=None):
GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET = get_configuration_value(
[
@ -67,6 +67,7 @@ class GitHubOAuthProvider(OauthAdapter):
self.userinfo_url,
client_secret,
code,
callback=callback,
)
def set_token_data(self):

View File

@ -20,7 +20,7 @@ class GoogleOAuthProvider(OauthAdapter):
scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
provider = "google"
def __init__(self, request, code=None, state=None):
def __init__(self, request, code=None, state=None, callback=None):
(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET) = get_configuration_value(
[
{
@ -66,6 +66,7 @@ class GoogleOAuthProvider(OauthAdapter):
self.userinfo_url,
client_secret,
code,
callback=callback,
)
def set_token_data(self):

View File

@ -0,0 +1,9 @@
from .workspace_project_join import process_workspace_project_invitations
def post_user_auth_workflow(
user,
is_signup,
request,
):
process_workspace_project_invitations(user=user)

View File

@ -13,8 +13,8 @@ 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.utils.redirection_path import get_redirection_path
from plane.authentication.utils.workspace_project_join import (
process_workspace_project_invitations,
from plane.authentication.utils.user_auth_workflow import (
post_user_auth_workflow,
)
from plane.db.models import User
from plane.authentication.adapter.error import (
@ -125,13 +125,15 @@ class SignInAuthEndpoint(View):
try:
provider = EmailProvider(
request=request, key=email, code=password, is_signup=False
request=request,
key=email,
code=password,
is_signup=False,
callback=post_user_auth_workflow,
)
user = provider.authenticate()
# Login the user and record his device info
user_login(request=request, user=user, is_app=True)
# Process workspace and project invitations
process_workspace_project_invitations(user=user)
# Get the redirection path
if next_path:
path = str(next_path)
@ -252,13 +254,15 @@ class SignUpAuthEndpoint(View):
try:
provider = EmailProvider(
request=request, key=email, code=password, is_signup=True
request=request,
key=email,
code=password,
is_signup=True,
callback=post_user_auth_workflow,
)
user = provider.authenticate()
# Login the user and record his device info
user_login(request=request, user=user, is_app=True)
# Process workspace and project invitations
process_workspace_project_invitations(user=user)
# Get the redirection path
if next_path:
path = next_path

View File

@ -9,8 +9,8 @@ from django.views import View
from plane.authentication.provider.oauth.github import GitHubOAuthProvider
from plane.authentication.utils.login import user_login
from plane.authentication.utils.redirection_path import get_redirection_path
from plane.authentication.utils.workspace_project_join import (
process_workspace_project_invitations,
from plane.authentication.utils.user_auth_workflow import (
post_user_auth_workflow,
)
from plane.license.models import Instance
from plane.authentication.utils.host import base_host
@ -107,12 +107,11 @@ class GitHubCallbackEndpoint(View):
provider = GitHubOAuthProvider(
request=request,
code=code,
callback=post_user_auth_workflow,
)
user = provider.authenticate()
# Login the user and record his device info
user_login(request=request, user=user, is_app=True)
# Process workspace and project invitations
process_workspace_project_invitations(user=user)
# Get the redirection path
if next_path:
path = next_path

View File

@ -11,8 +11,8 @@ from django.views import View
from plane.authentication.provider.oauth.google import GoogleOAuthProvider
from plane.authentication.utils.login import user_login
from plane.authentication.utils.redirection_path import get_redirection_path
from plane.authentication.utils.workspace_project_join import (
process_workspace_project_invitations,
from plane.authentication.utils.user_auth_workflow import (
post_user_auth_workflow,
)
from plane.license.models import Instance
from plane.authentication.utils.host import base_host
@ -105,12 +105,11 @@ class GoogleCallbackEndpoint(View):
provider = GoogleOAuthProvider(
request=request,
code=code,
callback=post_user_auth_workflow,
)
user = provider.authenticate()
# Login the user and record his device info
user_login(request=request, user=user, is_app=True)
# Process workspace and project invitations
process_workspace_project_invitations(user=user)
# Get the redirection path
path = get_redirection_path(user=user)
# redirect to referer path

View File

@ -18,8 +18,8 @@ from plane.authentication.provider.credentials.magic_code import (
)
from plane.authentication.utils.login import user_login
from plane.authentication.utils.redirection_path import get_redirection_path
from plane.authentication.utils.workspace_project_join import (
process_workspace_project_invitations,
from plane.authentication.utils.user_auth_workflow import (
post_user_auth_workflow,
)
from plane.bgtasks.magic_link_code_task import magic_link
from plane.license.models import Instance
@ -130,14 +130,15 @@ class MagicSignInEndpoint(View):
try:
provider = MagicCodeProvider(
request=request, key=f"magic_{email}", code=code
request=request,
key=f"magic_{email}",
code=code,
callback=post_user_auth_workflow,
)
user = provider.authenticate()
profile = Profile.objects.get(user=user)
# Login the user and record his device info
user_login(request=request, user=user, is_app=True)
# Process workspace and project invitations
process_workspace_project_invitations(user=user)
if user.is_password_autoset and profile.is_onboarded:
path = "accounts/set-password"
else:
@ -204,13 +205,14 @@ class MagicSignUpEndpoint(View):
try:
provider = MagicCodeProvider(
request=request, key=f"magic_{email}", code=code
request=request,
key=f"magic_{email}",
code=code,
callback=post_user_auth_workflow,
)
user = provider.authenticate()
# Login the user and record his device info
user_login(request=request, user=user, is_app=True)
# Process workspace and project invitations
process_workspace_project_invitations(user=user)
# Get the redirection path
if next_path:
path = str(next_path)