2015-12-12 22:34:08 +00:00
|
|
|
|
|
|
|
import falcon
|
2015-12-16 17:41:49 +00:00
|
|
|
import logging
|
2017-03-13 11:42:58 +00:00
|
|
|
import json
|
|
|
|
import hashlib
|
2015-12-12 22:34:08 +00:00
|
|
|
from certidude import authority
|
|
|
|
from certidude.auth import login_required, authorize_admin
|
2017-03-13 11:42:58 +00:00
|
|
|
from certidude.decorators import csrf_protection
|
2015-12-12 22:34:08 +00:00
|
|
|
|
2017-04-04 05:02:08 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-12-16 17:41:49 +00:00
|
|
|
|
2015-12-12 22:34:08 +00:00
|
|
|
class SignedCertificateDetailResource(object):
|
|
|
|
def on_get(self, req, resp, cn):
|
2017-03-13 11:42:58 +00:00
|
|
|
|
|
|
|
preferred_type = req.client_prefers(("application/json", "application/x-pem-file"))
|
2015-12-12 22:34:08 +00:00
|
|
|
try:
|
2017-03-13 11:42:58 +00:00
|
|
|
path, buf, cert = authority.get_signed(cn)
|
2016-03-21 21:42:39 +00:00
|
|
|
except EnvironmentError:
|
2016-03-27 20:38:14 +00:00
|
|
|
logger.warning(u"Failed to serve non-existant certificate %s to %s",
|
2016-03-21 21:42:39 +00:00
|
|
|
cn, req.context.get("remote_addr"))
|
2017-04-25 10:58:21 +00:00
|
|
|
raise falcon.HTTPNotFound()
|
2017-04-25 13:15:39 +00:00
|
|
|
|
|
|
|
if preferred_type == "application/x-pem-file":
|
|
|
|
resp.set_header("Content-Type", "application/x-pem-file")
|
|
|
|
resp.set_header("Content-Disposition", ("attachment; filename=%s.pem" % cn))
|
|
|
|
resp.body = buf
|
|
|
|
logger.debug(u"Served certificate %s to %s as application/x-pem-file",
|
|
|
|
cn, req.context.get("remote_addr"))
|
|
|
|
elif preferred_type == "application/json":
|
|
|
|
resp.set_header("Content-Type", "application/json")
|
|
|
|
resp.set_header("Content-Disposition", ("attachment; filename=%s.json" % cn))
|
|
|
|
resp.body = json.dumps(dict(
|
|
|
|
common_name = cn,
|
2017-08-16 20:25:16 +00:00
|
|
|
serial_number = "%x" % cert.serial_number,
|
|
|
|
signed = cert["tbs_certificate"]["validity"]["not_before"].native.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z",
|
|
|
|
expires = cert["tbs_certificate"]["validity"]["not_after"].native.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z",
|
2017-04-25 13:15:39 +00:00
|
|
|
sha256sum = hashlib.sha256(buf).hexdigest()))
|
|
|
|
logger.debug(u"Served certificate %s to %s as application/json",
|
|
|
|
cn, req.context.get("remote_addr"))
|
2016-03-21 21:42:39 +00:00
|
|
|
else:
|
2017-07-08 08:56:01 +00:00
|
|
|
logger.debug(u"Client did not accept application/json or application/x-pem-file")
|
2017-04-25 13:15:39 +00:00
|
|
|
raise falcon.HTTPUnsupportedMediaType(
|
|
|
|
"Client did not accept application/json or application/x-pem-file")
|
2015-12-12 22:34:08 +00:00
|
|
|
|
2016-03-27 20:38:14 +00:00
|
|
|
@csrf_protection
|
2015-12-12 22:34:08 +00:00
|
|
|
@login_required
|
|
|
|
@authorize_admin
|
|
|
|
def on_delete(self, req, resp, cn):
|
2016-03-27 20:38:14 +00:00
|
|
|
logger.info(u"Revoked certificate %s by %s from %s",
|
2016-03-21 21:42:39 +00:00
|
|
|
cn, req.context.get("user"), req.context.get("remote_addr"))
|
2017-03-13 11:42:58 +00:00
|
|
|
authority.revoke(cn)
|
2015-12-12 22:34:08 +00:00
|
|
|
|