Skip to content

Commit

Permalink
Merge pull request #218 from blockchain-certificates/feat/check-conte…
Browse files Browse the repository at this point in the history
…xt-order

feat(V3): provide check to make sure blockcerts context is last
  • Loading branch information
lemoustachiste committed Jan 26, 2022
2 parents eeb2c1d + 18e5396 commit d267c76
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 46 deletions.
12 changes: 11 additions & 1 deletion cert_issuer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def is_valid_url (url):
and url.__contains__(':'))

def validate_url (url):
if not is_valid_url (parsed_url):
if not is_valid_url (url):
raise ValueError('Invalid URL: {}'.format(url))
pass

Expand All @@ -33,13 +33,23 @@ def validate_type (certificate_type):

def validate_context (context, type):
vc_context_url = 'https://www.w3.org/2018/credentials/v1'
blockcerts_valid_context_url = [
'https://www.blockcerts.org/schema/3.0/context.json',
'https://blockcerts.org/schema/3.0/context.json',
'https://www.w3id.org/blockcerts/schema/3.0/context.json',
'https://w3id.org/blockcerts/schema/3.0/context.json',
'https://www.w3id.org/blockcerts/v3',
'https://w3id.org/blockcerts/v3'
]

if not isinstance(context, list):
raise ValueError('`@context` property must be an array')
if context[0] != vc_context_url:
raise ValueError('First @context declared must be {}, was given {}'.format(vc_context_url, context[0]))
if len(type) > 1 and len(context) == 1:
raise ValueError('A more specific type: {}, was detected, yet no context seems provided for that type'.format(type[1]))
if context[-1] not in blockcerts_valid_context_url:
raise ValueError('Last @context declared must be one of valid blockcerts context url, preferably {}, was given {}'.format(blockcerts_valid_context_url[-1]), context[-1])

pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
credential_example = {
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.blockcerts.org/schema/3.0-alpha/context.json",
"https://www.w3.org/2018/credentials/examples/v1"
"https://www.w3.org/2018/credentials/examples/v1",
"https://www.blockcerts.org/schema/3.0/context.json"
],
"id": "urn:uuid:bbba8553-8ec1-445f-82c9-a57251dd731c",
"type": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

