From fac2d73ede6d2e12059af89f26157dd5958516d4 Mon Sep 17 00:00:00 2001 From: jarbas Date: Tue, 4 Oct 2022 00:25:15 +0100 Subject: [PATCH] oauth --- ovos_local_backend/backend/auth.py | 23 +++++++++++------------ ovos_local_backend/database/oauth.py | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/ovos_local_backend/backend/auth.py b/ovos_local_backend/backend/auth.py index 500e643..0f46047 100644 --- a/ovos_local_backend/backend/auth.py +++ b/ovos_local_backend/backend/auth.py @@ -19,13 +19,11 @@ from ovos_local_backend.backend import API_VERSION from ovos_local_backend.backend.decorators import noindex, requires_auth -from ovos_local_backend.database.oauth import OAuthTokenDatabase +from ovos_local_backend.database.oauth import OAuthTokenDatabase, OAuthApplicationDatabase from ovos_local_backend.utils import nice_json def get_auth_routes(app): - oauth_in_progress = {} - @app.route(f"/{API_VERSION}/auth/token", methods=['GET']) @requires_auth @noindex @@ -50,18 +48,20 @@ def oauth_url(oauth_id): once user opens it callback is triggered """ params = dict(request.args) - - oauth_in_progress[oauth_id] = params - client = WebApplicationClient(params["client_id"]) - params["_client"] = client - oauth_in_progress[oauth_id] = params - request_uri = client.prepare_request_uri( params["auth_endpoint"], redirect_uri=request.base_url + f"/{API_VERSION}/auth/callback/{oauth_id}", scope=params["scope"], ) + with OAuthApplicationDatabase() as db: + db.add_application(oauth_id, + params["client_id"], + params["client_secret"], + params["auth_endpoint"], + params["token_endpoint"], + params["refresh_endpoint"], + params["scope"]) return request_uri, 200 @@ -74,13 +74,13 @@ def oauth_callback(oauth_id): params = dict(request.args) code = params["code"] - data = oauth_in_progress[oauth_id] - client = data["_client"] + data = OAuthApplicationDatabase()[oauth_id] client_id = data["client_id"] client_secret = data["client_secret"] token_endpoint = data["token_endpoint"] # Prepare and send a request to get tokens! Yay tokens! + client = WebApplicationClient(client_id) token_url, headers, body = client.prepare_token_request( token_endpoint, authorization_response=request.url, @@ -97,7 +97,6 @@ def oauth_callback(oauth_id): with OAuthTokenDatabase() as db: db.add_token(oauth_id, token_response) - oauth_in_progress.pop(oauth_id) return nice_json(params) @app.route(f"/{API_VERSION}/device//token/", methods=['GET']) diff --git a/ovos_local_backend/database/oauth.py b/ovos_local_backend/database/oauth.py index 8797c1d..9dac197 100644 --- a/ovos_local_backend/database/oauth.py +++ b/ovos_local_backend/database/oauth.py @@ -10,3 +10,23 @@ def add_token(self, oauth_service, token_data): def total_tokens(self): return len(self) + + +class OAuthApplicationDatabase(JsonStorageXDG): + def __init__(self): + super().__init__("ovos_oauth_apps") + + def add_application(self, oauth_service, + client_id, client_secret, + auth_endpoint, token_endpoint, refresh_endpoint, + scope): + self[oauth_service] = {"oauth_service": oauth_service, + "client_id": client_id, + "client_secret": client_secret, + "auth_endpoint": auth_endpoint, + "token_endpoint": token_endpoint, + "refresh_endpoint": refresh_endpoint, + "scope": scope} + + def total_apps(self): + return len(self)