From 54d8ad46f8afdde7c2131812cf5a14bae0a67cad Mon Sep 17 00:00:00 2001 From: ka Date: Sat, 15 Jun 2024 14:11:05 -0300 Subject: [PATCH] feat: moved queries to database module --- ajusta_bling/database/queries/auth.py | 16 ++++++++++++++ ajusta_bling/web/__init__.py | 30 +++------------------------ 2 files changed, 19 insertions(+), 27 deletions(-) create mode 100644 ajusta_bling/database/queries/auth.py diff --git a/ajusta_bling/database/queries/auth.py b/ajusta_bling/database/queries/auth.py new file mode 100644 index 0000000..2d8df34 --- /dev/null +++ b/ajusta_bling/database/queries/auth.py @@ -0,0 +1,16 @@ +import psycopg2.extensions + + +def insert_token(cur: psycopg2.extensions.cursor, ip_address: str, access_token: str, refresh_token: str, expires_in: str): + cur.execute( + """ + INSERT INTO auth.tokens + (ip_address,access_token,refresh_token,expires_in) VALUES (%s,%s,%s,%s) + """, + ( + ip_address, # ip + access_token, + refresh_token, + expires_in, + ) + ) \ No newline at end of file diff --git a/ajusta_bling/web/__init__.py b/ajusta_bling/web/__init__.py index 9f31227..09f3d9a 100644 --- a/ajusta_bling/web/__init__.py +++ b/ajusta_bling/web/__init__.py @@ -7,6 +7,7 @@ from os import getenv import requests from flask import Flask, redirect, request, session, url_for +import ajusta_bling.database.queries.auth as sqlAuth from ajusta_bling.common import Args from ajusta_bling.database import Database @@ -44,38 +45,13 @@ def callback(): data = payload, headers= {"Authorization": header}) data = response.json() - print(response.url) - print(data) access_token: str = str(data["access_token"]) refresh_token: str = str(data["refresh_token"]) expires_in: int = int(data["expires_in"]) - with db.get_cur() as cur: # TODO - cur.execute( - """ - INSERT INTO auth.tokens - ( - ip_address, - access_token, - refresh_token, - expires_in - ) VALUES ( - %s, - %s, - %s, - %s - ) - """, - ( - request.remote_addr, # ip - access_token, - refresh_token, - str(expires_in), - ) - ) + with db.get_cur() as cur: + sqlAuth.insert_token(cur, request.remote_addr, access_token, refresh_token, expires_in) - #TODO: Store the session in the database - #insert_session(payload['access_token'], payload['refresh_token'], payload['expires_in']) return redirect(url_for('index')) @app.route("/")