-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4aa833b
commit 1463f92
Showing
9 changed files
with
110 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.1.0" | ||
__version__ = "0.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from functools import wraps | ||
from promise import Promise, is_thenable | ||
|
||
from strawberry.types import Info | ||
from social_django.utils import load_backend, load_strategy | ||
from social_core.exceptions import MissingBackend | ||
from gqlauth.core.constants import Messages as GQLMessages | ||
|
||
from .types import SocialAuthInput | ||
from .constants import Messages | ||
|
||
|
||
def psa(f): | ||
@wraps(f) | ||
def wrapper(cls, info: Info, input_: SocialAuthInput, **kwargs): | ||
strategy = load_strategy() | ||
try: | ||
backend = load_backend(strategy, input_.provider, redirect_uri=None) | ||
|
||
user = backend.do_auth(input_.access_token) | ||
|
||
if user is None: | ||
return f(cls, info, input_, None, GQLMessages.INVALID_TOKEN, **kwargs) | ||
|
||
user_model = strategy.storage.user.user_model() | ||
|
||
if not isinstance(user, user_model): | ||
raise f(cls, info, input_, None, Messages.user_instance_error(user), **kwargs) | ||
|
||
return f(cls, info, input_, user, None, **kwargs) | ||
except MissingBackend: | ||
return f(cls, info, input_, None, Messages.NO_PROVIDER, **kwargs) | ||
except Exception as e: | ||
return f(cls, info, input_, None, Messages.exception(e), **kwargs) | ||
return wrapper | ||
|
||
|
||
def social_auth(f): | ||
""" | ||
Decorator for Getting social User. Use this decorator if you want to customize the SocialAuthMixin. | ||
:param f: Input: SocialAuthInput(provider, accessToken) | ||
:return: function with two additional arguments | ||
user: Entire User Object (Get your social data using user.social_user) | ||
errors: Any error occurred in the process of getting the Social User | ||
""" | ||
@psa | ||
@wraps(f) | ||
def wrapper(cls, info, _input, user, errors, **kwargs): | ||
def on_resolve(payload): | ||
payload.user = user | ||
payload.errors = errors | ||
return payload | ||
|
||
result = f(cls, info, _input, user, errors, **kwargs) | ||
|
||
if is_thenable(result): | ||
return Promise.resolve(result).then(on_resolve) | ||
return on_resolve(result) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,22 @@ | ||
from strawberry.types import Info | ||
from gqlauth.user.resolvers import BaseMixin | ||
from gqlauth.core.constants import Messages as GQLMessages | ||
from .decorators import social_auth | ||
from .types import SocialAuthInput | ||
from social_django.utils import load_backend, load_strategy | ||
from social_core.exceptions import MissingBackend | ||
|
||
from .types import SocialType | ||
from .constants import Messages | ||
|
||
|
||
class SocialAuthMixin(BaseMixin): | ||
"""Social Auth takes OAuth Provider and OAuth Access Token | ||
Allow user to perform social auth for the given OAuth provider and OAuth Access token | ||
:returns | ||
user: Entire User Object (Get your social data using user.social_user) | ||
errors: Any error occurred in the process of getting the Social User | ||
""" | ||
@classmethod | ||
def resolve_mutation(cls, info: Info, input_: SocialAuthInput) -> SocialType: | ||
strategy = load_strategy() | ||
try: | ||
backend = load_backend(strategy, input_.provider, redirect_uri=None) | ||
|
||
user = backend.do_auth(input_.access_token) | ||
|
||
if user is None: | ||
return SocialType(success=False, errors=GQLMessages.INVALID_TOKEN) | ||
|
||
user_model = strategy.storage.user.user_model() | ||
|
||
if not isinstance(user, user_model): | ||
return SocialType(success=False, errors=Messages.user_instance_error(user)) | ||
|
||
return SocialType.from_social_user(user) | ||
|
||
except MissingBackend: | ||
return SocialType(success=False, errors=Messages.NO_PROVIDER) | ||
except Exception as e: | ||
return SocialType(success=False, errors=Messages.exception(e)) | ||
@classmethod | ||
@social_auth | ||
def resolve_mutation(cls, info: Info, input_: SocialAuthInput, user, errors) -> SocialType: | ||
if errors is not None: | ||
return SocialType(success=False, errors=errors) | ||
return SocialType.from_social_user(user) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters