Skip to main content

A simple OAuth client library for Google, Kakao, Naver, and GitHub.

Project description

kpick-oauth

A small Python OAuth helper library for Kakao, Naver, Google, and GitHub.

Installation

pip install kpick-oauth

Usage

from kpick_oauth import (
    OAuthClient,
    OAuthTokenError,
    OAuthUserInfoError,
    generate_state,
    verify_state,
)

client = OAuthClient(
    provider="github",
    client_id="your-client-id",
    client_secret="your-client-secret",
)

# Create a state value before redirecting the user.
# Bind it to the user's session/cookie so the callback can recover it.
state = generate_state()

# state is required. get_authorize_url() raises ValueError if it is empty.
authorize_url = client.get_authorize_url(
    redirect_uri="https://example.com/oauth/callback",
    state=state,
    scope="read:user user:email",
)

# Redirect the user's browser to authorize_url.

# In your callback route, compare the stored state with the returned state.
if not verify_state(stored_state, returned_state):
    raise ValueError("Invalid OAuth state")

try:
    token = client.fetch_token(
        code="authorization-code",
        redirect_uri="https://example.com/oauth/callback",
    )
    user = client.fetch_user(token["access_token"])
except OAuthTokenError:
    # Provider returned an error payload (e.g. invalid_grant) or no access_token.
    raise
except OAuthUserInfoError:
    # Provider returned an error payload from the userinfo endpoint.
    raise

# user is an OAuthUser, not your application's main user account.
# Use (user.provider, user.provider_id) as the OAuth account key.
# Only trust user.email when user.email_verified is True.
app_user = link_or_create_account_by_provider_id(user.provider, user.provider_id)

if user.email_verified is True and user.email:
    app_user.email = user.email

Security Notes

  • Keep client_secret on your server. Do not put it in browser JavaScript.
  • Do not put access tokens in URLs.
  • Do not print or log tokens.
  • Use generate_state() before redirecting to the provider.
  • Use verify_state() in your callback before exchanging the code for a token.
  • get_authorize_url() requires a non-empty state.
  • Store provider login records by provider and provider_id.
  • Do not use email as the OAuth account key.
  • Kakao and Google emails are returned only when the provider marks them verified.
  • Naver and GitHub emails are marked verified when returned by the provider.
  • GitHub noreply emails are treated as unavailable and returned as None.
  • Check user.email_verified before trusting an email for account recovery or linking.
  • Do not log OAuthUser.raw; it can contain personal information.
  • Token and userinfo provider error payloads raise OAuthTokenError / OAuthUserInfoError. This includes Kakao's negative code and Naver's non-"00" resultcode.

Supported Providers

  • Kakao
  • Naver
  • Google
  • GitHub

Development

pip install -e ".[dev]"
pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kpick_oauth-0.2.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kpick_oauth-0.2.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file kpick_oauth-0.2.0.tar.gz.

File metadata

  • Download URL: kpick_oauth-0.2.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for kpick_oauth-0.2.0.tar.gz
Algorithm Hash digest
SHA256 84947275b1815159c4a6ca891d03fcc029679c6e22415781bc00537152e9e4fa
MD5 7786d553705e8467321a0c13a54673c2
BLAKE2b-256 9620bf2d4028f1a10ce4dc78b5cfb681302c857c00d37ce430d29f0109ec5b25

See more details on using hashes here.

File details

Details for the file kpick_oauth-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: kpick_oauth-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for kpick_oauth-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70b3bed83555ddbe7f1863caab9f18316afdd99a6e0e885505802303332bf283
MD5 d54081340c73666ef2720e238ebe6b98
BLAKE2b-256 e5a7061abf3efc5d8e940e64b4efc3426d445f0b1e1cb54a3362cb8a1bc21b64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page