Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add WeChat oauth provider #4050

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pengwon
Copy link

@pengwon pengwon commented Sep 26, 2024

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

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

Checklist:

  • I signed the CLA.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • 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.

Copy link
Contributor

sourcery-ai bot commented Sep 26, 2024

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
Loading

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.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @pengwon - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider making the WeChat OAuth URLs (authorization_endpoint, token_endpoint, user_endpoint) configurable parameters in the WeChatOAuthProvider class constructor, rather than hardcoding them.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@FeodorFitsner
Copy link
Contributor

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

* main: (31 commits)
  Migrate `colors` and `icons` variables to Enums (flet-dev#4180)
  feat: expose more properties in Controls (flet-dev#4105)
  feat!: Refactor `Badge` Control to a Dataclass; create new `Control.badge` property (flet-dev#4077)
  fix: clicking on `CupertinoContextMenuAction` doesn't close context menu (flet-dev#3948)
  fix dropdown `max_menu_height` (flet-dev#3974)
  Fix undefined name "Future" --> asyncio.Future (flet-dev#4230)
  wrap ListTile in material widget (flet-dev#4206)
  Update CONTRIBUTING.md (flet-dev#4208)
  TextField: suffix_icon, prefix_icon and icon can be Control or str (flet-dev#4173)
  feat!: enhance `Map` control (flet-dev#3994)
  skip running flutter doctor on windows if no_rich_output is True (flet-dev#4108)
  add --pyinstaller-build-args to pack cli command (flet-dev#4187)
  fix/feat: make `SearchBar`'s view height adjustable; add new properties (flet-dev#4039)
  fix: prevent button `style` from being modified in `before_update()` (flet-dev#4181)
  fix: disabling filled buttons is not visually respected (flet-dev#4090)
  when `label` is set, use `MainAxisSize.min` for the `Row` (flet-dev#3998)
  fix: `NavigationBarDestination.disabled` has no visual effect (flet-dev#4073)
  fix autofill in CupertinoTextField (flet-dev#4103)
  Linechart: jsonDecode tooltip before displaying (flet-dev#4069)
  fixed bgcolor, color and elevation (flet-dev#4126)
  ...
@pengwon
Copy link
Author

pengwon commented Oct 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants