diff options
author | Frederick Muriuki Muriithi | 2025-07-14 12:33:05 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2025-07-14 12:33:05 -0500 |
commit | d54b00a0901cd5ff8b9f7c5588c3d47dcda80966 (patch) | |
tree | 9c3bddc3a1b0cbb420fc5b9276f0267ecfcc4883 /uploader/oauth2/views.py | |
parent | 284f4b1141c9e13dfdbdf59699d73ddb3dcdf17b (diff) | |
download | gn-uploader-d54b00a0901cd5ff8b9f7c5588c3d47dcda80966.tar.gz |
Refactor out common functionality into separate function.
Diffstat (limited to 'uploader/oauth2/views.py')
-rw-r--r-- | uploader/oauth2/views.py | 40 |
1 files changed, 7 insertions, 33 deletions
diff --git a/uploader/oauth2/views.py b/uploader/oauth2/views.py index db4ef61..1ee4257 100644 --- a/uploader/oauth2/views.py +++ b/uploader/oauth2/views.py @@ -1,9 +1,6 @@ """Views for OAuth2 related functionality.""" -import uuid -from datetime import datetime, timedelta from urllib.parse import urljoin, urlparse, urlunparse -from authlib.jose import jwt from flask import ( flash, jsonify, @@ -18,9 +15,8 @@ from uploader import monadic_requests as mrequests from uploader.monadic_requests import make_error_handler from . import jwks +from .tokens import request_token from .client import ( - SCOPE, - oauth2_get, user_logged_in, authserver_uri, oauth2_clientid, @@ -33,8 +29,8 @@ oauth2 = Blueprint("oauth2", __name__) @oauth2.route("/code") def authorisation_code(): """Receive authorisation code from auth server and use it to get token.""" - def __process_error__(resp_or_exception): - app.logger.debug("ERROR: (%s)", resp_or_exception) + def __process_error__(error_response): + app.logger.debug("ERROR: (%s)", error_response.content) flash("There was an error retrieving the authorisation token.", "alert alert-danger") return redirect("/") @@ -60,36 +56,14 @@ def authorisation_code(): return redirect("/") baseurl = urlparse(request.base_url, scheme=request.scheme) - issued = datetime.now() - jwtkey = jwks.newest_jwk_with_rotation( - jwks.jwks_directory(app, "UPLOADER_SECRETS"), - int(app.config["JWKS_ROTATION_AGE_DAYS"])) - return mrequests.post( - urljoin(authserver_uri(), "auth/token"), - json={ - "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer", + return request_token( + token_uri=urljoin(authserver_uri(), "auth/token"), + user_id=request.args["user_id"], + extra_params={ "code": code, - "scope": SCOPE, "redirect_uri": urljoin( urlunparse(baseurl), url_for("oauth2.authorisation_code")), - "assertion": jwt.encode( - header={ - "alg": "RS256", - "typ": "JWT", - "kid": jwtkey.as_dict()["kid"] - }, - payload={ - "iss": str(oauth2_clientid()), - "sub": request.args["user_id"], - "aud": urljoin(authserver_uri(),"auth/token"), - "exp": (issued + timedelta(minutes=5)).timestamp(), - "nbf": int(issued.timestamp()), - "iat": int(issued.timestamp()), - "jti": str(uuid.uuid4()) - }, - key=jwtkey).decode("utf8"), - "client_id": oauth2_clientid() }).either(__process_error__, __success__) @oauth2.route("/public-jwks") |