Skip to content

Commit

Permalink
Merge pull request #9 from OpSecId/add-basic-refresh-service
Browse files Browse the repository at this point in the history
Add basic refresh service
  • Loading branch information
PatStLouis authored Nov 28, 2024
2 parents a04b0cb + 17c3169 commit 304a929
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
3 changes: 3 additions & 0 deletions backend/app/models/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class BaseModel(BaseModel, extra="allow"):
description: SkipJsonSchema[
Union[str, DescriptionField, List[DescriptionField]]
] = Field(None)

class Config:
populate_by_name = True

def model_dump(self, **kwargs) -> Dict[str, Any]:
return super().model_dump(by_alias=True, exclude_none=True, **kwargs)
Expand Down
17 changes: 9 additions & 8 deletions backend/app/plugins/registrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ async def format_credential(self, credential_input, options):
jsonpath_expr.update(credential, value)

# Refresh Service
# credential["refreshService"] = [
# {
# 'type': 'SupercessionRefresh',
# 'id': f'https://{settings.DOMAIN}/credentials/refresh?type={credential_type}&entity={entity_id}&cardinality={cardinality_id}'
# }
# ]
credential["refreshService"] = [
{
'type': 'SimpleRefreshQuery',
'id': f'https://{settings.DOMAIN}/credentials/refresh?type={credential_type}&entity={entity_id}&cardinality={cardinality_id}'
}
]

# Credential Status
status_list_id = credential_registration["status_lists"][-1]
Expand Down Expand Up @@ -302,7 +302,7 @@ async def format_credential(self, credential_input, options):
pass

credential = Credential(
# context=credential.get('@context'),
context=credential_template.get('@context'),
type=credential.get('type'),
id=credential.get('id'),
name=credential.get('name'),
Expand All @@ -311,8 +311,9 @@ async def format_credential(self, credential_input, options):
validUntil=credential.get('validUntil') or None,
credentialSubject=credential.get('credentialSubject'),
credentialStatus=credential.get('credentialStatus'),
refreshService=credential.get('refreshService'),
renderMethod=credential_template.get('renderMethod'),
).model_dump()
credential['@context'] = credential_template.get('@context')

return credential

Expand Down
9 changes: 4 additions & 5 deletions backend/app/routers/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
from app.plugins.orgbook import OrgbookPublisher
from app.plugins import (
TractionController,
OCAProcessor,
PublisherRegistrar,
)
from app.security import JWTBearer
import uuid
import json
import segno
import copy

router = APIRouter(prefix="/credentials")

Expand Down Expand Up @@ -60,13 +59,13 @@ async def publish_credential(request_body: Publication):

# Check cardinality, returns hash if new issuance is required
cardinality_hash = await PublisherRegistrar().check_cardinality(
credential_input=credential_input.copy(), options=options
credential_input=copy.deepcopy(credential_input), options=options
)

if cardinality_hash:
# Format credential
credential = await PublisherRegistrar().format_credential(
credential_input=credential_input, options=options
credential_input=copy.deepcopy(credential_input), options=options
)

traction = TractionController()
Expand Down Expand Up @@ -118,7 +117,7 @@ async def publish_credential(request_body: Publication):



@router.get("/refresh", tags=["Client"])
@router.get("/refresh", tags=["Public"])
async def refresh_credential(type: str, entity: str, cardinality: str, request: Request):
entity_id = entity
cardinality_id = cardinality
Expand Down
27 changes: 17 additions & 10 deletions backend/app/routers/registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def list_issuer_registrations():
async def register_issuer(request_body: IssuerRegistration):
registration = vars(request_body)

# Register issuer on TDW server and create DID Document
# Register issuer on DID Web server and create DID Document
did_document, authorized_key = await PublisherRegistrar().register_issuer(
registration
)
Expand All @@ -65,6 +65,8 @@ async def register_issuer(request_body: IssuerRegistration):
)
async def register_credential_type(request_body: CredentialRegistration):
credential_registration = request_body.model_dump()
credential_type = credential_registration.get("type")
credential_version = credential_registration.get("version")

mongo = MongoClient()

Expand Down Expand Up @@ -95,6 +97,12 @@ async def register_credential_type(request_body: CredentialRegistration):

# Fetch remote context
context = httpx.get(credential_registration["relatedResources"]["context"]).json()

# Inject well known context components
context['@context']['SimpleRefreshQuery'] = 'https://schema.org/WebAPI'
context['@context']['OCABundle'] = 'https://oca.colossi.network/specification/#bundle'
settings.LOGGER.info(context)

# TODO, test context
# credential_template['relatedResources'] = [
# {
Expand All @@ -115,15 +123,14 @@ async def register_credential_type(request_body: CredentialRegistration):

# Create OCA Bundle
oca_bundle = OCAProcessor().create_bundle(credential_registration, credential_template)
# context['@context']['OCABundle'] = ''
# credential_template['renderMethod'] = [
# {
# 'type': 'OCABundle',
# 'id': f'https://{settings.DOMAIN}/bundles/{credential_type}/{version}',
# 'name': 'Overlay Capture Architecture Bundle',
# 'digestMultibase': generate_digest_multibase(oca_bundle),
# }
# ]
credential_template['renderMethod'] = [
{
'type': 'OCABundle',
'id': f'https://{settings.DOMAIN}/bundles/{credential_type}/{credential_version}',
'name': 'Overlay Capture Architecture Bundle',
'digestMultibase': generate_digest_multibase(oca_bundle),
}
]

# Register credential type with Orgbook
await OrgbookPublisher().create_credential_type(credential_registration)
Expand Down
3 changes: 2 additions & 1 deletion backend/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import base64
import hashlib
import validators
from canonicaljson import encode_canonical_json
import re


Expand All @@ -26,7 +27,7 @@ def timestamp(minutes_forward=0):
return str((now + delta).isoformat("T", "seconds"))

def generate_digest_multibase(content):
return hashlib.sha256(content.encode()).digest()
return multibase.encode(hashlib.sha256(encode_canonical_json(content)).digest(), "base58btc")

def verkey_to_multikey(verkey):
return multibase.encode(bytes.fromhex(f"ed01{multibase.decode(f'z{verkey}').hex()}"), "base58btc")
Expand Down
4 changes: 2 additions & 2 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"app:app",
host="0.0.0.0",
port=8000,
reload=True,
# workers=4,
# reload=True,
workers=4,
)

0 comments on commit 304a929

Please sign in to comment.