Skip to content

Commit

Permalink
feat(session): used django session to store AWS credentials (#1)
Browse files Browse the repository at this point in the history
* feat(aws): used django session to store AWS credentials temporarily

* chore(session): removed auto-deletion of session data
  • Loading branch information
ronilrufo-domain authored Jul 4, 2024
1 parent dadb400 commit 05e399a
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions s3direct/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -125,13 +131,19 @@ 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')
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

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(aws_credentials.secret_key,
signing_date, region, 's3')
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})
Expand Down

0 comments on commit 05e399a

Please sign in to comment.