Skip to content

Commit

Permalink
Merge pull request #42 from blockchain-certificates/feat/context_url_…
Browse files Browse the repository at this point in the history
…getter

Feat/context url getter
  • Loading branch information
lemoustachiste authored Apr 8, 2022
2 parents 6e9c42e + 1d10f7b commit 8b28336
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 60 deletions.
4 changes: 3 additions & 1 deletion cert_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from cert_schema.jsonld_helpers import jsonld_document_loader, normalize_jsonld
from cert_schema.schema_validator import validate_unsigned_v1_2, validate_v1_2, validate_v2, validate_v2_1,\
validate_v3
from cert_schema.context_urls import ContextUrls

# Prefer consuming from ContextUrls, maintained here for backwards compatibility, will not get updated
from cert_schema.jsonld_helpers import BLOCKCERTS_V3_CANONICAL_CONTEXT, BLOCKCERTS_V3_CONTEXT, \
VERIFIABLE_CREDENTIAL_V1_CONTEXT, BLOCKCERTS_V2_ALPHA_CONTEXT, BLOCKCERTS_V2_CONTEXT, \
VERIFIABLE_CREDENTIAL_V1_CONTEXT, BLOCKCERTS_V2_CONTEXT, \
BLOCKCERTS_V2_CANONICAL_CONTEXT, \
BLOCKCERTS_VOCAB, JSONLD_OPTIONS, OPEN_BADGES_V2_CANONICAL_CONTEXT, OPEN_BADGES_V2_CONTEXT, \
BLOCKCERTS_V2_1_CONTEXT, BLOCKCERTS_V2_1_CANONICAL_CONTEXT
Expand Down
65 changes: 65 additions & 0 deletions cert_schema/context_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import json
BASE_DIR = os.path.abspath(os.path.dirname(__file__))

class ContextUrls:
def __init__(self):
with open(os.path.join(BASE_DIR, 'context_urls.json')) as context_data:
self.CONTEXT_URLS = json.load(context_data)

def v2(self):
return self.CONTEXT_URLS['BLOCKCERTS_V2_CONTEXT']

def v2_canonical(self):
return self.CONTEXT_URLS['BLOCKCERTS_V2_CANONICAL_CONTEXT']

def v2_blockcerts_org(self):
return self.CONTEXT_URLS['BLOCKCERTS_ORG_V2_CONTEXT']

def v2_all(self):
return [
self.v2(),
self.v2_canonical(),
self.v2_blockcerts_org()
]

def v2_1(self):
return self.CONTEXT_URLS['BLOCKCERTS_V2_1_CONTEXT']

def v2_1_canonical(self):
return self.CONTEXT_URLS['BLOCKCERTS_V2_1_CANONICAL_CONTEXT']

def v2_1_blockcerts_org(self):
return self.CONTEXT_URLS['BLOCKCERTS_ORG_V2_1_CONTEXT']

def v2_1_all(self):
return [
self.v2_1(),
self.v2_1_canonical(),
self.v2_1_blockcerts_org()
]

def v3(self):
return self.CONTEXT_URLS['BLOCKCERTS_V3_CONTEXT']

def v3_canonical(self):
return self.CONTEXT_URLS['BLOCKCERTS_V3_CANONICAL_CONTEXT']

def v3_blockcerts_org(self):
return self.CONTEXT_URLS['BLOCKCERTS_ORG_V3_CONTEXT']

def v3_all(self):
return [
self.v3(),
self.v3_canonical(),
self.v3_blockcerts_org()
]

def open_badge(self):
return self.CONTEXT_URLS['OPEN_BADGES_V2_CONTEXT']

def open_badge_canonical(self):
return self.CONTEXT_URLS['OPEN_BADGES_V2_CANONICAL_CONTEXT']

def verifiable_credential(self):
return self.CONTEXT_URLS['VERIFIABLE_CREDENTIAL_V1_CONTEXT']
114 changes: 57 additions & 57 deletions cert_schema/jsonld_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@
import validators

from cert_schema.errors import *
from cert_schema.context_urls import ContextUrls

try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen

