-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from LasseR15/feature/login-via-browser
Add login via browser + smaller docker image
- Loading branch information
Showing
10 changed files
with
112 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
services: | ||
bibox-to-pdf: | ||
image: ghcr.io/lasser15/bibox-to-pdf:latest | ||
restart: no | ||
build: | ||
context: . | ||
volumes: | ||
- ./books:/app/output/books | ||
ports: | ||
- '4200:4200' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
typer~=0.12.5 | ||
playwright~=1.47.0 | ||
ocrmypdf~=16.5.0 | ||
typer~=0.14.0 | ||
ocrmypdf~=16.6.2 | ||
img2pdf~=0.5.1 | ||
requests~=2.32.3 | ||
fastapi~=0.115.5 | ||
uvicorn~=0.32.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,94 @@ | ||
import typer | ||
from playwright.sync_api import sync_playwright | ||
from bibox_to_pdf.values.BiboxSelectors import BiboxSelectors | ||
import asyncio | ||
import base64 | ||
import hashlib | ||
import secrets | ||
import sys | ||
|
||
import requests | ||
import uvicorn | ||
from fastapi import FastAPI, Request, BackgroundTasks | ||
from fastapi.responses import HTMLResponse | ||
from rich import print as rprint | ||
|
||
from bibox_to_pdf.values.Constants import Constants | ||
from rich import print | ||
|
||
login_endpoint_queue = asyncio.Queue() | ||
|
||
app = FastAPI() | ||
config = uvicorn.Config(app, host='0.0.0.0', port=4200, log_level="warning") | ||
server = uvicorn.Server(config) | ||
|
||
@app.get('/login', response_class=HTMLResponse) | ||
async def login(req: Request, background_tasks: BackgroundTasks): | ||
code = req.query_params.get('code') | ||
if code is None: | ||
return '<h1>Error: Code is missing from request params</h1>' | ||
|
||
background_tasks.add_task(login_endpoint_queue.put, code) | ||
|
||
return '<h1>You can close this window now</h1>' | ||
|
||
|
||
async def start_webserver(): | ||
try: | ||
await server.serve() | ||
except Exception as e: | ||
rprint(f'Error starting webserver: {e}') | ||
sys.exit(1) | ||
|
||
def create_login_link(): | ||
login_url = Constants.biboxOauthLoginUrl | ||
client_id = Constants.biboxOauthClientId | ||
redirect_uri = 'http://localhost:4200/login' | ||
code_verifier = secrets.token_urlsafe(96)[:96] | ||
|
||
code_verifier_hashed = hashlib.sha256(code_verifier.encode('ascii')).digest() | ||
code_verifier_encoded = base64.urlsafe_b64encode(code_verifier_hashed) | ||
code_challenge = code_verifier_encoded.decode('ascii')[:-1] | ||
|
||
login_url = login_url + f'?client_id={client_id}&response_type=code&scope=openid&redirect_uri={redirect_uri}&code_challenge_method=S256&code_challenge={code_challenge}' | ||
|
||
return { | ||
'redirect_uri': redirect_uri, | ||
'code_verifier': code_verifier, | ||
'login_url': login_url, | ||
} | ||
|
||
def get_access_token(code: str, code_verifier: str, redirect_uri: str) -> str: | ||
token_endpoint = Constants.biboxOauthTokenUrl | ||
|
||
token_result = requests.post(token_endpoint, data={ | ||
'redirect_uri': redirect_uri, | ||
'code': code, | ||
'code_verifier': code_verifier, | ||
}) | ||
|
||
if token_result.status_code != 201 | 200: | ||
raise Exception(f'Error getting access token: {token_result.text}') | ||
|
||
def login_to_bibox(username: str, password: str) -> str: | ||
with sync_playwright() as p: | ||
print(f"Logging in to BiBox with user '{username}'") | ||
return token_result.json().get('access_token') | ||
|
||
browser = p.chromium.launch() | ||
page = browser.new_page() | ||
|
||
page.goto(Constants.biboxLoginUrl) | ||
page.wait_for_selector(BiboxSelectors.loginBtn) | ||
async def login_to_bibox() -> str: | ||
# Create a task in a separate thread with a webserver to handle the login callback | ||
webserver_task = asyncio.create_task(start_webserver()) | ||
|
||
page.type(BiboxSelectors.loginUsernameField, username) | ||
page.type(BiboxSelectors.loginPasswordField, password) | ||
while True: | ||
login_result = create_login_link() | ||
rprint('To log in to bibox open the following link in your browser: ') | ||
print(login_result["login_url"]) | ||
|
||
with page.expect_navigation(): | ||
page.click(BiboxSelectors.loginBtn) | ||
code_result = await login_endpoint_queue.get() | ||
|
||
try: | ||
page.wait_for_selector(BiboxSelectors.logoutBtn, timeout=10000) | ||
except: | ||
print('Login credentials incorrect or a network error occurred.') | ||
raise typer.Exit(1) | ||
access_token = get_access_token(code_result, login_result['code_verifier'], login_result['redirect_uri']) | ||
except Exception as e: | ||
rprint(f'Error getting access token: {e}') | ||
continue | ||
|
||
access_token = page.evaluate('() => window.localStorage.getItem("oauth.accessToken")') | ||
rprint('Successfully logged in to bibox') | ||
|
||
page.close() | ||
browser.close() | ||
await server.shutdown() | ||
webserver_task.cancel() | ||
|
||
return access_token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters