diff --git a/db.sqlite3 b/db.sqlite3 index 935fc10..94babe3 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/mainapp/views.py b/mainapp/views.py index cf1fbf9..064f6b9 100644 --- a/mainapp/views.py +++ b/mainapp/views.py @@ -5,12 +5,11 @@ from PIL import Image import base64 from css_html_js_minify import js_minify,process_single_js_file -# Create your views here. +# Create your views here.'' def home(request): tools = models.Tool.objects.all() categories = models.Tool.objects.order_by().values_list('category',flat=True).distinct() context={'tools':tools,'categories':categories} - print(settings.MEDIA_URL) return render(request,'general/home.html',context) def tool(request,tool_name): @@ -29,7 +28,6 @@ def user_profile(request,user_name): def tags(request,tag_name): tag = get_object_or_404(models.Tag,tag=tag_name) tools = tag.tool_set.all() - print(tools) return render(request,'general/tags.html',{'tools':tools}) @@ -59,7 +57,6 @@ def convert_file(request): output = pypandoc.convert_file(input_file_path,convert_to,outputfile=output_file_path) if os.path.exists(output_file_path): - print('exists') with open(output_file_path, 'rb+') as fh: response = HttpResponse(fh.read(), content_type="application/force-download") response['Content-Disposition'] = 'inline; filename=' + os.path.basename(output_file_path) @@ -80,11 +77,9 @@ def JpgToPng(request): path = default_storage.save(input_file_path,ContentFile(file_to_convert.read())) im = Image.open(input_file_path) im.save(output_file_path) - png_img = Image.open(output_file_path) - print("saved file: ", png_img) + png_img = Image.open(output_file_path) if os.path.exists(output_file_path): - print('exists') with open(output_file_path, 'rb+') as fh: response = HttpResponse(fh.read(), content_type="application/force-download") response['Content-Disposition'] = 'inline; filename=' + os.path.basename(output_file_path) @@ -114,7 +109,6 @@ def download_minified_file(request): input_file_path = os.path.join(settings.MEDIA_ROOT,'files',request.FILES.get("file").name) path = default_storage.save(input_file_path,ContentFile(request.FILES.get("file").read())) z = process_single_js_file(input_file_path,overwrite=False) - print(z); with open(z, 'rb+') as fh: res = HttpResponse(fh.read(),content_type="application/js") res['Content-Disposition'] = 'attachment; filename='+ os.path.basename(z) @@ -126,7 +120,6 @@ def download_minified_file(request): #sample download tool starts def about_sample_file(request,format): name = "sample."+format - print("i am working for ",name) input_file_path = os.path.join(settings.MEDIA_ROOT,'sample',name) with open(input_file_path, 'rb+') as fh: size = os.path.getsize(input_file_path)/1024 @@ -146,3 +139,58 @@ def download_sample_file(request,format): response['Content-Disposition'] = 'inline; filename=' + os.path.basename(input_file_path) return response return HttpResponse('Error while converting', status=404) + + + +# authentication with discourse sso +import base64 +import hmac +import hashlib +from urllib import parse + +from django.contrib.auth.decorators import login_required +from django.http import HttpResponseBadRequest, HttpResponseRedirect +from django.conf import settings + +@login_required +def sso(request): + payload = request.GET.get('sso') + signature = request.GET.get('sig') + + if payload is None or signature is None: + return HttpResponseBadRequest('No SSO payload or signature. Please contact support if this problem persists.') + + ## Validate the payload + + try: + payload = bytes(parse.unquote(payload), encoding='utf-8') + decoded = base64.decodestring(payload).decode('utf-8') + assert 'nonce' in decoded + assert len(payload) > 0 + except AssertionError: + return HttpResponseBadRequest('Invalid payload. Please contact support if this problem persists.') + + key = bytes(settings.DISCOURSE_SSO_SECRET, encoding='utf-8') # must not be unicode + h = hmac.new(key, payload, digestmod=hashlib.sha256) + this_signature = h.hexdigest() + + if not hmac.compare_digest(this_signature, signature): + return HttpResponseBadRequest('Invalid payload. Please contact support if this problem persists.') + + ## Build the return payload + + qs = parse.parse_qs(decoded) + params = { + 'nonce': qs['nonce'][0], + 'email': request.user.email, + 'external_id': request.user.id, + 'username': request.user.username, + 'require_activation': 'true' + } + + return_payload = base64.encodestring(bytes(parse.urlencode(params), 'utf-8')) + h = hmac.new(key, return_payload, digestmod=hashlib.sha256) + query_string = parse.urlencode({'sso': return_payload, 'sig': h.hexdigest()}) + + ## Redirect back to Discourse + return HttpResponseRedirect('%s?%s' % (settings.DISCOURSE_BASE_URL, query_string)) \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 556ef9e..4eb521f 100644 --- a/templates/base.html +++ b/templates/base.html @@ -26,7 +26,7 @@