presentation_example = {
"@context": [
"https://www.w3.org/2018/credentials/v1"
"https://www.w3.org/2018/credentials/v1",
"https://www.blockcerts.org/schema/3.0/context.json"
],
"type": [
"VerifiablePresentation"
Expand All @@ -16,26 +17,8 @@
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.blockcerts.org/schema/3.0-alpha/context.json",
"https://www.w3.org/2018/credentials/examples/v1",
{
"metadataJson": {
"@id": "https://schemas.learningmachine.com/2017/blockcerts/metadata",
"@type": "https://schemas.learningmachine.com/2017/types/text/json"
},
"displayHtml": {
"@id": "https://schemas.learningmachine.com/2017/blockcerts/displayHtml",
"@type": "https://schemas.learningmachine.com/2017/types/text/html"
},
"nonce": {
"@id": "https://schemas.learningmachine.com/2017/blockcerts/nonce",
"@type": "https://schema.org/Text"
},
"universalIdentifier": {
"@id": "https://schemas.learningmachine.com/2017/blockcerts/identifier",
"@type": "https://schema.org/Text"
}
}
"https://www.blockcerts.org/schema/3.0/context.json"
],
"id": "urn:uuid:bbba8553-8ec1-445f-82c9-a57251dd731c",
"metadataJson": "{\"schema\":{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"type\":\"object\",\"properties\":{\"displayOrder\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},\"certificate\":{\"order\":[],\"type\":\"object\",\"properties\":{\"issuingInstitution\":{\"title\":\"Issuing Institution\",\"type\":\"string\",\"default\":\"Learning Machine Technologies, Inc.\"}}},\"recipient\":{}}},\"certificate\":{\"issuingInstitution\":\"Learning Machine Technologies, Inc.\"},\"recipient\":{},\"displayOrder\":[\"certificate.issuingInstitution\"]}",
Expand Down
70 changes: 68 additions & 2 deletions tests/v3_certificate_validation/test_unit_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,74 @@ def test_validate_context_invalid_missing_context (self):

assert False

def test_validate_context_valid (self):
candidate_context_url = ['https://www.w3.org/2018/credentials/v1', 'https://www.w3id.org/blockcerts/v3.0-alpha']
def test_validate_context_invalid_last_context (self):
candidate_context_url = ['https://www.w3.org/2018/credentials/v1', 'https://www.w3id.org/blockcerts/v3', 'link.to.another.context']
candidate_type = ['VerifiableCredential', 'BlockcertsCredential']
try:
validate_context(candidate_context_url, candidate_type)
except:
assert True
return

assert False

def test_validate_context_valid_wwww3idcanon (self):
candidate_context_url = ['https://www.w3.org/2018/credentials/v1', 'https://www.w3id.org/blockcerts/v3']
candidate_type = ['VerifiableCredential', 'BlockcertsCredential']
try:
validate_context(candidate_context_url, candidate_type)
except:
assert False
return

assert True

def test_validate_context_valid_w3idcanon (self):
candidate_context_url = ['https://www.w3.org/2018/credentials/v1', 'https://w3id.org/blockcerts/v3']
candidate_type = ['VerifiableCredential', 'BlockcertsCredential']
try:
validate_context(candidate_context_url, candidate_type)
except:
assert False
return

assert True

def test_validate_context_valid_wwwblockcerts (self):
candidate_context_url = ['https://www.w3.org/2018/credentials/v1', 'https://www.blockcerts.org/schema/3.0/context.json']
candidate_type = ['VerifiableCredential', 'BlockcertsCredential']
try:
validate_context(candidate_context_url, candidate_type)
except:
assert False
return

assert True

def test_validate_context_valid_blockcerts (self):
candidate_context_url = ['https://www.w3.org/2018/credentials/v1', 'https://blockcerts.org/schema/3.0/context.json']
candidate_type = ['VerifiableCredential', 'BlockcertsCredential']
try:
validate_context(candidate_context_url, candidate_type)
except:
assert False
return

assert True

def test_validate_context_valid_wwww3id (self):
candidate_context_url = ['https://www.w3.org/2018/credentials/v1', 'https://www.w3id.org/blockcerts/schema/3.0/context.json']
candidate_type = ['VerifiableCredential', 'BlockcertsCredential']
try:
validate_context(candidate_context_url, candidate_type)
except:
assert False
return

assert True

def test_validate_context_valid_w3id (self):
candidate_context_url = ['https://www.w3.org/2018/credentials/v1', 'https://w3id.org/blockcerts/schema/3.0/context.json']
candidate_type = ['VerifiableCredential', 'BlockcertsCredential']
try:
validate_context(candidate_context_url, candidate_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
credential_example = {
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.blockcerts.org/schema/3.0-alpha/context.json",
"https://www.w3.org/2018/credentials/examples/v1"
"https://www.blockcerts.org/schema/3.0/context.json",
],
"id": "urn:uuid:bbba8553-8ec1-445f-82c9-a57251dd731c",
"type": [
Expand Down
23 changes: 3 additions & 20 deletions tests/v3_certificate_validation/test_unit_verify_presentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

presentation_example = {
"@context": [
"https://www.w3.org/2018/credentials/v1"
"https://www.w3.org/2018/credentials/v1",
"https://www.blockcerts.org/schema/3.0/context.json"
],
"type": [
"VerifiablePresentation"
Expand All @@ -14,26 +15,8 @@
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.blockcerts.org/schema/3.0-alpha/context.json",
"https://www.w3.org/2018/credentials/examples/v1",
{
"metadataJson": {
"@id": "https://schemas.learningmachine.com/2017/blockcerts/metadata",
"@type": "https://schemas.learningmachine.com/2017/types/text/json"
},
"displayHtml": {
"@id": "https://schemas.learningmachine.com/2017/blockcerts/displayHtml",
"@type": "https://schemas.learningmachine.com/2017/types/text/html"
},
"nonce": {
"@id": "https://schemas.learningmachine.com/2017/blockcerts/nonce",
"@type": "https://schema.org/Text"
},
"universalIdentifier": {
"@id": "https://schemas.learningmachine.com/2017/blockcerts/identifier",
"@type": "https://schema.org/Text"
}
}
"https://www.blockcerts.org/schema/3.0/context.json"
],
"id": "urn:uuid:bbba8553-8ec1-445f-82c9-a57251dd731c",
"metadataJson": "{\"schema\":{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"type\":\"object\",\"properties\":{\"displayOrder\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},\"certificate\":{\"order\":[],\"type\":\"object\",\"properties\":{\"issuingInstitution\":{\"title\":\"Issuing Institution\",\"type\":\"string\",\"default\":\"Learning Machine Technologies, Inc.\"}}},\"recipient\":{}}},\"certificate\":{\"issuingInstitution\":\"Learning Machine Technologies, Inc.\"},\"recipient\":{},\"displayOrder\":[\"certificate.issuingInstitution\"]}",
Expand Down

0 comments on commit d267c76

Please sign in to comment.