OPEN_BADGES_V2_CONTEXT = 'https://openbadgespec.org/v2/context.json'
OPEN_BADGES_V2_CANONICAL_CONTEXT = 'https://w3id.org/openbadges/v2'
ContextUrlsInstance = ContextUrls()

VERIFIABLE_CREDENTIAL_V1_CONTEXT = 'https://www.w3.org/2018/credentials/v1'
OPEN_BADGES_V2_CONTEXT = ContextUrlsInstance.open_badge()
OPEN_BADGES_V2_CANONICAL_CONTEXT = ContextUrlsInstance.open_badge_canonical()

VERIFIABLE_CREDENTIAL_V1_CONTEXT = ContextUrlsInstance.verifiable_credential()

BLOCKCERTS_V2_ALPHA_CONTEXT = 'https://w3id.org/blockcerts/schema/2.0-alpha/context.json'
BLOCKCERTS_ORG_V2_ALPHA_CONTEXT = 'https://www.blockcerts.org/schema/2.0-alpha/context.json'

BLOCKCERTS_V2_CONTEXT = 'https://w3id.org/blockcerts/schema/2.0/context.json'
BLOCKCERTS_ORG_V2_CONTEXT = 'https://www.blockcerts.org/schema/2.0/context.json'
BLOCKCERTS_V2_CANONICAL_CONTEXT = 'https://w3id.org/blockcerts/v2'
BLOCKCERTS_V2_CONTEXT = ContextUrlsInstance.v2()
BLOCKCERTS_ORG_V2_CONTEXT = ContextUrlsInstance.v2_blockcerts_org()
BLOCKCERTS_V2_CANONICAL_CONTEXT = ContextUrlsInstance.v2_canonical()

BLOCKCERTS_V2_1_CONTEXT = 'https://w3id.org/blockcerts/schema/2.1/context.json'
BLOCKCERTS_ORG_V2_1_CONTEXT = 'https://www.blockcerts.org/schema/2.1/context.json'
BLOCKCERTS_V2_1_CANONICAL_CONTEXT = 'https://w3id.org/blockcerts/v2.1'
BLOCKCERTS_V2_1_CONTEXT = ContextUrlsInstance.v2_1()
BLOCKCERTS_ORG_V2_1_CONTEXT = ContextUrlsInstance.v2_1_blockcerts_org()
BLOCKCERTS_V2_1_CANONICAL_CONTEXT = ContextUrlsInstance.v2_1_canonical()

BLOCKCERTS_V3_ALPHA_CONTEXT = 'https://w3id.org/blockcerts/schema/3.0-alpha/context.json'
BLOCKCERTS_V3_ORG_ALPHA_CONTEXT = 'https://www.blockcerts.org/schema/3.0-alpha/context.json'
Expand All @@ -40,9 +43,9 @@
BLOCKCERTS_V3_ORG_BETA_CONTEXT = 'https://www.blockcerts.org/schema/3.0-beta/context.json'
BLOCKCERTS_V3_BETA_CANONICAL_CONTEXT = 'https://w3id.org/blockcerts/v3.0-beta'

BLOCKCERTS_V3_CONTEXT = 'https://w3id.org/blockcerts/schema/3.0/context.json'
BLOCKCERTS__ORG_V3_CONTEXT = 'https://www.blockcerts.org/schema/3.0/context.json'
BLOCKCERTS_V3_CANONICAL_CONTEXT = 'https://w3id.org/blockcerts/v3'
BLOCKCERTS_V3_CONTEXT = ContextUrlsInstance.v3()
BLOCKCERTS__ORG_V3_CONTEXT = ContextUrlsInstance.v3_blockcerts_org()
BLOCKCERTS_V3_CANONICAL_CONTEXT = ContextUrlsInstance.v3_canonical()

BLOCKCERTS_VOCAB = 'https://w3id.org/blockcerts/3.0#'

Expand Down Expand Up @@ -71,52 +74,49 @@

PRELOADED_CONTEXTS = {}

with open(os.path.join(BASE_DIR, 'context_urls.json')) as context_data:
CONTEXT_URLS = json.load(context_data)

