1
0
mirror of https://github.com/laurivosandi/certidude synced 2024-12-23 00:25:18 +00:00

api: Fix request submission form

This commit is contained in:
Lauri Võsandi 2018-01-02 14:49:06 +00:00
parent 3d1e6768bb
commit 345c2802ea
4 changed files with 77 additions and 45 deletions

View File

@ -11,7 +11,7 @@ from asn1crypto.csr import CertificationRequest
from base64 import b64decode from base64 import b64decode
from certidude import config, authority, push, errors from certidude import config, authority, push, errors
from certidude.auth import login_required, login_optional, authorize_admin from certidude.auth import login_required, login_optional, authorize_admin
from certidude.decorators import csrf_protection, MyEncoder from certidude.decorators import csrf_protection, MyEncoder, serialize
from certidude.firewall import whitelist_subnets, whitelist_content_types from certidude.firewall import whitelist_subnets, whitelist_content_types
from datetime import datetime from datetime import datetime
from oscrypto import asymmetric from oscrypto import asymmetric
@ -38,8 +38,14 @@ class RequestListResource(object):
reasons = [] reasons = []
body = req.stream.read(req.content_length) body = req.stream.read(req.content_length)
try:
header, _, der_bytes = pem.unarmor(body) header, _, der_bytes = pem.unarmor(body)
csr = CertificationRequest.load(der_bytes) csr = CertificationRequest.load(der_bytes)
except ValueError:
raise falcon.HTTPBadRequest(
"Bad request",
"Malformed certificate signing request")
common_name = csr["certification_request_info"]["subject"].native["common_name"] common_name = csr["certification_request_info"]["subject"].native["common_name"]
""" """
@ -164,6 +170,9 @@ class RequestListResource(object):
# Request was accepted, but not processed # Request was accepted, but not processed
resp.status = falcon.HTTP_202 resp.status = falcon.HTTP_202
resp.body = ". ".join(reasons) resp.body = ". ".join(reasons)
if req.client_accepts("application/json"):
resp.body = json.dumps({"title":"Accepted", "description":resp.body},
cls=MyEncoder)
class RequestDetailResource(object): class RequestDetailResource(object):

View File

@ -67,6 +67,8 @@ def serialize(func):
""" """
import falcon import falcon
def wrapped(instance, req, resp, **kwargs): def wrapped(instance, req, resp, **kwargs):
retval = func(instance, req, resp, **kwargs)
if not resp.body and not resp.location:
if not req.client_accepts("application/json"): if not req.client_accepts("application/json"):
logger.debug("Client did not accept application/json") logger.debug("Client did not accept application/json")
raise falcon.HTTPUnsupportedMediaType( raise falcon.HTTPUnsupportedMediaType(
@ -74,6 +76,6 @@ def serialize(func):
resp.set_header("Cache-Control", "no-cache, no-store, must-revalidate") resp.set_header("Cache-Control", "no-cache, no-store, must-revalidate")
resp.set_header("Pragma", "no-cache") resp.set_header("Pragma", "no-cache")
resp.set_header("Expires", "0") resp.set_header("Expires", "0")
resp.body = json.dumps(func(instance, req, resp, **kwargs), cls=MyEncoder) resp.body = json.dumps(retval, cls=MyEncoder)
return wrapped return wrapped

View File

@ -220,6 +220,27 @@ function onAttributeUpdated(e) {
}) })
} }
function onSubmitRequest() {
$.ajax({
method: "POST",
url: "/api/request/",
headers: {
"Accept": "application/json; charset=utf-8",
"Content-Type": "application/pkcs10"
},
data: $("#request_body").val(),
success:function(attributes, status, xhr) {
// Close the modal
$("[data-dismiss=modal]").trigger({ type: "click" });
},
error: function(xhr, status, e) {
console.info("Submitting request failed with:", status, e);
alert(e);
}
})
}
function onServerStarted() { function onServerStarted() {
console.info("Server started"); console.info("Server started");
location.reload(); location.reload();

View File

@ -5,8 +5,8 @@
<button type="button" class="close" data-dismiss="modal">&times;</button> <button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Request submission</h4> <h4 class="modal-title">Request submission</h4>
</div> </div>
<form action="/api/request/" method="post">
<div class="modal-body"> <div class="modal-body">
<h5>Certidude client</h5> <h5>Certidude client</h5>
<p>Submit a certificate signing request from Mac OS X, Ubuntu or Fedora:</p> <p>Submit a certificate signing request from Mac OS X, Ubuntu or Fedora:</p>
@ -14,7 +14,7 @@
<pre><code>easy_install pip; <pre><code>easy_install pip;
pip3 install certidude; pip3 install certidude;
certidude bootstrap {{session.authority.common_name}} certidude bootstrap {{session.authority.common_name}}
</code></pre> </code></pre>
</div> </div>
<h5>UNIX & UNIX-like</h5> <h5>UNIX & UNIX-like</h5>
@ -53,15 +53,15 @@ curl -f -L -H "Content-type: application/pkcs10" \
<p>Use whatever tools you have available on your platform to generate <p>Use whatever tools you have available on your platform to generate
keypair and just paste ASCII armored PEM file contents here and hit submit:</p> keypair and just paste ASCII armored PEM file contents here and hit submit:</p>
<textarea id="request_body" style="width:100%; min-height: 4em;" <textarea id="request_body" style="width:100%; min-height: 10em;" placeholder="-----BEGIN CERTIFICATE REQUEST-----"></textarea>
placeholder="-----BEGIN CERTIFICATE REQUEST-----\n...\n-----END CERTIFICATE REQUEST-----"></textarea>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<div class="btn-group"> <div class="btn-group">
<button type="button" class="btn btn-success"><i class="fa fa-upload"></i> Submit</button> <button type="button" onclick="onSubmitRequest();" class="btn btn-primary"><i class="fa fa-upload"></i> Submit</button>
<button type="button" class="btn" data-dismiss="modal"><i class="fa fa-ban"></i> Close</button> <button type="button" class="btn btn-secondary" data-dismiss="modal"><i class="fa fa-ban"></i> Close</button>
</div> </div>
</div> </div>
</form>
</div> </div>
</div> </div>
</div> </div>