def get_certificate(self, certificate_uid): """ Returns certificate as byte array. We need this for v1 certs, which compute a binary hash. Raises KeyError if not found :param certificate_uid: :return: """ logging.debug('Retrieving certificate for uid=%s', certificate_uid) is_v1_uid = model.is_v1_uid(certificate_uid) if not is_v1_uid: return super(V1AwareCertificateStore, self).get_certificate(certificate_uid) # else it's V1.1 (if not valid, it will throw) certificate = self._find_certificate_metadata(uid=certificate_uid) if certificate: certificate_bytes = self._get_certificate_raw(certificate_uid) certificate_json = helpers.certificate_bytes_to_json( certificate_bytes) return model.to_certificate_model(certificate_json, certificate['txid'], certificate_bytes) message = 'Certificate metadata not found for certificate uid=%s' % certificate_uid logging.error(message) raise KeyError(message)
def test_to_certificate_model_v2(self): with open('data/2.0/bbba8553-8ec1-445f-82c9-a57251dd731c.json', 'rb') as cert_file: certificate_bytes = cert_file.read() certificate_json = helpers.certificate_bytes_to_json( certificate_bytes) certificate_model = model.to_certificate_model(certificate_json) self.assertEqual(certificate_model.version, BlockcertVersion.V2) self.assertEqual( certificate_model.issuer.id, 'https://www.blockcerts.org/samples/2.0/issuer-testnet.json') self.assertEqual( certificate_model.txid, 'd75b7a5bdb3d5244b753e6b84e987267cfa4ffa7a532a2ed49ad3848be1d82f8' ) self.assertEqual(certificate_model.title, 'Certificate of Accomplishment') self.assertEqual( certificate_model.description, 'Lorem ipsum dolor sit amet, mei docendi concludaturque ad, cu nec partem graece. Est aperiam consetetur cu, expetenda moderatius neglegentur ei nam, suas dolor laudem eam an.' ) self.assertEqual(certificate_model.uid, 'urn:uuid:bbba8553-8ec1-445f-82c9-a57251dd731c') self.assertEqual(certificate_model.subtitle, None) self.assertEqual(certificate_model.expires, None) self.assertEqual(certificate_model.issuer.name, 'University of Learning') self.assertEqual(certificate_model.recipient_public_key, 'mtr98kany9G1XYNU74pRnfBQmaCg2FZLmc') self.assertEqual(certificate_model.issued_on, parse_date('2017-06-29T14:58:57.461422+00:00')) self.assertEqual(certificate_model.chain, Chain.bitcoin_testnet)
def test_to_certificate_model_v2_alpha(self): with open('data/2.0-alpha/8e0b8a28-beff-43de-a72c-820bc360db3d.json', 'rb') as cert_file: certificate_bytes = cert_file.read() certificate_json = helpers.certificate_bytes_to_json( certificate_bytes) certificate_model = model.to_certificate_model(certificate_json) self.assertEqual(certificate_model.version, BlockcertVersion.V2_ALPHA) self.assertEqual( certificate_model.issuer.id, 'https://www.blockcerts.org/blockcerts_v2_alpha/samples/issuer_testnet.json' ) self.assertEqual( certificate_model.txid, '08e205566662b97f149ad677649bbb94ebc2f46c0ac72bc7c9b57d2d207015f4' ) self.assertEqual(certificate_model.title, 'This is the certificate title') self.assertEqual( certificate_model.description, 'This is the display description of the certificate.') self.assertEqual(certificate_model.uid, 'urn:uuid:8e0b8a28-beff-43de-a72c-820bc360db3d') self.assertEqual(certificate_model.subtitle, None) self.assertEqual(certificate_model.expires, None) self.assertEqual(certificate_model.issuer.name, 'Issuer Institution Name') self.assertEqual(certificate_model.recipient_public_key, 'mkwntSiQmc14H65YxwckLenxY3DsEpvFbe') self.assertEqual(certificate_model.issued_on, parse_date('2017-05-01 00:00:00+00:00')) self.assertEqual(certificate_model.chain, Chain.bitcoin_testnet)
def test_detect_version_v1_1(self): with open('data/1.1/sample_signed_cert-1.1.json', 'rb') as cert_file: certificate_bytes = cert_file.read() certificate_json = helpers.certificate_bytes_to_json( certificate_bytes) version = model.detect_version(certificate_json) self.assertEqual(version, BlockcertVersion.V1_1)
def test_detect_version_v1_2(self): with open('data/1.2/609c2989-275f-4f4c-ab02-b245cfb09017.json', 'rb') as cert_file: certificate_bytes = cert_file.read() certificate_json = helpers.certificate_bytes_to_json( certificate_bytes) version = model.detect_version(certificate_json) self.assertEqual(version, BlockcertVersion.V1_2)
def get_certificate_json(self, certificate_uid): """ Returns certificate as json. Propagates KeyError if key isn't found :param certificate_uid: :return: """ if certificate_uid.startswith(URN_UUID_PREFIX): uid = certificate_uid[len(URN_UUID_PREFIX):] elif certificate_uid.startswith('http'): last_slash = certificate_uid.rindex('/') uid = certificate_uid[last_slash + 1:] else: uid = certificate_uid logging.debug('Retrieving certificate for uid=%s', uid) certificate_bytes = self._get_certificate_raw(uid) logging.debug('Found certificate for uid=%s', uid) certificate_json = helpers.certificate_bytes_to_json(certificate_bytes) return certificate_json
def test_to_certificate_model_v1_1(self): with open('data/1.1/sample_signed_cert-1.1.json', 'rb') as cert_file: certificate_bytes = cert_file.read() certificate_json = helpers.certificate_bytes_to_json( certificate_bytes) txid = '1703d2f5d706d495c1c65b40a086991ab755cc0a02bef51cd4aff9ed7a8586aa' certificate_model = model.to_certificate_model( certificate_json, txid, certificate_bytes) self.assertEqual( certificate_model.issuer.id, 'http://www.blockcerts.org/mockissuer/issuer/got-issuer.json') self.assertEqual( certificate_model.txid, '1703d2f5d706d495c1c65b40a086991ab755cc0a02bef51cd4aff9ed7a8586aa' ) self.assertEqual(certificate_model.title, 'Game of Thrones Character') self.assertEqual( certificate_model.description, 'This certifies that the named character is an official Game of Thrones character.' ) self.assertEqual(certificate_model.uid, '75857d18-0e1c-4933-b4c8-33484396e06b') self.assertEqual(certificate_model.subtitle, None) self.assertEqual(certificate_model.expires, None) self.assertEqual(certificate_model.issuer.name, 'Game of thrones characters') self.assertEqual(certificate_model.recipient_public_key, 'mgCNaPM3TFhh8Yn6U6VcEM9jWLhQbizy1x') self.assertEqual(certificate_model.issued_on, parse_date('2016-08-29 00:00:00+00:00')) # self.assertEqual(certificate_model.blockcert_signature, None) self.assertIsNotNone(certificate_model.signature_image[0].image) self.assertEqual(certificate_model.signatures[0].signature_type, SignatureType.signed_content) self.assertEqual(certificate_model.signatures[1].signature_type, SignatureType.signed_transaction) self.assertIsNone(certificate_model.signatures[1].merkle_proof) self.assertEqual(certificate_model.chain, Chain.bitcoin_testnet)
def test_to_certificate_model_v1_2(self): """ Note this is a bitcoin_mainnet certificate with different uid :return: """ with open('data/1.2/609c2989-275f-4f4c-ab02-b245cfb09017.json', 'rb') as cert_file: certificate_bytes = cert_file.read() certificate_json = helpers.certificate_bytes_to_json( certificate_bytes) certificate_model = model.to_certificate_model(certificate_json) self.assertEqual(certificate_model.version, BlockcertVersion.V1_2) self.assertEqual( certificate_model.issuer.id, 'http://www.blockcerts.org/mockissuer/issuer/got-issuer_live.json' ) self.assertEqual( certificate_model.txid, '8623beadbc7877a9e20fb7f83eda6c1a1fc350171f0714ff6c6c4054018eb54d' ) self.assertEqual(certificate_model.title, 'Game of Thrones Character') self.assertEqual( certificate_model.description, 'This certifies that the named character is an official Game of Thrones character.' ) self.assertEqual(certificate_model.uid, '609c2989-275f-4f4c-ab02-b245cfb09017') self.assertEqual(certificate_model.subtitle, None) self.assertEqual(certificate_model.expires, None) self.assertEqual(certificate_model.issuer.name, 'Game of thrones issuer') self.assertEqual(certificate_model.recipient_public_key, '1AAGG6jirbu9XwikFpkHokbbiYpjVtFe1G') self.assertEqual(certificate_model.issued_on, parse_date('2016-10-03 00:00:00+00:00')) self.assertIsNotNone(certificate_model.signature_image[0].image) self.assertEqual(certificate_model.chain, Chain.bitcoin_mainnet)