-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: pstlouis <[email protected]>
- Loading branch information
1 parent
8c505da
commit ff34ded
Showing
12 changed files
with
243 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,29 @@ | ||
from typing import Union, List, Dict | ||
from typing import Union, List, Dict, Any | ||
from pydantic import BaseModel, Field | ||
from .did_document import DidDocument | ||
from config import settings | ||
|
||
class BaseRequest(BaseModel): | ||
def model_dump(self, **kwargs) -> Dict[str, Any]: | ||
return super().model_dump(by_alias=True, exclude_none=True, **kwargs) | ||
|
||
class RegisterDID(BaseModel): | ||
didDocument: DidDocument = Field() | ||
|
||
class RequestDID(BaseRequest): | ||
namespace: str = Field(example="example") | ||
identifier: str = Field(example="issuer") | ||
|
||
|
||
class RegisterDID(BaseRequest): | ||
didDocument: DidDocument = Field(example={ | ||
"@context": [], | ||
"id": f"{settings.DID_WEB_BASE}:example:issuer" | ||
}) | ||
|
||
|
||
class RequestDIDUpgrade(BaseRequest): | ||
id: str = Field(example=f"{settings.DID_WEB_BASE}:example:issuer") | ||
updateKey: str = Field(example="z...") | ||
|
||
|
||
class PublishLogEntry(BaseRequest): | ||
logEntry: list = Field() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,70 @@ | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from fastapi import APIRouter, Depends, HTTPException, Response | ||
from fastapi.responses import JSONResponse | ||
from app.models.web_requests import RegisterDID | ||
from app.models.web_requests import RequestDID, RegisterDID, RequestDIDUpgrade, PublishLogEntry | ||
from config import settings | ||
from app.plugins import AskarVerifier, AskarStorage, TrustDidWeb | ||
from app.dependencies import ( | ||
identifier_available, | ||
did_document_exists, | ||
valid_did_registration, | ||
) | ||
from app.utilities import create_did_doc_template | ||
# from app.dependencies import ( | ||
# did_document_exists, | ||
# valid_did_registration, | ||
# ) | ||
from app.utilities import location_available, to_did_web, valid_did_registration, did_document_exists, bootstrap_did_doc | ||
import jsonlines | ||
import json | ||
|
||
router = APIRouter() | ||
router = APIRouter(tags=["Identifiers"]) | ||
|
||
|
||
@router.get("/{namespace}/{identifier}", summary="Request DID configuration.") | ||
async def get_did( | ||
namespace: str, identifier: str, logs: bool = False, dependency=Depends(identifier_available) | ||
): | ||
did_doc = create_did_doc_template(namespace, identifier) | ||
if logs: | ||
return JSONResponse( | ||
status_code=200, | ||
content={ | ||
"logEntry": TrustDidWeb().provision_log_entry(did_doc), | ||
"options": AskarVerifier().create_proof_config(), | ||
}, | ||
) | ||
@router.post("/did/request", summary="Request new identifier.") | ||
async def request_did(request_body: RequestDID): | ||
did = to_did_web(request_body.model_dump()['namespace'], request_body.model_dump()['identifier']) | ||
await location_available(did) | ||
return JSONResponse( | ||
status_code=200, | ||
content={ | ||
"document": did_doc, | ||
"document": bootstrap_did_doc(did), | ||
"options": AskarVerifier().create_proof_config(), | ||
}, | ||
) | ||
|
||
|
||
@router.post("/{namespace}/{identifier}", summary="Register DID.") | ||
async def register_did( | ||
request_body: RegisterDID, | ||
namespace: str, | ||
identifier: str, | ||
did_document=Depends(valid_did_registration), | ||
): | ||
await AskarStorage().store("didDocument", f"{namespace}:{identifier}", did_document) | ||
return JSONResponse(status_code=201, content={"didDocument": did_document}) | ||
|
||
|
||
@router.put("/{namespace}/{identifier}", summary="Update DID document.") | ||
async def update_did( | ||
namespace: str, identifier: str, dependency=Depends(did_document_exists) | ||
): | ||
raise HTTPException(status_code=501, detail="Not implemented.") | ||
@router.post("/did/register", summary="Register identifier.") | ||
async def upgrade_did(request_body: RegisterDID): | ||
did_document = request_body.model_dump()['didDocument'] | ||
await location_available(did_document['id']) | ||
await valid_did_registration(did_document) | ||
await AskarStorage().store("didDocument", did_document['id'], did_document) | ||
return JSONResponse( | ||
status_code=201, | ||
content={ | ||
"didDocument": did_document, | ||
}, | ||
) | ||
|
||
|
||
@router.delete("/{namespace}/{identifier}", summary="Archive DID.") | ||
async def delete_did( | ||
namespace: str, identifier: str, dependency=Depends(did_document_exists) | ||
): | ||
raise HTTPException(status_code=501, detail="Not implemented.") | ||
@router.post("/did/upgrade", summary="Upgrade to Trust DID Web.") | ||
async def request_did_upgrade(request_body: RequestDIDUpgrade): | ||
await did_document_exists(request_body.model_dump()['id']) | ||
did_document = await AskarStorage().fetch("didDocument", request_body.model_dump()['id']) | ||
log_entry = TrustDidWeb().provision_log_entry(did_document, request_body.model_dump()['updateKey']) | ||
return JSONResponse( | ||
status_code=200, | ||
content={ | ||
"logEntry": log_entry, | ||
"proofOptions": AskarVerifier().create_proof_config(challenge=log_entry[0]), | ||
}, | ||
) | ||
|
||
|
||
@router.get("/{namespace}/{identifier}/did.json", summary="Get DID document.") | ||
async def get_did( | ||
namespace: str, identifier: str, dependency=Depends(did_document_exists) | ||
): | ||
did_doc = await AskarStorage().fetch("didDocument", f"{namespace}:{identifier}") | ||
return JSONResponse(status_code=200, content=did_doc) | ||
@router.post("/did/log", summary="Publish log entry.") | ||
async def publish_log(request_body: PublishLogEntry, response: Response): | ||
log_entry = request_body.model_dump()['logEntry'] | ||
did_tdw = log_entry[3]['value']['id'] | ||
did_web = 'did:web:'+':'.join(did_tdw.split(':')[3:]) | ||
await did_document_exists(did_web) | ||
# await valid_log_entry(log_entry) | ||
did_document = await AskarStorage().fetch("didDocument", did_web) | ||
did_document['alsoKnownAs'] = log_entry[3]['value']['id'] | ||
did_document = await AskarStorage().update("didDocument", did_web, did_document) | ||
logs = [json.dumps(log_entry)] | ||
did_document = await AskarStorage().store("didLogs", did_web, logs) | ||
return JSONResponse(status_code=201, content={'did': did_tdw}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from fastapi import APIRouter, Depends, HTTPException, Response | ||
from fastapi.responses import JSONResponse | ||
from config import settings | ||
from app.plugins import AskarVerifier, AskarStorage, TrustDidWeb | ||
from app.utilities import to_did_web, did_document_exists | ||
import jsonlines | ||
import json | ||
|
||
router = APIRouter(tags=["Resolvers"]) | ||
|
||
@router.get("/.well-known/did.json") | ||
async def get_endorser_did(): | ||
did_document = await AskarStorage().fetch("didDocument", settings.DID_WEB_BASE) | ||
return JSONResponse(status_code=200, content=did_document) | ||
|
||
@router.get("/{namespace}/{identifier}/did.json") | ||
async def get_did( | ||
namespace: str, identifier: str | ||
): | ||
did = to_did_web(namespace, identifier) | ||
await did_document_exists(did) | ||
did_document = await AskarStorage().fetch("didDocument", did) | ||
return JSONResponse(status_code=200, content=did_document) | ||
|
||
|
||
@router.get("/{namespace}/{identifier}/did.jsonl") | ||
async def get_did_logs( | ||
namespace: str, identifier: str, response: Response | ||
): | ||
did = to_did_web(namespace, identifier) | ||
await did_document_exists(did) | ||
did_logs = await AskarStorage().fetch("didLogs", did) | ||
did_logs = jsonlines.Reader(did_logs).read() | ||
response.headers['Content-Type'] = 'application/octet-stream' | ||
return did_logs |
Oops, something went wrong.