supertokens-python icon indicating copy to clipboard operation
supertokens-python copied to clipboard

Github thirdparty provider does not set raw_user_info_from_provider in get_user_info

Open brentnortham opened this issue 1 year ago • 1 comments

Unlike other providers, the GitHub provider requests user data from the GitHub api and returns the user_id and email but never sets or provides the raw_user_info_from_provider, meaning we have to unnecessarily implement that as a second call in a thirdparty_sign_in_up_post.

brentnortham avatar Feb 02 '24 16:02 brentnortham

For the time being I've done this:

def override_github_user_info(oi):

    async def get_user_info(
        oauth_tokens: dict[str, Any],
        user_context: dict[str, Any]
    ) -> UserInfo:
        headers = {
            "Authorization": f"Bearer {oauth_tokens.get('access_token')}",
            "Accept": "application/vnd.github.v3+json",
        }

        email_info: list[Any] = await do_get_request("https://api.github.com/user/emails", headers=headers)  # type: ignore
        user_info = await do_get_request("https://api.github.com/user", headers=headers)

        raw_user_info_from_provider = RawUserInfoFromProvider({}, {})
        raw_user_info_from_provider.from_user_info_api = user_info
        raw_user_info_from_provider.from_id_token_payload["email"] = email_info

        email = None
        is_verified = False
        for info in email_info:
            if info["primary"]:
                email = info["email"]
                is_verified = info["verified"]

        return UserInfo(
            third_party_user_id=str(user_info.get("id")),
            email = None if email is None else UserInfoEmail(email, is_verified),
            raw_user_info_from_provider=raw_user_info_from_provider,
        )

    oi.get_user_info = get_user_info

    return oi

...
                ProviderInput(
                    config=ProviderConfig(
                        third_party_id="github",
                        clients=[
                            ProviderClientConfig(
                                client_id="xxx",
                                client_secret="xxx",
                            )
                        ],
                    ),
                    override=override_github_git_user_info
                ),

brentnortham avatar Feb 03 '24 17:02 brentnortham