forked from github/plane
dev: auth callback for runing user post authentication workflows (#4498)
This commit is contained in:
parent
61e83ed808
commit
8d860396bd
@ -21,9 +21,10 @@ from .error import AuthenticationException, AUTHENTICATION_ERROR_CODES
|
|||||||
class Adapter:
|
class Adapter:
|
||||||
"""Common interface for all auth providers"""
|
"""Common interface for all auth providers"""
|
||||||
|
|
||||||
def __init__(self, request, provider):
|
def __init__(self, request, provider, callback=None):
|
||||||
self.request = request
|
self.request = request
|
||||||
self.provider = provider
|
self.provider = provider
|
||||||
|
self.callback = callback
|
||||||
self.token_data = None
|
self.token_data = None
|
||||||
self.user_data = None
|
self.user_data = None
|
||||||
|
|
||||||
@ -48,7 +49,8 @@ class Adapter:
|
|||||||
def complete_login_or_signup(self):
|
def complete_login_or_signup(self):
|
||||||
email = self.user_data.get("email")
|
email = self.user_data.get("email")
|
||||||
user = User.objects.filter(email=email).first()
|
user = User.objects.filter(email=email).first()
|
||||||
|
# Check if sign up case or login
|
||||||
|
is_signup = bool(user)
|
||||||
if not user:
|
if not user:
|
||||||
# New user
|
# New user
|
||||||
(ENABLE_SIGNUP,) = get_configuration_value(
|
(ENABLE_SIGNUP,) = get_configuration_value(
|
||||||
@ -115,6 +117,13 @@ class Adapter:
|
|||||||
user.token_updated_at = timezone.now()
|
user.token_updated_at = timezone.now()
|
||||||
user.save()
|
user.save()
|
||||||
|
|
||||||
|
if self.callback:
|
||||||
|
self.callback(
|
||||||
|
user,
|
||||||
|
is_signup,
|
||||||
|
self.request,
|
||||||
|
)
|
||||||
|
|
||||||
if self.token_data:
|
if self.token_data:
|
||||||
self.create_update_account(user=user)
|
self.create_update_account(user=user)
|
||||||
|
|
||||||
|
@ -4,8 +4,8 @@ from plane.authentication.adapter.base import Adapter
|
|||||||
class CredentialAdapter(Adapter):
|
class CredentialAdapter(Adapter):
|
||||||
"""Common interface for all credential providers"""
|
"""Common interface for all credential providers"""
|
||||||
|
|
||||||
def __init__(self, request, provider):
|
def __init__(self, request, provider, callback=None):
|
||||||
super().__init__(request, provider)
|
super().__init__(request, provider, callback)
|
||||||
self.request = request
|
self.request = request
|
||||||
self.provider = provider
|
self.provider = provider
|
||||||
|
|
||||||
|
@ -23,8 +23,9 @@ class OauthAdapter(Adapter):
|
|||||||
userinfo_url,
|
userinfo_url,
|
||||||
client_secret=None,
|
client_secret=None,
|
||||||
code=None,
|
code=None,
|
||||||
|
callback=None,
|
||||||
):
|
):
|
||||||
super().__init__(request, provider)
|
super().__init__(request, provider, callback=callback)
|
||||||
self.client_id = client_id
|
self.client_id = client_id
|
||||||
self.scope = scope
|
self.scope = scope
|
||||||
self.redirect_uri = redirect_uri
|
self.redirect_uri = redirect_uri
|
||||||
|
@ -21,6 +21,7 @@ class EmailProvider(CredentialAdapter):
|
|||||||
key=None,
|
key=None,
|
||||||
code=None,
|
code=None,
|
||||||
is_signup=False,
|
is_signup=False,
|
||||||
|
callback=None,
|
||||||
):
|
):
|
||||||
super().__init__(request, self.provider)
|
super().__init__(request, self.provider)
|
||||||
self.key = key
|
self.key = key
|
||||||
|
@ -24,6 +24,7 @@ class MagicCodeProvider(CredentialAdapter):
|
|||||||
request,
|
request,
|
||||||
key,
|
key,
|
||||||
code=None,
|
code=None,
|
||||||
|
callback=None,
|
||||||
):
|
):
|
||||||
|
|
||||||
(
|
(
|
||||||
|
@ -22,7 +22,7 @@ class GitHubOAuthProvider(OauthAdapter):
|
|||||||
provider = "github"
|
provider = "github"
|
||||||
scope = "read:user user:email"
|
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(
|
GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET = get_configuration_value(
|
||||||
[
|
[
|
||||||
@ -67,6 +67,7 @@ class GitHubOAuthProvider(OauthAdapter):
|
|||||||
self.userinfo_url,
|
self.userinfo_url,
|
||||||
client_secret,
|
client_secret,
|
||||||
code,
|
code,
|
||||||
|
callback=callback,
|
||||||
)
|
)
|
||||||
|
|
||||||
def set_token_data(self):
|
def set_token_data(self):
|
||||||
|
@ -20,7 +20,7 @@ class GoogleOAuthProvider(OauthAdapter):
|
|||||||
scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
|
scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
|
||||||
provider = "google"
|
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(
|
(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET) = get_configuration_value(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
@ -66,6 +66,7 @@ class GoogleOAuthProvider(OauthAdapter):
|
|||||||
self.userinfo_url,
|
self.userinfo_url,
|
||||||
client_secret,
|
client_secret,
|
||||||
code,
|
code,
|
||||||
|
callback=callback,
|
||||||
)
|
)
|
||||||
|
|
||||||
def set_token_data(self):
|
def set_token_data(self):
|
||||||
|
@ -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)
|
@ -13,8 +13,8 @@ from plane.authentication.utils.login import user_login
|
|||||||
from plane.license.models import Instance
|
from plane.license.models import Instance
|
||||||
from plane.authentication.utils.host import base_host
|
from plane.authentication.utils.host import base_host
|
||||||
from plane.authentication.utils.redirection_path import get_redirection_path
|
from plane.authentication.utils.redirection_path import get_redirection_path
|
||||||
from plane.authentication.utils.workspace_project_join import (
|
from plane.authentication.utils.user_auth_workflow import (
|
||||||
process_workspace_project_invitations,
|
post_user_auth_workflow,
|
||||||
)
|
)
|
||||||
from plane.db.models import User
|
from plane.db.models import User
|
||||||
from plane.authentication.adapter.error import (
|
from plane.authentication.adapter.error import (
|
||||||
@ -125,13 +125,15 @@ class SignInAuthEndpoint(View):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
provider = EmailProvider(
|
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()
|
user = provider.authenticate()
|
||||||
# Login the user and record his device info
|
# Login the user and record his device info
|
||||||
user_login(request=request, user=user, is_app=True)
|
user_login(request=request, user=user, is_app=True)
|
||||||
# Process workspace and project invitations
|
|
||||||
process_workspace_project_invitations(user=user)
|
|
||||||
# Get the redirection path
|
# Get the redirection path
|
||||||
if next_path:
|
if next_path:
|
||||||
path = str(next_path)
|
path = str(next_path)
|
||||||
@ -252,13 +254,15 @@ class SignUpAuthEndpoint(View):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
provider = EmailProvider(
|
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()
|
user = provider.authenticate()
|
||||||
# Login the user and record his device info
|
# Login the user and record his device info
|
||||||
user_login(request=request, user=user, is_app=True)
|
user_login(request=request, user=user, is_app=True)
|
||||||
# Process workspace and project invitations
|
|
||||||
process_workspace_project_invitations(user=user)
|
|
||||||
# Get the redirection path
|
# Get the redirection path
|
||||||
if next_path:
|
if next_path:
|
||||||
path = next_path
|
path = next_path
|
||||||
|
@ -9,8 +9,8 @@ from django.views import View
|
|||||||
from plane.authentication.provider.oauth.github import GitHubOAuthProvider
|
from plane.authentication.provider.oauth.github import GitHubOAuthProvider
|
||||||
from plane.authentication.utils.login import user_login
|
from plane.authentication.utils.login import user_login
|
||||||
from plane.authentication.utils.redirection_path import get_redirection_path
|
from plane.authentication.utils.redirection_path import get_redirection_path
|
||||||
from plane.authentication.utils.workspace_project_join import (
|
from plane.authentication.utils.user_auth_workflow import (
|
||||||
process_workspace_project_invitations,
|
post_user_auth_workflow,
|
||||||
)
|
)
|
||||||
from plane.license.models import Instance
|
from plane.license.models import Instance
|
||||||
from plane.authentication.utils.host import base_host
|
from plane.authentication.utils.host import base_host
|
||||||
@ -107,12 +107,11 @@ class GitHubCallbackEndpoint(View):
|
|||||||
provider = GitHubOAuthProvider(
|
provider = GitHubOAuthProvider(
|
||||||
request=request,
|
request=request,
|
||||||
code=code,
|
code=code,
|
||||||
|
callback=post_user_auth_workflow,
|
||||||
)
|
)
|
||||||
user = provider.authenticate()
|
user = provider.authenticate()
|
||||||
# Login the user and record his device info
|
# Login the user and record his device info
|
||||||
user_login(request=request, user=user, is_app=True)
|
user_login(request=request, user=user, is_app=True)
|
||||||
# Process workspace and project invitations
|
|
||||||
process_workspace_project_invitations(user=user)
|
|
||||||
# Get the redirection path
|
# Get the redirection path
|
||||||
if next_path:
|
if next_path:
|
||||||
path = next_path
|
path = next_path
|
||||||
|
@ -11,8 +11,8 @@ from django.views import View
|
|||||||
from plane.authentication.provider.oauth.google import GoogleOAuthProvider
|
from plane.authentication.provider.oauth.google import GoogleOAuthProvider
|
||||||
from plane.authentication.utils.login import user_login
|
from plane.authentication.utils.login import user_login
|
||||||
from plane.authentication.utils.redirection_path import get_redirection_path
|
from plane.authentication.utils.redirection_path import get_redirection_path
|
||||||
from plane.authentication.utils.workspace_project_join import (
|
from plane.authentication.utils.user_auth_workflow import (
|
||||||
process_workspace_project_invitations,
|
post_user_auth_workflow,
|
||||||
)
|
)
|
||||||
from plane.license.models import Instance
|
from plane.license.models import Instance
|
||||||
from plane.authentication.utils.host import base_host
|
from plane.authentication.utils.host import base_host
|
||||||
@ -105,12 +105,11 @@ class GoogleCallbackEndpoint(View):
|
|||||||
provider = GoogleOAuthProvider(
|
provider = GoogleOAuthProvider(
|
||||||
request=request,
|
request=request,
|
||||||
code=code,
|
code=code,
|
||||||
|
callback=post_user_auth_workflow,
|
||||||
)
|
)
|
||||||
user = provider.authenticate()
|
user = provider.authenticate()
|
||||||
# Login the user and record his device info
|
# Login the user and record his device info
|
||||||
user_login(request=request, user=user, is_app=True)
|
user_login(request=request, user=user, is_app=True)
|
||||||
# Process workspace and project invitations
|
|
||||||
process_workspace_project_invitations(user=user)
|
|
||||||
# Get the redirection path
|
# Get the redirection path
|
||||||
path = get_redirection_path(user=user)
|
path = get_redirection_path(user=user)
|
||||||
# redirect to referer path
|
# redirect to referer path
|
||||||
|
@ -18,8 +18,8 @@ from plane.authentication.provider.credentials.magic_code import (
|
|||||||
)
|
)
|
||||||
from plane.authentication.utils.login import user_login
|
from plane.authentication.utils.login import user_login
|
||||||
from plane.authentication.utils.redirection_path import get_redirection_path
|
from plane.authentication.utils.redirection_path import get_redirection_path
|
||||||
from plane.authentication.utils.workspace_project_join import (
|
from plane.authentication.utils.user_auth_workflow import (
|
||||||
process_workspace_project_invitations,
|
post_user_auth_workflow,
|
||||||
)
|
)
|
||||||
from plane.bgtasks.magic_link_code_task import magic_link
|
from plane.bgtasks.magic_link_code_task import magic_link
|
||||||
from plane.license.models import Instance
|
from plane.license.models import Instance
|
||||||
@ -130,14 +130,15 @@ class MagicSignInEndpoint(View):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
provider = MagicCodeProvider(
|
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()
|
user = provider.authenticate()
|
||||||
profile = Profile.objects.get(user=user)
|
profile = Profile.objects.get(user=user)
|
||||||
# Login the user and record his device info
|
# Login the user and record his device info
|
||||||
user_login(request=request, user=user, is_app=True)
|
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:
|
if user.is_password_autoset and profile.is_onboarded:
|
||||||
path = "accounts/set-password"
|
path = "accounts/set-password"
|
||||||
else:
|
else:
|
||||||
@ -204,13 +205,14 @@ class MagicSignUpEndpoint(View):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
provider = MagicCodeProvider(
|
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()
|
user = provider.authenticate()
|
||||||
# Login the user and record his device info
|
# Login the user and record his device info
|
||||||
user_login(request=request, user=user, is_app=True)
|
user_login(request=request, user=user, is_app=True)
|
||||||
# Process workspace and project invitations
|
|
||||||
process_workspace_project_invitations(user=user)
|
|
||||||
# Get the redirection path
|
# Get the redirection path
|
||||||
if next_path:
|
if next_path:
|
||||||
path = str(next_path)
|
path = str(next_path)
|
||||||
|
Loading…
Reference in New Issue
Block a user