Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(session): used django session to store AWS credentials #1

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
ronilrufo-domain marked this conversation as resolved.
Show resolved Hide resolved

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