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

In flet login flow, authorization should allow to override #776

Merged
merged 1 commit into from
Dec 28, 2022

Conversation

hokaso
Copy link
Contributor

@hokaso hokaso commented Dec 28, 2022

Authorization should allow to override, such as request_token() methods, there are some customizing authorization flows don't support to get access_token thought local client, even if using environment variables are not allowed. More reference: https://open.feishu.cn/document/common-capabilities/sso/api/get-access_token?lang=en-US

In this case, developers should write the code for subsequent authentication and obtaining user information by themselves. According to the above LARK authentication process, developers need to obtain user information from their own servers.

…ds, there are some customizing authorization flows don't support to get access_token thought local client, even if using environment variables are not allowed. More reference: https://open.feishu.cn/document/common-capabilities/sso/api/get-access_token?lang=en-US
@CLAassistant
Copy link

CLAassistant commented Dec 28, 2022

CLA assistant check
All committers have signed the CLA.

@hokaso
Copy link
Contributor Author

hokaso commented Dec 28, 2022

Override example:

from typing import Optional, List

from flet.auth.authorization import Authorization
from flet.auth.oauth_provider import OAuthProvider
from flet.auth.user import User


class LarkAuth(Authorization):

    def __init__(
            self,
            provider: OAuthProvider,
            fetch_user: bool,
            fetch_groups: bool,
            scope: Optional[List[str]] = None,
            saved_token: Optional[str] = None,
    ):
        super().__init__(
            provider,
            fetch_user,
            fetch_groups,
            scope,
            saved_token,
        )
        self.user = User(
            [],
            id="",
        )

    def request_token(self, code: str):
        self.user.code = code

Lark login example:

import os

import flet as ft
from flet.auth.oauth_provider import OAuthProvider

from page_with_lark import LarkAuth


class Test(object):

    def main(self, page: ft.Page):

        provider = OAuthProvider(
            client_id="your client_id",
            client_secret="",  # Leave empty
            authorization_endpoint="https://passport.feishu.cn/suite/passport/oauth/authorize",
            redirect_url="http://localhost:8550/api/oauth/redirect",
        )

        def login_click(e):

            page.login(
                provider,
                authorization=LarkAuth
            )

        def on_login(e):
            if e.error:
                raise Exception(e.error)
            print(page.auth.user.code)

            # Subsequent authentication flow

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


if __name__ == '__main__':
    test = Test()
    ft.app(target=test.main, port=8550)

@FeodorFitsner
Copy link
Contributor

Thank you! Looks good to me.

@FeodorFitsner FeodorFitsner merged commit ff35962 into flet-dev:main Dec 28, 2022
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