Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process of authentication #208

Open
Ushakek opened this issue Feb 15, 2024 · 0 comments
Open

Process of authentication #208

Ushakek opened this issue Feb 15, 2024 · 0 comments

Comments

@Ushakek
Copy link

Ushakek commented Feb 15, 2024

Hi! Thanks for your work!
But I need help of community.

In my Django project with django-rest-framework, we use JWT authentication and want to implement the social login by google. So, I installed and configured drf-social-oauth2 with backend google-oath2 in the project as shown in docs.

settings.py

INSTALLED_APPS = [
    ...
    "social_django",
    "oauth2_provider",
    "drf_social_oauth2",
    ...
]

...

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
                ...
            ],
        },
    }
]

REST_FRAMEWORK = {
    ...
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        "oauth2_provider.contrib.rest_framework.OAuth2Authentication",
        "drf_social_oauth2.authentication.SocialAuthentication",
    ),
}

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "drf_social_oauth2.backends.DjangoOAuth2",
    "social_core.backends.google.GoogleOAuth2",
)

SOCIAL_AUTH_REQUIRE_POST = True
ACTIVATE_JWT = True

SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/userinfo.profile",
]


SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = env(
    "SOCIAL_AUTH_GOOGLE_OAUTH2_KEY", default="SOCIAL_AUTH_GOOGLE_OAUTH2_KEY"
)
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = env(
    "SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET", default="SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET"
)

urls.py

urlpatterns = [
    ...
    path("", include("social_django.urls", namespace="social")),
    path("auth/", include("drf_social_oauth2.urls", namespace="drf")),
]

And now the question itself.
How to implement redirection with the necessary data after user authorization on an external resource?

I have a user's entry point with a redirect to google authorization

    path(
        "sign_in/google",
        views.AuthViewSet.as_view({"post": "sign_in_google"}),
        name="sign_in_google",
    ),

method

    @extend_schema(request=None, responses={302: None})
    def sign_in_google(self, request, *args, **kwargs):
        strategy = load_strategy(request)
        backend = load_backend(
            strategy=strategy,
            name="google-oauth2",
            redirect_uri=f"{self.request.scheme}://{self.request.get_host()}/complete/google-oauth2/",
        )
        return do_auth(backend, REDIRECT_FIELD_NAME)

And if I follow as per our requirements we need the user to redirect to google-oauth2 page and login with credentials and also our app to access data and create user. This achieved by
http://localhost:8000/sign_in/google API it redirect to http://localhost:8000/complete/google-oauth2/ with necessary parameters and social user with django user created and redirect to LOGIN_REDIRECT_URL with session authentication.
This part is working and user is created successfully.

Redirection is working fine. Reverse redirection with user creation works fine too. But how do I send data to the api in the documentation to get an authorization token on the server? (We do not have session authorization).

Please need your help to understand the process, I read a lot and watched tutorials but didn't get as expected.

If you need any detail please ask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant