aboutsummaryrefslogtreecommitdiff
path: root/uploader/oauth2/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'uploader/oauth2/views.py')
-rw-r--r--uploader/oauth2/views.py40
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")