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

Mr oauth #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions oauth_example/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
Count = user_counts.dataclass()

# Auth client setup for GitHub
redirect_uri = "http://localhost:8000/auth_redirect"
client = GitHubAppClient(os.getenv("AUTH_CLIENT_ID"),
os.getenv("AUTH_CLIENT_SECRET"),
redirect_uri="http://localhost:8000/auth_redirect")
login_link = client.login_link()
redirect_uri=redirect_uri)
login_link = client.login_link(redirect_uri=redirect_uri)


# Beforeware that puts the user_id in the request scope or redirects to the login page if not logged in.
Expand Down Expand Up @@ -50,7 +51,7 @@ def increment(auth):
@app.get('/login')
def login():
return Div(P("You are not logged in."),
A('Log in with GitHub', href=client.login_link()))
A('Log in with GitHub', href=login_link))

# To log out, we just remove the user_id from the session.
@app.get('/logout')
Expand All @@ -66,7 +67,7 @@ def auth_redirect(code:str, session, state:str=None):
print(f"state: {state}") # Not used in this example.
try:
# The code can be used once, to get the user info:
info = client.retr_info(code)
info = client.retr_info(code, redirect_uri=redirect_uri)
print(f"info: {info}")
# Use client.retr_id(code) directly if you just want the id, otherwise get the id with:
user_id = info[client.id_key]
Expand All @@ -90,4 +91,4 @@ def auth_redirect(code:str, session, state:str=None):
print(f"Error: {e}")
return f"Could not log in."

serve(port=8000)
serve(port=8000)
7 changes: 4 additions & 3 deletions oauth_example/minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
import json

# Auth client
redirect_uri = "http://localhost:8000/auth_redirect"
client = GitHubAppClient(os.getenv("AUTH_CLIENT_ID"),
os.getenv("AUTH_CLIENT_SECRET"),
redirect_uri="http://localhost:8000/auth_redirect")
redirect_uri=redirect_uri)

app = FastHTML()

@app.get('/')
def home():
return P("Login link: ", A("click here", href=client.login_link()))
return P("Login link: ", A("click here", href=client.login_link(redirect_uri=redirect_uri)))

@app.get('/auth_redirect')
def auth_redirect(code:str, session):
info = client.retr_info(code)
info = client.retr_info(code, redirect_uri=redirect_uri)
return H1("Profile info"), P(json.dumps(info))

serve(port=8000)
8 changes: 6 additions & 2 deletions oauth_example/oa.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from fasthtml.common import *
from fasthtml.oauth import GoogleAppClient, OAuth

cli = GoogleAppClient.from_file('/Users/jhoward/git/nbs/oauth-test/client_secret.json')
redirect_uri = "http://localhost:5001/redirect"
cli = GoogleAppClient.from_file('/Users/jhoward/git/nbs/oauth-test/client_secret.json',
redirect_uri=redirect_uri)

class Auth(OAuth):
def login(self, info, state): return RedirectResponse('/', status_code=303)
Expand All @@ -16,7 +18,9 @@ def chk_auth(self, info, ident, session):
def home(auth): return P('Logged in!'), A('Log out', href='/logout')

@app.get('/login')
def login(req): return Div(P("Not logged in"), A('Log in', href=oauth.login_link(req)))
def login(req):
return Div(P("Not logged in"), A('Log in', href=cli.login_link(redirect_uri=redirect_uri)))


serve()