with open(OBI_JSON_LD_CONTEXT_V2) as data_file:
with open(OBI_JSON_LD_CONTEXT_V2) as data_file:
obi_context = json.load(data_file)
PRELOADED_CONTEXTS[CONTEXT_URLS['OPEN_BADGES_V2_CONTEXT']] = obi_context
PRELOADED_CONTEXTS[CONTEXT_URLS['OPEN_BADGES_V2_CANONICAL_CONTEXT']] = obi_context

with open(VERIFIABLE_CREDENTIAL_JSON_LD_CONTEXT_V1) as data_file:
cred_context = json.load(data_file)
PRELOADED_CONTEXTS[CONTEXT_URLS['VERIFIABLE_CREDENTIAL_V1_CONTEXT']] = cred_context

with open(JSON_LD_CONTEXT_V2_0_ALPHA) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[BLOCKCERTS_V2_ALPHA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_ORG_V2_ALPHA_CONTEXT] = bc_context

with open(JSON_LD_CONTEXT_V2_0) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[CONTEXT_URLS['BLOCKCERTS_V2_CONTEXT']] = bc_context
PRELOADED_CONTEXTS[CONTEXT_URLS['BLOCKCERTS_ORG_V2_CONTEXT']] = bc_context
PRELOADED_CONTEXTS[CONTEXT_URLS['BLOCKCERTS_V2_CANONICAL_CONTEXT']] = bc_context

with open(JSON_LD_CONTEXT_V2_1) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[CONTEXT_URLS['BLOCKCERTS_V2_1_CONTEXT']] = bc_context
PRELOADED_CONTEXTS[CONTEXT_URLS['BLOCKCERTS_ORG_V2_1_CONTEXT']] = bc_context
PRELOADED_CONTEXTS[CONTEXT_URLS['BLOCKCERTS_V2_1_CANONICAL_CONTEXT']] = bc_context

with open(JSON_LD_CONTEXT_V3_0_ALPHA) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[BLOCKCERTS_V3_ALPHA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_V3_ORG_ALPHA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_V3_ALPHA_CANONICAL_CONTEXT] = bc_context

with open(JSON_LD_CONTEXT_V3_0_BETA) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[BLOCKCERTS_V3_BETA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_V3_ORG_BETA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_V3_BETA_CANONICAL_CONTEXT] = bc_context

with open(JSON_LD_CONTEXT_V3_0) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[CONTEXT_URLS['BLOCKCERTS_V3_CONTEXT']] = bc_context
PRELOADED_CONTEXTS[CONTEXT_URLS['BLOCKCERTS_ORG_V3_CONTEXT']] = bc_context
PRELOADED_CONTEXTS[CONTEXT_URLS['BLOCKCERTS_V3_CANONICAL_CONTEXT']] = bc_context
PRELOADED_CONTEXTS[ContextUrlsInstance.open_badge()] = obi_context
PRELOADED_CONTEXTS[ContextUrlsInstance.open_badge_canonical()] = obi_context

with open(VERIFIABLE_CREDENTIAL_JSON_LD_CONTEXT_V1) as data_file:
cred_context = json.load(data_file)
PRELOADED_CONTEXTS[ContextUrlsInstance.verifiable_credential()] = cred_context

with open(JSON_LD_CONTEXT_V2_0_ALPHA) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[BLOCKCERTS_V2_ALPHA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_ORG_V2_ALPHA_CONTEXT] = bc_context

with open(JSON_LD_CONTEXT_V2_0) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[ContextUrlsInstance.v2()] = bc_context
PRELOADED_CONTEXTS[ContextUrlsInstance.v2_blockcerts_org()] = bc_context
PRELOADED_CONTEXTS[ContextUrlsInstance.v2_canonical()] = bc_context

with open(JSON_LD_CONTEXT_V2_1) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[ContextUrlsInstance.v2_1()] = bc_context
PRELOADED_CONTEXTS[ContextUrlsInstance.v2_1_blockcerts_org()] = bc_context
PRELOADED_CONTEXTS[ContextUrlsInstance.v2_1_canonical()] = bc_context

with open(JSON_LD_CONTEXT_V3_0_ALPHA) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[BLOCKCERTS_V3_ALPHA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_V3_ORG_ALPHA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_V3_ALPHA_CANONICAL_CONTEXT] = bc_context

