Skip to content

Authentication #1

Closed
Closed
@FeodorFitsner

Description

@FeodorFitsner

Useful materials:

Configuring OAuth provider:

provider = GitHubOAuthProvider(
  "<client_id>",
  "<client_secret>",
  "<redirect_url>",
  ["user", "public_repo])

Authenticate (start OAuth flow) user:

auth: Authentication = page.login(provider, fetch_user=True, fetch_groups=True)
# fetch_users, fetch_groups if enabled ensure required scopes are set
page.auth # the last Authentication
page.auth.token # authentication token
page.auth.user # instance of AuthUser class
page.auth.provider # instance of OAuthProvider

Check if a session is authenticated:

if page.auth != None:
  # user is logged in
class OAuthProvider():
  _name
  client_id
  client_secret
  authorization_endpoint
  token_endpoint
  redirect_url
  def get_user():
    pass
class OAuthToken():
  token
  created
  expiresIn
  refreshToken
  scope
  token_type
class AuthUser():
  id
class GitHubUser(AuthUser):
  full_name
  email
  teams

Providers

GitHub

  • authorization_endpoint: https://github.com/login/oauth/authorize
  • token_endpoint: https://github.com/login/oauth/access_token
  • User scope: read:user, user:email
  • Groups scope: read:org
  • Get emails
  • Get user
  • Get teams

Google

  • authorization_endpoint: https://accounts.google.com/o/oauth2/auth
  • token_endpoint: https://oauth2.googleapis.com/token
  • User scope: https://www.googleapis.com/auth/userinfo.email, https://www.googleapis.com/auth/userinfo.profile
  • Groups scope: ?
  • Get user: https://www.googleapis.com/oauth2/v3/userinfo
    • sub
    • email
    • name

Azure

Redirect URLs

Web: http(s)://{application_url}/api/oauth/redirect
Desktop: http://localhost/api/oauth/redirect
Mobile: flet://api/oauth/redirect

login(provider) method flow

  • Generate random state value, save state in Authentication object.
  • Build authorization URL with redirect_url.
  • Call page.oauth_login(authorization_url, state).
  • Fletd stores an expiring cache object under oauth_state_{state} key and page_id:session_id value.
  • Fletd calls oauth_login on a client with authorization_url and state.
  • Flet client opens authorization_url in:
  • After user login and consent on OAuth provider side:
    • Web and desktop:
      • Redirects back to redirect_url.
      • Fletd fetches page_id:session_id by oauth_state_{state} key. Verifies state.
      • Fletd sends on_authorize event to a Python code with code, error, state.
      • Fletd generates HTML output with JavaScript code closing browser window/tab.
    • Mobile:
      • Verifies state.
      • Flet client sends on_authorize event to a Python code with code, error, state.
  • On Python side internal on_authorize handler called:
    • Validate state
    • Request token with code, client_secret, token_endpoint.
  • user and groups optionally fetch.
  • page.on_login event handler called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImprovement/Optimization

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions