From b43fff99aba494c616eec764c5f30ec6105009ad Mon Sep 17 00:00:00 2001 From: Ronil Rufo Date: Tue, 2 Jul 2024 14:01:55 +0800 Subject: [PATCH 1/2] feat(aws): used django session to store AWS credentials temporarily --- s3direct/views.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/s3direct/views.py b/s3direct/views.py index e9cabcbf..d0047fa3 100644 --- a/s3direct/views.py +++ b/s3direct/views.py @@ -1,17 +1,20 @@ import json from datetime import datetime + from django.conf import settings from django.http import (HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, HttpResponseNotFound, HttpResponseServerError) from django.views.decorators.csrf import csrf_protect from django.views.decorators.http import require_POST + try: from urllib.parse import unquote except ImportError: from urlparse import unquote + from .utils import (get_aws_credentials, get_aws_v4_signature, - get_aws_v4_signing_key, get_s3direct_destinations, get_key) + get_aws_v4_signing_key, get_key, get_s3direct_destinations) @csrf_protect @@ -67,6 +70,9 @@ def get_upload_params(request): return HttpResponseServerError(resp, content_type='application/json') aws_credentials = get_aws_credentials() + request.session["aws_access_key_id"] = aws_credentials.access_key + request.session["aws_secret_access_key"] = aws_credentials.secret_key + if not aws_credentials.secret_key or not aws_credentials.access_key: resp = json.dumps({'error': 'AWS credentials config missing.'}) return HttpResponseServerError(resp, content_type='application/json') @@ -125,13 +131,22 @@ def generate_aws_v4_signature(request): resp = json.dumps({'error': 'S3 region config missing.'}) return HttpResponseServerError(resp, content_type='application/json') - aws_credentials = get_aws_credentials() - if not aws_credentials.secret_key or not aws_credentials.access_key: - resp = json.dumps({'error': 'AWS credentials config missing.'}) - return HttpResponseServerError(resp, content_type='application/json') - - signing_key = get_aws_v4_signing_key(aws_credentials.secret_key, - signing_date, region, 's3') + try: + access_key = request.session["aws_access_key_id"] + secret_key = request.session["aws_secret_access_key"] + except KeyError: + aws_credentials = get_aws_credentials() + access_key = aws_credentials.access_key + secret_key = aws_credentials.secret_key + else: + for key in ["aws_access_key_id", "aws_secret_access_key"]: + del request.session[key] + + if not secret_key or not access_key: + resp = json.dumps({"error": "AWS credentials config missing."}) + return HttpResponseServerError(resp, content_type="application/json") + + signing_key = get_aws_v4_signing_key(secret_key, signing_date, region, "s3") signature = get_aws_v4_signature(signing_key, message) resp = json.dumps({'s3ObjKey': signature}) From 055c7018158ce152f0efdef6509dbd226fe0dc9f Mon Sep 17 00:00:00 2001 From: Ronil Rufo Date: Tue, 2 Jul 2024 15:19:59 +0800 Subject: [PATCH 2/2] chore(session): removed auto-deletion of session data --- s3direct/views.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/s3direct/views.py b/s3direct/views.py index d0047fa3..2cc3b6ce 100644 --- a/s3direct/views.py +++ b/s3direct/views.py @@ -138,9 +138,6 @@ def generate_aws_v4_signature(request): aws_credentials = get_aws_credentials() access_key = aws_credentials.access_key secret_key = aws_credentials.secret_key - else: - for key in ["aws_access_key_id", "aws_secret_access_key"]: - del request.session[key] if not secret_key or not access_key: resp = json.dumps({"error": "AWS credentials config missing."})