Skip to main content

强大、灵活且易用的OAuth授权集成组件

Project description

SenWeaver-OAuth

💫 强大、灵活且易用的OAuth授权集成组件 - Python版。提供统一便捷的第三方登录解决方案,已集成40+主流平台,包括Github、Gitee、微博、钉钉、百度、QQ、微信、Google、Facebook、抖音、领英、微软、飞书、京东、支付宝等国内外知名平台的OAuth授权。

特性

  • 丰富的OAuth平台: 支持国内外数十家知名的第三方平台的OAuth登录
  • 自定义state: 支持自定义State和缓存方式,开发者可根据实际情况选择任意缓存插件
  • 自定义OAuth: 提供统一接口,支持接入任意OAuth网站,快速实现OAuth登录功能
  • 自定义Http接口: 提供统一的HTTP工具接口,开发者可以根据自己项目的实际情况选择相对应的HTTP工具
  • 自定义Scope: 支持自定义scope,以适配更多的业务场景,而不仅仅是为了登录
  • 多种缓存实现: 支持内存缓存和Redis缓存,满足单实例和分布式应用的需求
  • 代码规范·简单: 代码结构清晰、逻辑简单,遵循Python编码规范

快速开始

安装

pip install senweaver-oauth

使用示例

from senweaver_oauth import AuthRequest, AuthConfig
from senweaver_oauth.source import AuthGiteeSource

# 创建授权request
auth_config = AuthConfig(
    client_id="clientId",
    client_secret="clientSecret",
    redirect_uri="redirectUri"
)
auth_request = AuthRequest.build(AuthGiteeSource, auth_config)

# 生成授权页面
auth_url = auth_request.authorize("state")
print(auth_url)

# 授权登录
callback = {
    "code": "授权码",
    "state": "state"
}
auth_response = auth_request.login(callback)
print(auth_response)

或者使用Builder模式:

from senweaver_oauth import AuthRequestBuilder
from senweaver_oauth import AuthConfig

# 方式一:静态配置AuthConfig
auth_request = AuthRequestBuilder.builder() \
    .source("gitee") \
    .auth_config(AuthConfig(
        client_id="clientId",
        client_secret="clientSecret",
        redirect_uri="redirectUri"
    )) \
    .build()

# 生成授权页面
auth_url = auth_request.authorize("state")
print(auth_url)

# 方式二:动态获取并配置AuthConfig
def get_config(source):
    # 通过source动态获取AuthConfig
    # 此处可以灵活的从数据库或配置文件中获取配置
    return AuthConfig(
        client_id="clientId",
        client_secret="clientSecret",
        redirect_uri="redirectUri"
    )

auth_request = AuthRequestBuilder.builder() \
    .source("gitee") \
    .auth_config_function(get_config) \
    .build()

# 自定义平台
auth_request = AuthRequestBuilder.builder() \
    .extend_source(CustomAuthSource) \
    .source("custom") \
    .auth_config(AuthConfig(
        client_id="clientId",
        client_secret="clientSecret",
        redirect_uri="redirectUri"
    )) \
    .build()

使用Redis缓存

默认情况下,SenWeaver OAuth使用内存缓存存储OAuth状态和令牌信息。对于分布式应用,可以使用Redis缓存:

import redis
from senweaver_oauth.cache import RedisCacheStore, DefaultCacheStore

# 创建Redis客户端
redis_client = redis.Redis(
    host="localhost",
    port=6379,
    db=0,
    password=None
)

# 创建Redis缓存存储
redis_cache = RedisCacheStore(
    redis_client=redis_client,
    prefix="senweaver_oauth:",  # 缓存键前缀
    ttl=300  # 默认过期时间(秒)
)

# 设置为默认缓存实例
DefaultCacheStore.set_instance(redis_cache)

更多缓存配置请参考缓存存储文档

已实现的平台

SenWeaver OAuth目前已集成以下40+平台的授权登录:

平台 授权方式 平台 授权方式 平台 授权方式
Github OAuth 2.0 Gitee OAuth 2.0 微博 OAuth 2.0
钉钉 OAuth 2.0 百度 OAuth 2.0 QQ OAuth 2.0
微信 OAuth 2.0 微信开放平台 OAuth 2.0 微信小程序 OAuth 2.0
微信企业版 OAuth 2.0 Google OAuth 2.0 Facebook OAuth 2.0
抖音 OAuth 2.0 领英 OAuth 2.0 微软 OAuth 2.0
飞书 OAuth 2.0 淘宝 OAuth 2.0 京东 OAuth 2.0
支付宝 OAuth 2.0 推特 OAuth 1.0a 小米 OAuth 2.0
华为 OAuth 2.0 企业微信 OAuth 2.0 酷家乐 OAuth 2.0
Gitlab OAuth 2.0 美团 OAuth 2.0 饿了么 OAuth 2.0
阿里云 OAuth 2.0 今日头条 OAuth 2.0 Teambition OAuth 2.0
StackOverflow OAuth 2.0 Pinterest OAuth 2.0 人人网 OAuth 2.0
腾讯云开发平台 OAuth 2.0 OSChina OAuth 2.0 Coding OAuth 2.0
Amazon OAuth 2.0 Slack OAuth 2.0 Line OAuth 2.0
喜马拉雅 OAuth 2.0 学科网 OAuth 2.0

更多平台正在持续添加中...

示例项目

贡献

欢迎贡献代码,提交问题和建议!

开源协议

MIT

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

senweaver_oauth-0.1.4.tar.gz (52.3 kB view details)

Uploaded Source

Built Distribution

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

senweaver_oauth-0.1.4-py3-none-any.whl (109.6 kB view details)

Uploaded Python 3

File details

Details for the file senweaver_oauth-0.1.4.tar.gz.

File metadata

  • Download URL: senweaver_oauth-0.1.4.tar.gz
  • Upload date:
  • Size: 52.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for senweaver_oauth-0.1.4.tar.gz
Algorithm Hash digest
SHA256 f678bb4c7f767a24bdd28478122188f4e79a6025953fa427865882f8a8533f9d
MD5 d46da94e17c5b47af97072580ab9c627
BLAKE2b-256 0ca508564b70416cf59fc787dd082de334c68ee2fcfe37a0713e038e0c0f8612

See more details on using hashes here.

Provenance

The following attestation bundles were made for senweaver_oauth-0.1.4.tar.gz:

Publisher: python-publish.yml on senweaver/senweaver-oauth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file senweaver_oauth-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for senweaver_oauth-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 58c65a3c5320d10b4e859af6e7e41c175350c0f64bfcad38b20b1fc27abb4d30
MD5 4ac9af99d53fd156affc5d0159a45ed9
BLAKE2b-256 88e49c46b70663eac6959fe6d6d4272ca27859ddfeb4c5cf8d2dd96abc4c87c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for senweaver_oauth-0.1.4-py3-none-any.whl:

Publisher: python-publish.yml on senweaver/senweaver-oauth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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