Skip to content

Commit

Permalink
Merge branch 'export-cert'
Browse files Browse the repository at this point in the history
Adds a button to export certificates.

Closes #150
  • Loading branch information
tobiasbrunner committed Jan 16, 2024
2 parents c30f51a + ddfa9d5 commit ef74464
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 39 deletions.
34 changes: 20 additions & 14 deletions strongMan/apps/certificates/static/certificates/details.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/**
* Created by root on 18/04/16.
*/
jQuery(function($) {
var $remove_btns = $('button.remove-btn');
$remove_btns.each(function () {
var $btn = $(this);
var $form = $btn.closest('form');

function removeBtnClicked(form) {
jform = $(form);
btn = jform.find('.remove-btn');
if (btn.hasClass('btn-default')) {
btn.removeClass('btn-default').addClass('btn-danger');
btn.find('.removebtn-text').text('Are you sure?');
return false;
} else {
return true;
}
}
var $submitter = null;
$form.submit(function (form) {
if ($submitter && $submitter[0] == $btn[0] && $btn.hasClass('btn-default')) {
$btn.removeClass('btn-default').addClass('btn-danger');
$btn.find('.removebtn-text').text('Are you sure?');
return false;
} else {
return true;
}
});
$form.find('button[type=submit]').click(function () {
$submitter = $(this);
});
});
});
48 changes: 25 additions & 23 deletions strongMan/apps/certificates/templates/certificates/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,23 @@ <h2 class="text-center">
</div>
</div>
<div class="row">
<div class="col-xs-12">
{% if not readonly %}
<div class="row text-center">
<form action="" method="POST" onsubmit="return removeBtnClicked(this)">
{% csrf_token %}
<span title="Remove">
<button type="submit" class="btn btn-default remove-btn" name="remove_cert"
id="remove_cert-btn">
<span class="glyphicon glyphicon-remove"></span> <span
class="removebtn-text">Remove</span>
</button>
</span>
</form>
<div class="col-xs-12 text-center">
<form action="" method="POST">
{% csrf_token %}
<div class="btn-group" role="group" aria-label="Certificate operations">
<button type="submit" class="btn btn-default" name="export_cert"
id="export_cert-btn" title="Download certificate in PEM format">
<span class="glyphicon glyphicon-download"></span> <span>Download</span>
</button>
{% if not readonly %}
<button type="submit" class="btn btn-default remove-btn" name="remove_cert"
id="remove_cert-btn" title="Remove certificate">
<span class="glyphicon glyphicon-remove"></span> <span
class="removebtn-text">Remove</span>
</button>
{% endif %}
</div>
{% endif %}
</form>
</div>
</div>

Expand Down Expand Up @@ -214,18 +216,18 @@ <h3 class="text-center"><span class="glyphicon glyphicon-lock" aria-hidden="fals
</div>
</div>
<div class="row">
<div class="text-center">
<form action="" method="POST" onsubmit="return removeBtnClicked(this)">
<div class="col-xs-12 text-center">
<form action="" method="POST">
{% csrf_token %}
<span title="Remove private key">
<button type="submit" class="btn btn-default remove-btn" name="remove_privatekey"
id="remove_privatekey-btn">
<span class="glyphicon glyphicon-remove"></span> <span class="removebtn-text">Remove private key</span>
</button>
</span>
<span title="Remove private key">
<button type="submit" class="btn btn-default remove-btn" name="remove_privatekey"
id="remove_privatekey-btn">
<span class="glyphicon glyphicon-remove"></span> <span class="removebtn-text">Remove private key</span>
</button>
</span>
</form>
</div>
</div>
{% endif %}
</div>
{% endblock content %}
{% endblock content %}
23 changes: 22 additions & 1 deletion strongMan/apps/certificates/views/DetailsHandler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import oscrypto.asymmetric
from urllib.parse import quote

from django.contrib import messages
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render
from ..models import UserCertificate, ViciCertificate, CertificateDoNotDelete
from ..forms import ChangeNicknameForm
Expand Down Expand Up @@ -28,11 +31,15 @@ def _render_user_details(self):

def handle(self):
if self._is_vicicert():
if self.request.method == "POST" and "export_cert" in self.request.POST:
return self._export_certificate()
return self._render_vici_details()

if self.request.method == "GET":
return self._render_user_details()
elif self.request.method == "POST":
if "export_cert" in self.request.POST:
return self._export_certificate()
if "remove_cert" in self.request.POST:
return self._delete_cert()
elif "remove_privatekey" in self.request.POST:
Expand Down Expand Up @@ -78,3 +85,17 @@ def _delete_private_key(self):
messages.add_message(self.request, messages.ERROR, "Can't delete private key. " + str(e))

return self._render_user_details()

def _export_certificate(self):
try:
cert = oscrypto.asymmetric.load_certificate(self.certificate.der_container)
pem_bytes = oscrypto.asymmetric.dump_certificate(cert)

filename = quote(self._vicicert.nickname if self._is_vicicert() else self._usercert.nickname)

response = HttpResponse(pem_bytes, content_type="application/x-pem-file")
response['Content-Disposition'] = "attachment; filename*=utf-8''%s.pem" % filename
return response
except Exception as e:
messages.add_message(self.request, messages.ERROR, "Couldn't export the cert. " + str(e))
return self._render_user_details()
2 changes: 1 addition & 1 deletion strongMan/helper_apps/vici/wrapper/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get_certificates(self):
:rtype: list
'''
certificates = []
for certificate in self.session.list_certs():
for certificate in self.session.list_certs({"type": "X509"}):
certificates.append(certificate)
return certificates

Expand Down

0 comments on commit ef74464

Please sign in to comment.