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

[Question] How to use the convert token logic inside Django? #212

Open
digoburigo opened this issue Mar 19, 2024 · 6 comments
Open

[Question] How to use the convert token logic inside Django? #212

digoburigo opened this issue Mar 19, 2024 · 6 comments

Comments

@digoburigo
Copy link

I want to convert the token from OAuth to the Django session one, but I need to do it inside Django. How can this be achieved?

Tried to use requests and making a POST call internally but it's not working.

@wagnerdelima
Copy link
Owner

I could not understand your question. Be clearer, more concise and give examples.

@digoburigo
Copy link
Author

digoburigo commented Mar 19, 2024

I'm trying to convert the token from the Google OAuth callback, getting the token from the Django session and returning it to another application. But when I deploy the server, it's not being able to request to itself. Then I'm asking if it's possible to call this lib view internally or something like that

def complete_google(request: HttpRequest) -> Response:
    social: str = "google-oauth2"
    code: str = request.GET.get("code")
    url: str = "https://oauth2.googleapis.com/token"
    payload: Dict[str, str] = {
        "client_id": SOCIAL_AUTH_GOOGLE_OAUTH2_KEY,
        "client_secret": SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET,
        "code": code,
        "redirect_uri": f"{DJANGO_BASE_URL}/complete/google-oauth2/",
        "grant_type": "authorization_code",
    }
    headers: Dict[str, str] = {"Content-Type": "application/x-www-form-urlencoded"}
    response: Response = requests.post(url, headers=headers, data=payload)

    response_json: Dict[str, str] = response.json()
    google_auth_token = response_json.get("access_token")
    url: str = f"{DJANGO_BASE_URL}/auth/convert-token/"
    payload: Dict[str, str] = {
        "grant_type": "convert_token",
        "backend": social,
        "client_id": APPLICATION_CLIENT_ID_GOOGLE,
        "client_secret": APPLICATION_CLIENT_SECRET_GOOGLE,
        "token": google_auth_token,
    }
    headers: Dict[str, str] = {"Content-Type": "application/x-www-form-urlencoded"}
    response: Response = requests.post(url, headers=headers, data=payload)
    return Response(
        response.json(),
     )

@wagnerdelima
Copy link
Owner

What is your django base url?

You could do it with self.request.build_absolute_uri('/')[:-1]

Or something like that.

Finishing the day here. Hope you find it out!

@digoburigo
Copy link
Author

digoburigo commented Mar 19, 2024

That's only the URL that Django application it's running.

For example, if it's running locally with NGINX is http://localhost:80

Thanks for the info. I will try out. Good day!

@wagnerdelima
Copy link
Owner

Succeeded?

@digoburigo
Copy link
Author

No, I'm moving this to a different application, that's going to be in charge of validating the users. So I guess I will not encounter this problem. But if is there a way to make it to call itself would be ideal. Is there any way to call the view responsible for converting the token ?

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

2 participants