-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_user_login.py
More file actions
37 lines (25 loc) · 925 Bytes
/
01_user_login.py
File metadata and controls
37 lines (25 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Example 1: User login with automatic flow selection.
Tries browser (Auth Code + PKCE) first; falls back to Device Code flow
when no browser is available (e.g., SSH sessions, CI).
Usage:
uv run python examples/01_user_login.py
"""
from __future__ import annotations
import os
import authgate
AUTHGATE_URL = os.environ["AUTHGATE_URL"]
CLIENT_ID = os.environ["CLIENT_ID"]
def main() -> None:
# authenticate() caches the token on disk and refreshes it automatically.
client, token = authgate.authenticate(
AUTHGATE_URL,
CLIENT_ID,
scopes=["openid", "profile", "email"],
)
print(f"Logged in! access_token={token.access_token[:20]}...")
# Use the client to call an OAuth endpoint (e.g., UserInfo).
userinfo = client.userinfo(token.access_token)
print(f"Subject : {userinfo.sub}")
print(f"Email : {userinfo.email}")
if __name__ == "__main__":
main()