flet icon indicating copy to clipboard operation
flet copied to clipboard

feat: Add WeChat oauth provider

Open pengwon opened this issue 1 year ago • 1 comments

Description

Added WeChat OAuth Provider by extending the existing OAuthToken class to include WeChat-specific fields openid and unionid, also modify Authorization class to adapt WeChat OAuth flow.

Test Code

import flet as ft
from flet.auth.provider import WeChatOAuthProvider


def main(page: ft.Page):
    provider = WeChatOAuthProvider(
        client_id="test_client_id",
        client_secret="test_client_secret",
        redirect_url="http://localhost:8550/oauth_callback",
    )

    def login_click(e):
        page.login(provider)

    def on_login(e):
        print("Login error:", e.error)
        print("Access token:", page.auth.token.access_token)
        print("User ID:", page.auth.user.id)

    page.on_login = on_login
    page.add(ft.ElevatedButton("Login with GitHub", on_click=login_click))


ft.app(main, port=8550, view=ft.AppView.WEB_BROWSER)

Type of change

  • [x] New feature (non-breaking change which adds functionality)

Checklist:

  • [x] I signed the CLA.
  • [x] My code follows the style guidelines of this project
  • [x] I have performed a self-review of my own code
  • [x] I have commented my code, particularly in hard-to-understand areas
  • [x] My changes generate no new warnings
  • [x] New and existing tests pass locally with my changes

Screenshots (if applicable):

Additional details

Summary by Sourcery

Add support for WeChat as an OAuth provider by implementing a new WeChatOAuthProvider class and extending the OAuthToken class to handle WeChat-specific fields. Update the Authorization class to integrate the WeChat OAuth flow, ensuring proper token and user data management.

New Features:

  • Introduce a new WeChat OAuth provider by extending the existing OAuthToken class to support WeChat-specific fields such as openid and unionid.

Enhancements:

  • Modify the Authorization class to accommodate the WeChat OAuth flow, including changes to request token handling and user information retrieval.

pengwon avatar Sep 26 '24 03:09 pengwon

Reviewer's Guide by Sourcery

This pull request adds WeChat OAuth provider support to the Flet authentication system. The implementation extends the existing OAuth functionality to accommodate WeChat's specific OAuth flow, which differs from standard implementations. Key changes include modifying the Authorization class, creating a new WeChatOAuthToken class, and implementing a new WeChatOAuthProvider class.

Sequence Diagram

sequenceDiagram
    participant User
    participant App
    participant WeChatOAuthProvider
    participant WeChatAPI
    User->>App: Initiate login
    App->>WeChatOAuthProvider: Request authorization URL
    WeChatOAuthProvider->>WeChatAPI: Generate authorization URL
    WeChatAPI-->>User: Present QR code
    User->>WeChatAPI: Scan QR code and authorize
    WeChatAPI-->>App: Redirect with authorization code
    App->>WeChatOAuthProvider: Request token
    WeChatOAuthProvider->>WeChatAPI: Exchange code for token
    WeChatAPI-->>WeChatOAuthProvider: Return token with openid
    WeChatOAuthProvider->>WeChatAPI: Fetch user info
    WeChatAPI-->>WeChatOAuthProvider: Return user details
    WeChatOAuthProvider-->>App: Return authenticated user

File-Level Changes

Change Details Files
Extend OAuthToken class to support WeChat-specific fields
  • Create new WeChatOAuthToken class inheriting from OAuthToken
  • Add 'openid' and 'unionid' fields to WeChatOAuthToken
sdk/python/packages/flet-runtime/src/flet_runtime/auth/oauth_token.py
Modify Authorization class to handle WeChat OAuth flow
  • Add 'appid' parameter to authorization URL generation
  • Implement WeChat-specific token request handling
  • Add conditional logic for fetching user data based on provider type
  • Implement WeChat-specific token conversion
sdk/python/packages/flet-runtime/src/flet_runtime/auth/authorization.py
Implement WeChatOAuthProvider class
  • Create new WeChatOAuthProvider class inheriting from OAuthProvider
  • Implement WeChat-specific endpoints and scopes
  • Add methods for fetching user details using WeChat's API
sdk/python/packages/flet-runtime/src/flet_runtime/auth/providers/wechat_oauth_provider.py

Tips and commands
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

sourcery-ai[bot] avatar Sep 26 '24 03:09 sourcery-ai[bot]

@pengwon Thank you for your contribution! Could you look into resolving conflicts in this PR please?

FeodorFitsner avatar Oct 29 '24 17:10 FeodorFitsner

I will have a try. Thank you for your review.

pengwon avatar Oct 30 '24 02:10 pengwon

At this time, we’re not accepting refactoring PRs, non-trivial changes, or new features for Flet v0.2x. Our focus is currently on preparing the upcoming “v1” release (in the v1 branch).

But if you'd like to adopt your PR for v1 branch that'd be awesome.

FeodorFitsner avatar Jun 12 '25 16:06 FeodorFitsner