flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.61k stars 455 forks source link

feat: Add wechat oauth provider #4043

Closed pengwon closed 2 months ago

pengwon commented 2 months ago

Description

Add WeChat OAuth Provider

Test Code

import unittest
from authorization import Authorization

class TestWeChatOAuthProvider(unittest.TestCase):
    def setUp(self):
        self.auth = Authorization(provider=WeChatProvider())

    def test_get_authorization_data(self):
        authorization_url, state = self.auth.get_authorization_data()
        self.assertIn("https://api.weixin.qq.com", authorization_url)
        self.assertEqual(len(state), 22)

    def test_request_token(self):
        code = "test_code"
        self.auth.request_token(code)
        self.assertIsNotNone(self.auth.token)

    async def test_request_token_async(self):
        code = "test_code"
        await self.auth.request_token_async(code)
        self.assertIsNotNone(self.auth.token)

if __name__ == "__main__":
    unittest.main()

Type of change

Checklist:

Screenshots (if applicable):

Additional details

Summary by Sourcery

Add a WeChat OAuth provider to the authentication module, allowing users to authenticate using WeChat credentials. This includes handling specific WeChat OAuth token fields and fetching user details. Unit tests are included to ensure the functionality works as expected.

New Features:

Enhancements:

Tests:

sourcery-ai[bot] commented 2 months ago

Reviewer's Guide by Sourcery

This pull request adds WeChat OAuth provider support to the Flet runtime authentication system. The implementation includes modifications to existing authentication classes and the addition of a new WeChat-specific OAuth provider class.

File-Level Changes

Change Details Files
Add WeChat OAuth provider implementation
  • Create WeChatOAuthProvider class with WeChat-specific endpoints and methods
  • Implement user fetching methods for WeChat
  • Add WeChat-specific parameters (openid, unionid) to OAuth token
sdk/python/packages/flet-runtime/src/flet_runtime/auth/providers/wechat_oauth_provider.py
Modify Authorization class to support WeChat OAuth flow
  • Add WeChat-specific logic in get_authorization_data method
  • Implement WeChat-specific token request handling
  • Update user fetching methods to handle WeChat's unique user data structure
sdk/python/packages/flet-runtime/src/flet_runtime/auth/authorization.py
Update User class to be more flexible
  • Modify User class initialization to accept arbitrary keyword arguments
  • Add dynamic attribute setting for User objects
sdk/python/packages/flet-runtime/src/flet_runtime/auth/user.py
Extend OAuthToken to include WeChat-specific fields
  • Add openid and unionid fields to OAuthToken class
sdk/python/packages/flet-runtime/src/flet_runtime/auth/oauth_token.py

Sequence Diagram

sequenceDiagram
    participant C as Client
    participant A as Authorization
    participant W as WeChatOAuthProvider
    participant WA as WeChat API
    C->>A: get_authorization_data()
    A->>W: Generate authorization URL
    W-->>A: Return URL and state
    A-->>C: Return authorization URL and state
    C->>A: request_token(code)
    A->>W: Prepare token request
    W->>WA: Request access token
    WA-->>W: Return access token and user info
    W-->>A: Return token and user data
    A-->>C: Return authenticated user

Tips - 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](https://app.sourcery.ai): - Enable or disable the Sourcery-generated pull request summary or reviewer's guide; - Change the review language; - You can always [contact us](mailto:support@sourcery.ai) if you have any questions or feedback.
CLAassistant commented 2 months ago

CLA assistant check
All committers have signed the CLA.