with open(JSON_LD_CONTEXT_V3_0_BETA) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[BLOCKCERTS_V3_BETA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_V3_ORG_BETA_CONTEXT] = bc_context
PRELOADED_CONTEXTS[BLOCKCERTS_V3_BETA_CANONICAL_CONTEXT] = bc_context

with open(JSON_LD_CONTEXT_V3_0) as data_file:
bc_context = json.load(data_file)
PRELOADED_CONTEXTS[ContextUrlsInstance.v3()] = bc_context
PRELOADED_CONTEXTS[ContextUrlsInstance.v3_canonical()] = bc_context
PRELOADED_CONTEXTS[ContextUrlsInstance.v3_blockcerts_org()] = bc_context

def to_loader_response(data, url):
return {
Expand Down
62 changes: 61 additions & 1 deletion examples/2.1/tampered_unmapped_fields.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name='cert-schema',
version='3.0.7',
version='3.1.0',
description='Blockchain certificates JSON-LD context and JSON schemas',
author='[email protected]',
url='https://github.com/blockchain-certificates/cert-schema',
Expand Down
80 changes: 80 additions & 0 deletions tests/test_context_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import unittest

from cert_schema import ContextUrls

class TestContextUrls(unittest.TestCase):
@classmethod
def setUpClass(self):
self.instance = ContextUrls()

def test_v2(self):
output = self.instance.v2()
self.assertTrue(output == 'https://w3id.org/blockcerts/schema/2.0/context.json')

def test_v2_canonical(self):
output = self.instance.v2_canonical()
self.assertTrue(output == 'https://w3id.org/blockcerts/v2')

def test_v2_blockcerts_org(self):
output = self.instance.v2_blockcerts_org()
self.assertTrue(output == 'https://www.blockcerts.org/schema/2.0/context.json')

def test_v2_all(self):
output = self.instance.v2_all()
self.assertEqual(output, [
'https://w3id.org/blockcerts/schema/2.0/context.json',
'https://w3id.org/blockcerts/v2',
'https://www.blockcerts.org/schema/2.0/context.json'
])

def test_v2_1(self):
output = self.instance.v2_1()
self.assertTrue(output == 'https://w3id.org/blockcerts/schema/2.1/context.json')

def test_v2_1_canonical(self):
output = self.instance.v2_1_canonical()
self.assertTrue(output == 'https://w3id.org/blockcerts/v2.1')

def test_v2_1_blockcerts_org(self):
output = self.instance.v2_1_blockcerts_org()
self.assertTrue(output == 'https://www.blockcerts.org/schema/2.1/context.json')

def test_v2_1_all(self):
output = self.instance.v2_1_all()
self.assertEqual(output, [
'https://w3id.org/blockcerts/schema/2.1/context.json',
'https://w3id.org/blockcerts/v2.1',
'https://www.blockcerts.org/schema/2.1/context.json'
])

def test_v3(self):
output = self.instance.v3()
self.assertTrue(output == 'https://w3id.org/blockcerts/schema/3.0/context.json')

def test_v3_canonical(self):
output = self.instance.v3_canonical()
self.assertTrue(output == 'https://w3id.org/blockcerts/v3')

def test_v3_blockcerts_org(self):
output = self.instance.v3_blockcerts_org()
self.assertTrue(output == 'https://www.blockcerts.org/schema/3.0/context.json')

def test_v3_all(self):
output = self.instance.v3_all()
self.assertEqual(output, [
'https://w3id.org/blockcerts/schema/3.0/context.json',
'https://w3id.org/blockcerts/v3',
'https://www.blockcerts.org/schema/3.0/context.json'
])

def test_open_badge(self):
output = self.instance.open_badge()
self.assertTrue(output == 'https://openbadgespec.org/v2/context.json')

def test_open_badge_canonical(self):
output = self.instance.open_badge_canonical()
self.assertTrue(output == 'https://w3id.org/openbadges/v2')

def test_verifiable_credentials(self):
output = self.instance.verifiable_credential()
self.assertTrue(output == 'https://www.w3.org/2018/credentials/v1')

0 comments on commit 8b28336

Please sign in to comment.