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_secreton 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-emptystate.- Store provider login records by
providerandprovider_id. - Do not use
emailas 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_verifiedbefore 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 negativecodeand Naver's non-"00"resultcode.
PKCE (Optional, Recommended Where Supported)
PKCE (RFC 7636) adds defense-in-depth against authorization code interception.
This library accepts PKCE parameters as opt-in arguments. Providers whose
official documentation does not list PKCE are rejected with ValueError so
callers do not get a false sense of protection.
| Provider | PKCE support |
|---|---|
| yes | |
| GitHub | yes |
| Kakao | no (per Kakao Developers docs) |
| Naver | no (per Naver Developers docs) |
from kpick_oauth import OAuthClient, generate_pkce_pair, generate_state
client = OAuthClient("google", client_id, client_secret)
state = generate_state()
pkce = generate_pkce_pair()
# Store BOTH state and pkce.code_verifier in the user's session.
authorize_url = client.get_authorize_url(
redirect_uri="https://example.com/oauth/callback",
state=state,
scope="openid email profile",
code_challenge=pkce.code_challenge,
)
# ... in the callback, after verify_state(...) ...
token = client.fetch_token(
code=callback_code,
redirect_uri="https://example.com/oauth/callback",
code_verifier=pkce.code_verifier,
)
Passing code_challenge to a Kakao or Naver client, or code_verifier to their
fetch_token, raises ValueError. Omit PKCE for those providers.
Supported Providers
- Kakao
- Naver
- GitHub
Development
pip install -e ".[dev]"
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kpick_oauth-0.3.0.tar.gz.
File metadata
- Download URL: kpick_oauth-0.3.0.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a081b098ae43fae52b78d2c8ae7654cfd87e15e560915a04f3637773959620bc
|
|
| MD5 |
d31963fa68cd201e940ff2bd7dd85435
|
|
| BLAKE2b-256 |
9271950dfbfdb63267716a568776aa64011f9725918331cce5fa437531d3b426
|
File details
Details for the file kpick_oauth-0.3.0-py3-none-any.whl.
File metadata
- Download URL: kpick_oauth-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5418a8ebd03c481b068067442e124fb20edca686c17d8fcdd8e7d5383f88e12d
|
|
| MD5 |
7cdf40ac0fa09b9a144afbed20fa5c08
|
|
| BLAKE2b-256 |
5bbd88d96c5f8b4305114c8f6c72c219a6bbcf0d827c6a20986f7d1a994e18e4
|