Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Oct 3, 2022
1 parent 6a4b56a commit fac2d73
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
23 changes: 11 additions & 12 deletions ovos_local_backend/backend/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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,
Expand All @@ -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/<uuid>/token/<oauth_id>", methods=['GET'])
Expand Down
20 changes: 20 additions & 0 deletions ovos_local_backend/database/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit fac2d73

Please sign in to comment.